PHP上传文件失败
PHP Uploading file unsuccessful
我正在尝试将文件上传到我的本地服务器,但总是失败。
我所有的文件都在里面 /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!";
}
?>
试试这个:
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。如果是这样,它会转到上传部分。
你没有设置变量
$target_path
你的意思是但没用过
$target_file
相反。
您的输入文件是
<input name="uploadedfile" type="file" />
所以将 $_FILES['fileToUpload']['name']
更改为 $_FILES['uploadedfile']['name']
$_FILES['uploadedfile']['name']
必须具有文件字段Name
属性的值
我正在尝试将文件上传到我的本地服务器,但总是失败。
我所有的文件都在里面 /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!";
}
?>
试试这个:
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。如果是这样,它会转到上传部分。
你没有设置变量
$target_path
你的意思是但没用过
$target_file
相反。
您的输入文件是
<input name="uploadedfile" type="file" />
所以将 $_FILES['fileToUpload']['name']
更改为 $_FILES['uploadedfile']['name']
$_FILES['uploadedfile']['name']
必须具有文件字段Name
属性的值