kapz22 Posted March 7, 2010 Share Posted March 7, 2010 How do i check if a file has been selected to uplload? I tried doing: if ($_FILES["userfile"] != null) { echo "a file has been selected to upload"; exit; } else { echo "a file has not been selected"; exit; } this does not work and i dont know why. please can some one help. thanks Quote Link to comment https://forums.phpfreaks.com/topic/194446-_files/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 7, 2010 Share Posted March 7, 2010 $_FILES["userfile"] will be an array. The php.net upload documentation shows what elements it contains - http://us3.php.net/manual/en/features.file-upload.php About the only time it would be NULL would be if uploads are not enabled on your server or the HTTP request for your page was not the result of a valid upload form with a form field by the name "userfile". You can also see what is in the array using the following code - echo "<pre>",print_r($_FILES,true),"</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/194446-_files/#findComment-1022748 Share on other sites More sharing options...
kapz22 Posted March 7, 2010 Author Share Posted March 7, 2010 i came up with this, but i was wondering if there was a better way to do it. if($_FILES["userfile"]["name"] != "") { etc etc Quote Link to comment https://forums.phpfreaks.com/topic/194446-_files/#findComment-1022750 Share on other sites More sharing options...
LeadingWebDev Posted March 7, 2010 Share Posted March 7, 2010 check $_FILES["field_name"]["tmp_name"] != "none" this should work, as if file not uploaded, tmp name by default will come "none", and not null =) also u can check filesize or name as u did before. name becomes null, size becomes 0 Quote Link to comment https://forums.phpfreaks.com/topic/194446-_files/#findComment-1022753 Share on other sites More sharing options...
kapz22 Posted March 7, 2010 Author Share Posted March 7, 2010 Thanks. Im just a php novice so i dont know if the way i am doing things is the right way of doing it. Quote Link to comment https://forums.phpfreaks.com/topic/194446-_files/#findComment-1022755 Share on other sites More sharing options...
PFMaBiSmAd Posted March 7, 2010 Share Posted March 7, 2010 The ['error'] element will be a zero value if a file was selected and uploaded successfully. Nothing else really makes sense. The ["name"] element can be set, but there can still be upload errors and the ["tmp_name"] element will never be the string "none". Quote Link to comment https://forums.phpfreaks.com/topic/194446-_files/#findComment-1022757 Share on other sites More sharing options...
LeadingWebDev Posted March 7, 2010 Share Posted March 7, 2010 i cant call myself pro as well, but exactly true way to check if file uploaded is to check tmp file, before preforming any actions with file. Quote Link to comment https://forums.phpfreaks.com/topic/194446-_files/#findComment-1022758 Share on other sites More sharing options...
thewooleymammoth Posted March 8, 2010 Share Posted March 8, 2010 http://us.php.net/manual/en/function.isset.php Quote Link to comment https://forums.phpfreaks.com/topic/194446-_files/#findComment-1022876 Share on other sites More sharing options...
khr2003 Posted March 8, 2010 Share Posted March 8, 2010 check out this function: http://php.net/manual/en/function.is-uploaded-file.php Quote Link to comment https://forums.phpfreaks.com/topic/194446-_files/#findComment-1022885 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.