我有这个代码用于上传服务器上的文件。但我得到一个由else语句生成的自定义错误,但我需要知道php无法上传文件的真正原因。 我收到错误2但我没有得到实际的消息。 有什么建议吗?
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! ".$_FILES['uploadedfile']['error'];
}
答案 0 :(得分:1)
我不确定为什么要在目标参数$target_path."/"
的末尾添加目录分隔符。
删除它或替换为$target_path.basename( $_FILES['uploadedfile']['name'])
应该可以解决问题。
<?php
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! ".$_FILES['uploadedfile']['error'];
}
?>
答案 1 :(得分:1)
你可以尝试这个实际发生的错误
$upload_errors = array(
UPLOAD_ERR_OK => "No errors.",
UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.",
UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",
UPLOAD_ERR_PARTIAL => "Partial upload.",
UPLOAD_ERR_NO_FILE => "No file.",
UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
UPLOAD_ERR_EXTENSION => "File upload stopped by extension.",
UPLOAD_ERR_EMPTY => "File is empty." // add this to avoid an offset
);
// error: report what PHP says went wrong
$err = $upload_errors[$_FILES['uploadedfile']['error']];
echo $err;