Jump to content

check if array element is empty/null


droidus

Recommended Posts

	// 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!"; }
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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) {

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.