我有一个访问者可以上传文件的页面,但这不是必需的,所以在我的脚本中我设置了一个条件,如果没有设置文件输入(我命名为#upload; upload')或空的不要上传任何内容,文件名将等于' file'已经存在于db中;否则上传它。 问题是条件不起作用;即使我将输入留空,也会执行上传脚本;我知道,因为我得到了脚本中声明的消息 这是编码并谢谢
//select exercice
$ex_query = mysqli_query($link, " SELECT * FROM exercices WHERE ID = '".$exercice_ID."' ");
$exercice = mysqli_fetch_assoc($ex_query);
// --- update script if post
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_FILES["upload"]) && !empty($_FILES["upload"])){
$target_dir = "docs/";
$date_f_n = date("Y-m-d_h-i-sa");
$file_n = basename( $_FILES["upload"]["name"]);
$file_n = $date_f_n.$file_n;
$target_file = $target_dir . $file_n;
$uploadOk = 1;
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file is a actual image or fake image
$check = filesize($_FILES["upload"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
$filesizeer = "File is not an image.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["upload"]["size"] > 5000000) {
$filesizeer = "Sorry, your file is too large.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$error = "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file)) {
$upsuccess= "The file ". $file_n. " has been uploaded.";
} else {
$upfaled = "Sorry, there was an error uploading your file.";
}
}//end else upload = 0
}else{
$file_n = $exercice['file'];
}
答案 0 :(得分:0)
与上传有关的 FIRST 事项是检查上传是否全部执行,并且成功。仅仅因为存在$_FILES['foo']
并不意味着此foo
文件的上传实际上已成功。
裸露的最低限度检查:
if ($_FILES['foo']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code #" . $_FILES['foo']['error']);
}
甚至
if (isset($_FILES['foo']) && etc...)
如果你想要真正严格正确。
如果您想允许非上传,但仍然无法解决实际错误,请检查您要允许的特定错误代码为“ok”。错误代码在此处定义:http://php.net/manual/en/features.file-upload.errors.php