<?php
if($_SERVER["REQUEST_METHOD"] == "GET"){
$file = $_GET["fname"];
}else{
if(isset($_POST["content"]) && isset($_POST["file"])){
$folder = "/uploads/";
$file = $_POST["file"];
$content = $_POST["content"];
if(file_exists($file)){
echo "<h1>That file already exists on the server</h1>";
}else{
file_put_contents(basename($folder, $file), $content);
}
}else{
echo "<h1>Request has failed to be sent</h1>";
}
}
我使用 file_put_contents 因为我认为我可以将文件放在那里等等。
我尝试使用我的代码
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET") {
$file = $_GET["fname"];
} else {
if (isset($_POST["content"]) && isset($_POST["file"])) {
$folder = __DIR__ . "/uploads/";
$file = basename($_POST["file"]);
$content = $_POST["content"];
$filePath = $folder . $file;
if (file_exists($filePath)) {
echo "<h1>That file already exists on the server</h1>";
} else {
if (!is_dir($folder)) {
mkdir($folder, 0777, true);
}
if (file_put_contents($filePath, $content) !== false) {
echo "<h1>File successfully created</h1>";
} else {
echo "<h1>Failed to save file</h1>";
}
}
} else {
echo "<h1>Request has failed to be sent</h1>";
}
}
?>
此代码应该执行上传。