bugzy Posted April 26, 2012 Share Posted April 26, 2012 A beginner question.. I have tried "isset" but it aint working.. I wonder what PHP function to use to check if a user actually put a file to be uploaded on the server. A validation just in case a user just click the submit button w/out even choosing a file first. I have this code but it aint working... <?php if(isset($_POST['submit'])) { if(isset($_FILES['uploaded_file'])) { echo "<span class=\"error_validation\">You haven't choose a logo to upload!<br></span>"; echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>"; } else { if($_FILES['uploaded_file']["type"] == "image/gif") { $target_path = "logo/"; $target_path = $target_path. basename('matzhee_logo.gif'); if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) { echo "<span class=\"error_validation\">The Logo has been successfully changed! Refresh the page to see the changed!<br></span>"; echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>"; } else { echo "<span class=\"error_validation\">There was an error uploading the logo. Pls. try again.<br></span>"; echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>"; } } else { echo "<span class=\"error_validation\">Invalid file format. We are only accepting image file. Pls. try again.<br></span>"; echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>"; } } } else { me_redirect_to("edit_logo.php"); } ?> Anyone? thanks Quote Link to comment https://forums.phpfreaks.com/topic/261668-how-to-check-if-theres-a-file-to-be-pass-on-the-server/ Share on other sites More sharing options...
xyph Posted April 26, 2012 Share Posted April 26, 2012 You probably wanted to use if(isset($_FILES['uploaded_file']) == FALSE) As of now, you're producing an error if it is set. You might also want to check if it's empty Quote Link to comment https://forums.phpfreaks.com/topic/261668-how-to-check-if-theres-a-file-to-be-pass-on-the-server/#findComment-1340872 Share on other sites More sharing options...
bugzy Posted April 26, 2012 Author Share Posted April 26, 2012 You probably wanted to use if(isset($_FILES['uploaded_file']) == FALSE) As of now, you're producing an error if it is set. You might also want to check if it's empty Hey thanks I have tried both like this one <?php if(empty($_FILES['uploaded_file'])) ?> but it's still bypassing the validation... Quote Link to comment https://forums.phpfreaks.com/topic/261668-how-to-check-if-theres-a-file-to-be-pass-on-the-server/#findComment-1340874 Share on other sites More sharing options...
xyph Posted April 26, 2012 Share Posted April 26, 2012 Show us your form, and try using print_r($_FILES); in your action page. It will spit out everything contained in your $_FILES array. Quote Link to comment https://forums.phpfreaks.com/topic/261668-how-to-check-if-theres-a-file-to-be-pass-on-the-server/#findComment-1340875 Share on other sites More sharing options...
bugzy Posted April 26, 2012 Author Share Posted April 26, 2012 Show us your form, and try using print_r($_FILES); in your action page. It will spit out everything contained in your $_FILES array. form <?php <form enctype="multipart/form-data" method="post" action="<?php echo "uploaded_logo.php?edited=1" ?>"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> <tr> <td>Choose a Logo to Upload: </td> <td><input name="uploaded_file" type="file" /></td> </tr> <tr> <td> </td> <td><input type="submit" value="Upload File" name="submit"></td> </tr> </form> ?> print_r Array ( [name] => [type] => [tmp_name] => [error] => 4 => 0 ) what do you think is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/261668-how-to-check-if-theres-a-file-to-be-pass-on-the-server/#findComment-1340878 Share on other sites More sharing options...
xyph Posted April 26, 2012 Share Posted April 26, 2012 http://www.php.net/manual/en/features.file-upload.errors.php UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded. You could try something like this for validation. <?php // Check if the array hasn't even been set if( isset($_FILES['uploaded_file']) == FALSE ) { echo 'You must choose a file to upload'; // Otherwise, check if the error key isn't empty } elseif( empty($_FILES['uploaded_file']['error']) == FALSE ) { // If it's not empty, report which error was found switch( $_FILES['uploaded_file']['error'] ) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: echo 'File was too big'; break; case UPLOAD_ERR_PARTIAL: echo 'Only part of the file could be uploaded'; break; case UPLOAD_ERR_NO_FILE: echo 'No file was selected'; break; case UPLOAD_ERR_NO_TMP_DIR: echo 'No temporary directory specified for uploads'; break; case UPLOAD_ERR_CANT_WRITE: echo 'Could not write file to disk'; break; } // Otherwise, the array is set, and theres no errors } else { echo 'Everything went okay with '.$_FILES['uploaded_file']['name']; } ?> There's a lot there, but it pretty much covers any error you'd run in to. Let me know if you don't understand any of it, and I'll explain with more detail. Quote Link to comment https://forums.phpfreaks.com/topic/261668-how-to-check-if-theres-a-file-to-be-pass-on-the-server/#findComment-1340884 Share on other sites More sharing options...
bugzy Posted April 26, 2012 Author Share Posted April 26, 2012 http://www.php.net/manual/en/features.file-upload.errors.php UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded. You could try something like this for validation. <?php // Check if the array hasn't even been set if( isset($_FILES['uploaded_file']) == FALSE ) { echo 'You must choose a file to upload'; // Otherwise, check if the error key isn't empty } elseif( empty($_FILES['uploaded_file']['error']) == FALSE ) { // If it's not empty, report which error was found switch( $_FILES['uploaded_file']['error'] ) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: echo 'File was too big'; break; case UPLOAD_ERR_PARTIAL: echo 'Only part of the file could be uploaded'; break; case UPLOAD_ERR_NO_FILE: echo 'No file was selected'; break; case UPLOAD_ERR_NO_TMP_DIR: echo 'No temporary directory specified for uploads'; break; case UPLOAD_ERR_CANT_WRITE: echo 'Could not write file to disk'; break; } // Otherwise, the array is set, and theres no errors } else { echo 'Everything went okay with '.$_FILES['uploaded_file']['name']; } ?> There's a lot there, but it pretty much covers any error you'd run in to. Let me know if you don't understand any of it, and I'll explain with more detail. thanks so much xyph! I'm using it now and it'a an all in one validation for everything. Though I want to maximize it, where will I set up there the maximum file size allowed? Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/261668-how-to-check-if-theres-a-file-to-be-pass-on-the-server/#findComment-1340887 Share on other sites More sharing options...
xyph Posted April 26, 2012 Share Posted April 26, 2012 I'm not going to code for you, you put it where it logically makes sense. $_FILES['uploaded_files']['size'] should contain the file's size. There's more here: http://www.php.net/manual/en/features.file-upload.post-method.php Quote Link to comment https://forums.phpfreaks.com/topic/261668-how-to-check-if-theres-a-file-to-be-pass-on-the-server/#findComment-1340891 Share on other sites More sharing options...
bugzy Posted April 26, 2012 Author Share Posted April 26, 2012 I'm not going to code for you, you put it where it logically makes sense. $_FILES['uploaded_files']['size'] should contain the file's size. There's more here: http://www.php.net/manual/en/features.file-upload.post-method.php Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/261668-how-to-check-if-theres-a-file-to-be-pass-on-the-server/#findComment-1340892 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.