0

I have a feature on my website where users can create an album to organize their photos better. They can upload new photos or they can select from photos they have already uploaded. In my PHP I have code that goes through the files they uploaded, if they uploaded any new ones. It works great, but I only want it to work when they ACTUALLY upload NEW photos. Right now, it runs even if they selected already uploaded, old photos. I am using the HTML5 "multiple" attribute so they can upload multiple photos. In my PHP, how can I write a conditional if statement to only run this code when they upload photos?

foreach ($_FILES['uploads']['name'] as $key => $file) {
   $time= time();
   $target= UPLOADPATH . $time . $file;
   move_uploaded_file($_FILES['uploads']['tmp_name'][$key], $target);
}

I have tried using:

if ($_FILES['uploads']['size'] != 0)
if ($_FILES != 0)
if ($_FILES['upload']['name'] !=0)

But, so far no success. What can I do? What am I doing wrong?

Thanks

UPDATE

I ended up setting a variable to the value of the first element in the $_FILES array.

Like so:

$file_test= $_FILES['uploads']['size']['0'];

Then I used it in the test condion:

($file_test != 0) { 
    // run the code;
}

Thanks everyone for the help!!

6
  • if (! empty($_FILES)) should do the trick. Commented Oct 19, 2012 at 18:51
  • empty() doesn't work on files. The enctype includes it in the post, regardless if it has bytes or not. Commented Oct 19, 2012 at 18:53
  • @AlienWebguy Then what can I do? Commented Oct 19, 2012 at 18:54
  • var_dump($_FILES['uploads']['size']); Commented Oct 19, 2012 at 18:55
  • 1
    @user1759682 just to clarify, do you want to compare sent files to already uploaded files to see if the ones provided are different? Commented Oct 19, 2012 at 19:07

3 Answers 3

2

I guess you store the already uploaded photos anywhere, then you could use a combination of getimagesize() function (PHP Manual) to check whether the uploaded file is an image or not. And then compare the MD5 hash of the uploaded files using the md5_file() function (PHP Manual) with the ones stored. If they are stored in a database it might be a performance increase to directly store the MD5 of each file in a field for itself, so you can easily query the database for existing MD5 hash.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

First your have to rearrange your $_FILES array using this function

function rearrange( $arr ){
        foreach($arr as $key => $items){
            foreach($items as $i => $val){
                $array[$i][$key] = $val;
            }
        }
        return $array;
}

$files = rearrange($_FILES['uploads']);

//Then do the foreach loop upload

if(!empty($files))
{

    foreach ($files as $file) {
     // Check the file size inside the foreach loop
     if($file['size'] > 0)
     {
       $time= time();
       $target= UPLOADPATH . $time . $file['name'];
       move_uploaded_file($file['tmp_name'], $target);
     }
    }

}

1 Comment

The name attribute of your input tag must be uploads[]
0
if(!empty($_FILES)){
    // you've got files?!?!
}

Lucky guess here, don't really understand your question.

1 Comment

@AlienWebguy If no file is uploaded, $_FILES is an empty array. I don't get the question probably...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.