Forum Moderators: coopster

Message Too Old, No Replies

Uploading array of files problem

         

Marked

7:56 am on Sep 24, 2009 (gmt 0)

10+ Year Member



Hey guys, I'm having a little trouble with a form and uploading an array of files.

With my script, there are 6 fields to upload screenshots. When you upload screenshots, a directory is created, and those image files are moved to that directory.

What I am having trouble with is coming up with an if statement that defines if there are any screenshots uploaded or not. This is because screenshots are optional, and if none are selected then a directory is created anyway and that causes issues on the display side(not my code).

Here is the relevant part of the code:

From the form:

<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />

The upload code:

mkdir($screenshotsdir.$title, 0777);
$screenshots_directory = $screenshotsdir.$title.'/';

foreach ($_FILES["pictures"]["error"] as $key => $error)
{
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
if (!$tmp_name) continue;

$name = basename($_FILES["pictures"]["name"][$key]);

if ($error == UPLOAD_ERR_OK)
{
if ( move_uploaded_file($tmp_name,$screenshots_directory.$name) )
$uploaded_array[] .= "Uploaded file '".$name."'.<br/>\n";

else
$errormsg .= "Could not move uploaded file '".$tmp_name."' to '".$name."'<br/>\n";
}
else $errormsg .= "Upload error. [".$error."] on file '".$name."'<br/>\n";
}

So, what I'm trying to do is create an if statement which is if there were any screenshots selected for upload, and that will surround the entire 2nd part of the code above.

If it helps, I got the code from php website:
[php.net...]

Thanks in advance for any support.

Pico_Train

10:13 am on Sep 24, 2009 (gmt 0)

10+ Year Member



if(!empty($_POST['pcitures'])){
code...
}

Will that help?

Marked

12:34 pm on Sep 24, 2009 (gmt 0)

10+ Year Member



Thanks for your reply. I did try that previously, and I tried it again just now. I did this:

if(!empty($_POST['pictures'])){
echo 'Pictures present';

CODE HERE

}else{
echo 'No pictures';
}

The result that came up was "No pictures" everytime, with and without images selected in the form. So I guess this means whether or not images are selected in the form, $_POST['pictures']returns something regardless. It's the difference that I need to find and have in the if statement...

I have also tried things like if(isset($_POST['pictures'])) and if($_POST['pictures']['size'] > 0)) but the same result as above comes up every time. if($_FILES['scriptfile']['size'] > 0) works for a single upload, but not when it is an array...

Pico_Train

5:29 am on Sep 25, 2009 (gmt 0)

10+ Year Member



do a print_r($_POST['pictures']); for both selected and none selected and post the results for both.

idfer

5:17 pm on Sep 25, 2009 (gmt 0)

10+ Year Member



Marked, you're on the right track with the original upload code. The data for uploaded files go into the $_FILES global and not $_POST. You could check count($_FILES['pictures']['name']) to see if there are any files uploaded, but then they may all have errors associated with them...

Here's an easy way to create the directory only when needed: call is_dir() just before you want to create a file and only create the directory if it doesn't exist. I've bolded the required changes in your code below:

[b]$screenshots_directory = $screenshotsdir.$title;[/b]

foreach ($_FILES["pictures"]["error"] as $key => $error) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
if (!$tmp_name) continue;

$name = basename($_FILES["pictures"]["name"][$key]);

if ($error == UPLOAD_ERR_OK) {
[b]if(!is_dir($screenshots_directory)) {
mkdir($screenshots_directory, 0777);
}[/b]
if ( move_uploaded_file($tmp_name,$screenshots_directory.[b]'/'.[/b]$name) )
$uploaded_array[] .= "Uploaded file '".$name."'.<br/>\n";
else
$errormsg .= "Could not move uploaded file '".$tmp_name."' to '".$name."'<br/>\n";
} else
$errormsg .= "Upload error. [".$error."] on file '".$name."'<br/>\n";
}

Pico_Train

5:54 am on Sep 26, 2009 (gmt 0)

10+ Year Member



oops...sorry, missed that!

Marked

5:35 am on Sep 27, 2009 (gmt 0)

10+ Year Member



Thank you both very much for you replies,

idfer, your advice worked again(i think you helped me with a previous topic).

I did figure this out previously by browsing through the php documentation. Instead of using is_dir() I used file_exists() which actually checks both directories and files. But I only need to check for a directory, so I will switch to is_dir().

Thanks again.