krash11554 Posted October 2, 2012 Share Posted October 2, 2012 (edited) I have code that checks if a file is selected do this. But my problem is it is running that code even though i dont have a selected file in a input box. heres the code. if(isset($_FILES['editcar_fileupload']) && empty($_POST) === false){ //check where to start uploading pictures $image_name = $_FILES['editcar_fileupload']['name']; $image_size = $_FILES['editcar_fileupload']['size']; $image_temp = $_FILES['editcar_fileupload']['tmp_name']; $allowed_ext = array('jpg','jpeg','png','gif'); $count = count($image_name); if (isset($image_name) && empty($image_name) === false){ foreach($image_name as $image_names){ $image_ext = strtolower(end(explode('.', $image_names))); if(in_array($image_ext, $allowed_ext) === false && $true === true){ $errors[] = 'File type is not allowed'; } } if($count > 10){ $errors[] = 'You can not upload more than 10 pictures for your car.'; } } } here is the html <input type="file" onchange="this.form.editcar_fakeinput.value = this.value;" name="editcar_fileupload[]" multiple="multiple" id="editcar_fileupload" /> it says the $image_name has values in it but it really dosent. Edited October 2, 2012 by krash11554 Quote Link to comment https://forums.phpfreaks.com/topic/269021-uploading-picture-problem/ Share on other sites More sharing options...
requinix Posted October 2, 2012 Share Posted October 2, 2012 Look at the error $_FILES['editcar_fileupload']['error'] to see if a file was actually uploaded (or if there was some other error). Error codes Quote Link to comment https://forums.phpfreaks.com/topic/269021-uploading-picture-problem/#findComment-1382384 Share on other sites More sharing options...
krash11554 Posted October 2, 2012 Author Share Posted October 2, 2012 I have errror 4 which is no file is selected but how come it passes the isset()? if there is no file selected?? Quote Link to comment https://forums.phpfreaks.com/topic/269021-uploading-picture-problem/#findComment-1382386 Share on other sites More sharing options...
chintansshah Posted October 3, 2012 Share Posted October 3, 2012 I suggest to use empty() instead of isset() Quote Link to comment https://forums.phpfreaks.com/topic/269021-uploading-picture-problem/#findComment-1382411 Share on other sites More sharing options...
PFMaBiSmAd Posted October 3, 2012 Share Posted October 3, 2012 The $_FILES array will be set and not empty for most of the possible error conditions. You must test if the file was actually uploaded, the ['error'] element is set and equal to zero, before you can access any of the uploaded file information. Quote Link to comment https://forums.phpfreaks.com/topic/269021-uploading-picture-problem/#findComment-1382412 Share on other sites More sharing options...
White_Lily Posted October 3, 2012 Share Posted October 3, 2012 (edited) could check to see if the field is empty? The HTML: <html> <form action="" method="POST" enctype="multipart/form-data"> <label>File Upload</label><input type="file" name="file" /> <input type="submit" name="submit" value="Upload" /> </form> </html> The PHP: <?php if(!$_POST["file"]){ echo "No file selected."; }else{ //processing code } ?> Edited October 3, 2012 by White_Lily Quote Link to comment https://forums.phpfreaks.com/topic/269021-uploading-picture-problem/#findComment-1382442 Share on other sites More sharing options...
requinix Posted October 3, 2012 Share Posted October 3, 2012 Sigh. I suggest to use empty() instead of isset() Won't work. The $_FILES array will be set and not empty for most of the possible error conditions. You must test if the file was actually uploaded, the ['error'] element is set and equal to zero, before you can access any of the uploaded file information. ^ THIS could check to see if the field is empty? <?php if(!$_POST["file"]){ echo "No file selected."; }else{ //processing code } ?> Won't work. Quote Link to comment https://forums.phpfreaks.com/topic/269021-uploading-picture-problem/#findComment-1382544 Share on other sites More sharing options...
PFMaBiSmAd Posted October 3, 2012 Share Posted October 3, 2012 I have errror 4 which is no file is selected but how come it passes the isset()? if there is no file selected?? If you saw a value of 4 in the error element - $_FILES['editcar_fileupload']['error'], wouldn't that mean that $_FILES['editcar_fileupload'] ISSET? Quote Link to comment https://forums.phpfreaks.com/topic/269021-uploading-picture-problem/#findComment-1382582 Share on other sites More sharing options...
Rage Posted October 4, 2012 Share Posted October 4, 2012 One way todo this (if i am understanding correctly): if($_GET['action']=="upload") { // Process Image } then have the upload form <form method="post" action="yourpage.php?action=upload" > Quote Link to comment https://forums.phpfreaks.com/topic/269021-uploading-picture-problem/#findComment-1382602 Share on other sites More sharing options...
PFMaBiSmAd Posted October 4, 2012 Share Posted October 4, 2012 That will let you detect if a form was submitted, but it doesn't tell you anything about the uploaded file. Quote Link to comment https://forums.phpfreaks.com/topic/269021-uploading-picture-problem/#findComment-1382608 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.