我试图将文件上传到我的本地服务器,但它一直没有成功。
我的所有文件都在/ var / www / html /中 但是我在html文件夹中创建了一个名为uploads的文件夹,我将其权限更改为777(我从搜索中获得的平均值是最符合我需求的)
这是我的代码: 的的index.html
<!DOCTYPE html>
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
upload.php的
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES['fileToUpload']['name']);
echo "Target File: " . $target_file . "<br />";
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
答案 0 :(得分:1)
您的输入文件是
<input name="uploadedfile" type="file" />
所以将$_FILES['fileToUpload']['name']
更改为$_FILES['uploadedfile']['name']
$_FILES['uploadedfile']['name']
必须具有文件字段Name
属性的值
答案 1 :(得分:0)
试试这个:
<强>的index.html 强>
<!DOCTYPE html>
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
<强> upload.php的强>
if(isset($_FILES["uploadedfile"]["type"]) && ($_FILES["uploadedfile"]["size"] < 5000000)){
$sourcePath = $_FILES['uploadedfile']['tmp_name'];
$file = $_FILES['uploadedfile']['name'];
$targetPath = "/uploads/".$file;
if(move_uploaded_file($sourcePath,$targetPath)){
echo "The file: ".$_FILES['uploadedfile']['name']." has been uploaded";
}else{
echo "Looks like it failed.";
}
}else{
echo "You forgot to select a file, or the file size is too large.";
}
这样做是检查文件是否存在并检查它是否小于5MB。如果是这样,它将继续上传到上传部分。
答案 2 :(得分:0)
您没有设置变量
$target_path
你的意思是但没有用过
$target_file
代替。