mysql - Error in php error handling (file upload) -
i want put out error message when uploaded image size on 3mb. current code. should put out error message when image on 3mb, nothing. what's wrong code?
if ( $_files['file']['size'] != 0 ) { //image check start if ((($_files["file"]["type"] == "image/gif") || ($_files["file"]["type"] == "image/jpeg") || ($_files["file"]["type"] == "image/png") || ($_files["file"]["type"] == "image/pjpeg")) && ($_files["file"]["size"] < 3072000)) //image check end { if (file_exists($upload_path."/".$_files["file"]["name"])) { $file_tmp = $_files["file"]["name"]; } //link if there file identical file name else { $photoid = $upfile_idx.".".substr($_files['file']['name'],-3); move_uploaded_file($_files["file"]["tmp_name"], $upload_path."/".$photoid); $file_tmp = $photoid ; } //upload image file upload folder , generate id image } else { error("maximum image size exceeded or invalid file format."); } } //insert $file_tmp database here ---------- error code (added later) function error($msg) { echo "<script>alert(\"$msg\");history.go(-1);</script>"; exit; }
i've found what's wrong. in php.ini file, there 'upload_max_filesize = 3m' , that's causing problem. when changed 'upload_max_filesize = 4m', worked fine. thank help.
something on following should good.
<?php if ( $_files['file']['size'] != 0 ) { //image check start if(!in_array($_files["file"]["type"], array("image/gif", "image/jpeg", "image/png", "image/pjpeg"))) { error("file should jpg/jpeg/png/pjepg only"); return; } if($_files["file"]["size"] > 3145728) { error("file should less 3 mb"); return; } #rest of upload code should go below here. } ?>
3145728 translates 3 mb in bytes since $_files["file"]["size"] gives size of file being uploaded in bytes.
Comments
Post a Comment