droidus Posted September 30, 2011 Share Posted September 30, 2011 i am trying to check if the array element (which would be a file) is null or not. when i run it, uploading one file, it says "Please select some files to upload first!" X 3. what am i doing wrong? my code: http://pastebin.com/m0k3fEd2 Quote Link to comment https://forums.phpfreaks.com/topic/248146-check-if-array-element-is-emptynull/ Share on other sites More sharing options...
Pikachu2000 Posted September 30, 2011 Share Posted September 30, 2011 Paste the code here in code tags instead of a link to pastebin. Quote Link to comment https://forums.phpfreaks.com/topic/248146-check-if-array-element-is-emptynull/#findComment-1274243 Share on other sites More sharing options...
droidus Posted September 30, 2011 Author Share Posted September 30, 2011 // If the folder is not in the default position, we must place it in the right spot for ($i = 0; $i<$filesCount; $i++) { if($filesCount[$i] != null) { if($folderDestination!="default") { if (file_exists("users/" . $_SESSION['user'] . "/uploads/" . $folderDestination . "/" . $_FILES["file"]["name"][$i])) { echo $_FILES["file"]["name"][$i] . " already exists.<br>"; } else { move_uploaded_file($_FILES["file"]["tmp_name"][$i], "users/" . $_SESSION['user'] . "/uploads/" . $folderDestination . "/" . $_FILES["file"]["name"][$i]); echo "Your file has been successfully uploaded, and you can now view it right away!<br><a href='users/' . $_SESSION[user] .'/uploads/' . $folderDestination . '/' . $_FILES[file][name][$i]Click here to go to your file!"; } } else { // If else, we put it in the uploads folder if (file_exists("users/" . $_SESSION['user'] . "/uploads/" . $_FILES["file"]["name"][$i])) { echo $_FILES["file"]["name"][$i] . " already exists.<br>"; } else { move_uploaded_file($_FILES["file"]["tmp_name"][$i], "users/" . $_SESSION['user'] . "/uploads/" . $_FILES["file"]["name"][$i]); echo "Your file has been successfully uploaded, and you can now view it right away!<br><a href='users/$_SESSION[user] /uploads/$_FILES[file][name][$i]' target='_blank'>Click here to go to your file</a>!"; } } } else { echo "Please select some files to upload first!"; } } Quote Link to comment https://forums.phpfreaks.com/topic/248146-check-if-array-element-is-emptynull/#findComment-1274245 Share on other sites More sharing options...
Pikachu2000 Posted September 30, 2011 Share Posted September 30, 2011 This doesn't make any sense: for ($i = 0; $i<$filesCount; $i++) { if($filesCount[$i] != null) { Is $filesCount an array, or is it not an array? Quote Link to comment https://forums.phpfreaks.com/topic/248146-check-if-array-element-is-emptynull/#findComment-1274249 Share on other sites More sharing options...
droidus Posted October 1, 2011 Author Share Posted October 1, 2011 yes, it is. Quote Link to comment https://forums.phpfreaks.com/topic/248146-check-if-array-element-is-emptynull/#findComment-1274585 Share on other sites More sharing options...
Pikachu2000 Posted October 1, 2011 Share Posted October 1, 2011 Then what is this: $i < $filesCount supposed to do? If you want to know the number of elements in the array, you need to find number that with count. You can't directly compare an integer to an array. In fact, I'm pretty sure you should be getting an infinite loop with that code because $i will always evaluate as less than $filesCount. Quote Link to comment https://forums.phpfreaks.com/topic/248146-check-if-array-element-is-emptynull/#findComment-1274591 Share on other sites More sharing options...
droidus Posted October 1, 2011 Author Share Posted October 1, 2011 but i have this defined: $filesCount = count($file); Quote Link to comment https://forums.phpfreaks.com/topic/248146-check-if-array-element-is-emptynull/#findComment-1274670 Share on other sites More sharing options...
trq Posted October 1, 2011 Share Posted October 1, 2011 Then $filesCount is no longer an array is it. Quote Link to comment https://forums.phpfreaks.com/topic/248146-check-if-array-element-is-emptynull/#findComment-1274672 Share on other sites More sharing options...
droidus Posted October 2, 2011 Author Share Posted October 2, 2011 ok; anything else that sticks out as to why it wouldn't work? Quote Link to comment https://forums.phpfreaks.com/topic/248146-check-if-array-element-is-emptynull/#findComment-1274838 Share on other sites More sharing options...
jcbones Posted October 2, 2011 Share Posted October 2, 2011 So that would mean that: if($filesCount[$i] != null) { Should be: if($files[$i] != null) { Right? Quote Link to comment https://forums.phpfreaks.com/topic/248146-check-if-array-element-is-emptynull/#findComment-1274848 Share on other sites More sharing options...
mikesta707 Posted October 2, 2011 Share Posted October 2, 2011 No. The problem is you are overwriting your array with the value of its size. As far as PHP is concerned, you no longer have an array, since you changed the reference to it to another piece of data. Its kind of like the size of the array (say, 5) killed the array and stole his identity... What you want to do is change the name of the variable you use to store the size of the array, or use the size of the array directly in the for loop. for example //assuming $filesCount is your array $count = count($filesCount); //now we still have $filesCount for ($i = 0; $i<$count; $i++) { if($filesCount[$i] != null) { ... or //dont change $filesCount for ($i = 0; $i<count($filesCount); $i++) { if($filesCount[$i] != null) { Quote Link to comment https://forums.phpfreaks.com/topic/248146-check-if-array-element-is-emptynull/#findComment-1274856 Share on other sites More sharing options...
Buddski Posted October 2, 2011 Share Posted October 2, 2011 but i have this defined: $filesCount = count($file); Says to me that the array of files is $file (judging by the code posted at least), which means the file should be called using, like jcbones said, $file[$i] It seems the OP is confused about which is the array and which isnt Quote Link to comment https://forums.phpfreaks.com/topic/248146-check-if-array-element-is-emptynull/#findComment-1274877 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.