[英]Error uploading file from HTML form
Here is a problem. 这是个问题。 I have an HTML form with several fields in it. 我有一个HTML表单,其中包含多个字段。 One of the fields - 'Upload file'. 字段之一-“上传文件”。 When I upload a file, everything works properly. 当我上传文件时,一切正常。 But when I choose to submit the form without a file, it gives me the error message: "There was an error uploading the file, please try again". 但是,当我选择提交不带文件的表单时,它会显示错误消息:“上载文件时出错,请重试”。 Looks to me that the script thinks that uploading a file is mandatory. 在我看来,脚本认为必须上传文件。 How do I change it? 我该如何更改?
Here is my PHP: 这是我的PHP:
//File upload
// Where the file is going to be placed
$target_path = "uploads/";
// Add the original filename to our target path.
//Result is "uploads/filename.extension"
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
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!";
}
//End of file upload
Thank you! 谢谢!
You should check using the function is_uploaded_file 您应该使用函数is_uploaded_file进行检查
try adding the following condition before calling the function move_uploaded_file 在调用函数move_uploaded_file之前尝试添加以下条件
if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) {
move_uploaded_file
returns a bool, true
or false
, depending on the success of the operation. move_uploaded_file
返回布尔值true
或false
,取决于操作是否成功。
A solution would be to check more rigorously, eg if a file was uploaded at all via is_uploaded_file
function. 一种解决方案是更严格地检查,例如,是否通过is_uploaded_file
函数上载了文件。
For proper working, the function
is_uploaded_file()
needs an argument like$_FILES['userfile']['tmp_name']
, - the name of the uploaded file on the clients machine$_FILES['userfile']['name']
does not work. 为了正常工作,函数is_uploaded_file()
需要一个$_FILES['userfile']['tmp_name']
-客户端计算机$_FILES['userfile']['name']
上载文件$_FILES['userfile']['name']
不行。
The target path of move_uploaded_files should be a folder, not a file. move_uploaded_files的目标路径应该是文件夹,而不是文件。
You should also check if the folder is_writeable and is_dir. 您还应该检查文件夹is_writeable和is_dir。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.