rugzo Posted April 5, 2009 Share Posted April 5, 2009 Hi All, i have a problem. I have a script which uploads and rezises the image directly to mysql. I implemented it to my registration form so the users can also upload and update their photo. My only problem is if the photo input field is blank (if the user doesn't want to upload his photo) the script stops with the error that the loaded file is not an image. Here is the script --> include_once ('image.php'); $sinif = new imaj; $tip = array ( "image/pjpeg", "image/jpeg", "image/jpg", "image/gif", "image/png", "image/x-png" ); if (!empty($_FILES['dosya'])) { if (!in_array($_FILES['dosya']['type'],$tip)) { exit('file is not an image!'); } $sinif->img($_FILES['dosya']['tmp_name']); $sinif->resize(130); $sinif->store('tmp/'.$_FILES['dosya']['name']); $icerik = base64_encode(file_get_contents('tmp/'.$_FILES['dosya']['name'])); $dosya_bilgi = getimagesize('tmp/'.$_FILES['dosya']['name']); if ($query !== false) { if (unlink('tmp/'.$_FILES['dosya']['name']) == true) { } } else { if (unlink('tmp/'.$_FILES['dosya']['name']) == true) { } } } Even ther is the --> if (!empty($_FILES['dosya'])) the script doesn't recognize that the field is empty and that it has to continue the next query. I tried it also like -> if(isset($_POST['dosya'])) or if($_POST['dosya']<>'') or if($_POST['dosya'] !=''). I succeeded that with one of the aboves that it recognizes that the dosya field is empty and it also worked to continue with the rest of the query but then i saw that it is also not uploading the file when inserted. Can anyone help?? Link to comment https://forums.phpfreaks.com/topic/152657-image-upload-script/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 5, 2009 Share Posted April 5, 2009 You need to check the ['error'] element before you do anything with any of the uploaded file information - http://www.php.net/manual/en/features.file-upload.errors.php You will note that an error value of 4 indicates that no file was uploaded. A value of 0 indicates the file was uploaded without error. You should only process the uploaded file if the value is 0. Link to comment https://forums.phpfreaks.com/topic/152657-image-upload-script/#findComment-801696 Share on other sites More sharing options...
rugzo Posted April 5, 2009 Author Share Posted April 5, 2009 Thanks for the quick reply but i don't have a problem with the file upload. If a file is inserted it uploads it without any problem. The only problem is if a file is not inserted, then it returns that the file is not an image. I just want this; it should check if a file is inserted. If the file is not inserted (it can be that the user wants the add his photo after he registered...) it should continue with the rest of the form and not stop with the error that the file is not an image --> if (!in_array($_FILES['dosya']['type'],$tip)) { exit('file is not an image!'); } I also do not want to remove this part since it checks the type of the file. I don't know but i think that this part is not doing its job, it doesnt stop if the field is empty --> if (!empty($_FILES['dosya'])) { Link to comment https://forums.phpfreaks.com/topic/152657-image-upload-script/#findComment-801704 Share on other sites More sharing options...
PFMaBiSmAd Posted April 5, 2009 Share Posted April 5, 2009 $_FILES['dosya'] will only be empty for one of the following - uploads are not enabled on the sever, your form does not have enctype="multipart/form-data", or the size of the uploaded file exceeds post_max_size. When a file is not selected in an upload form $_FILES['dosya'] is not empty. It in fact contains $_FILES['dosya']['error'] with a value of 4 like you were already told. By testing for $_FILES['dosya']['error'] == 0, before you use any of the unloaded file information, your code will at least not attempt to insert any of the information into the database unless a file was successfully uploaded. Link to comment https://forums.phpfreaks.com/topic/152657-image-upload-script/#findComment-801708 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.