Forum Moderators: coopster
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.
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...
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";
}
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.