php upload file condition not working -
i have page the visitor can upload file, thats not required, in script put condition if file input (i named 'upload') not set or empty don't upload anything, , file name equal 'file' existed in db; else upload it. problem condition not working; when leave input empty upload script executed; know because messages declared in script here coded , thank you
//select exercice $ex_query = mysqli_query($link, " select * exercices 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 actual image or fake image $check = filesize($_files["upload"]["tmp_name"]); if($check !== false) { $uploadok = 1; } else { $filesizeer = "file not image."; $uploadok = 0; } // check file size if ($_files["upload"]["size"] > 5000000) { $filesizeer = "sorry, file large."; $uploadok = 0; } // check if $uploadok set 0 error if ($uploadok == 0) { $error = "sorry, file not uploaded."; // if ok, try 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 error uploading file."; } }//end else upload = 0 }else{ $file_n = $exercice['file']; }
the first thing have upload check if upload performed @ all, , succeeded. because $_files['foo']
exists doesn't mean upload of foo
file succeeded.
bare minimum checking:
if ($_files['foo']['error'] !== upload_err_ok) { die("upload failed error code #" . $_files['foo']['error']); }
or even
if (isset($_files['foo']) && etc...)
if want strictly correct.
if want allow non-uploads, still fail on real errors, check specific error codes you'd allow "ok". error codes defined here: http://php.net/manual/en/features.file-upload.errors.php
Comments
Post a Comment