Jump to content

Image upload problem


Chappers

Recommended Posts

Hi everyone,

 

I've made myself a multiple image upload form, and everything was going fine until I realised that the check to ensure an image had been selected before continuing was resulting in any unused upload fields giving the error message of 'no file selected'. So, thought I'd check the array for being empty for that check, then pass it to the while loop, so only one instance of 'no file selected' would come up if the array was empty. Here's the original part of the code:

<?php
if(!isset($_POST['upload'])) {
echo<<<BOX

<form name="upload" enctype="multipart/form-data" method="POST" action="{$_SERVER['PHP_SELF']}">
<table class="submitform" align='center' width="70%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td><span class="field-names">*Image file:</span></td>
<td><input class="fields" type="file" name="file[]" value=""></td>
</tr>
<tr>
<td><span class="field-names">*Image file:</span></td>
<td><input class="fields" type="file" name="file[]" value=""></td>
</tr>
<tr>
<td colspan='2' align='center'><br><input class="button" type="submit" name="upload" value="Upload"></td>
</tr>
</table>
</form>
BOX;
}

else {

while (list($key,$value) = each($_FILES['file']['name'])) {

$filetemp = $_FILES['file']['tmp_name'][$key];
$filetype = $_FILES['file']['type'][$key];
$filesize = $_FILES['file']['size'][$key];

if ($filetemp == 'none' || $filetemp =='') {
echo "No file selected.<p>";
}}}

?>

So I added an if statement as a check to see if my idea of checking the array before the while loop instead was going to work:

<?php
if(!isset($_POST['upload'])) {
echo<<<BOX

<form name="upload" enctype="multipart/form-data" method="POST" action="{$_SERVER['PHP_SELF']}">
<table class="submitform" align='center' width="70%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td><span class="field-names">*Image file:</span></td>
<td><input class="fields" type="file" name="file[]" value=""></td>
</tr>
<tr>
<td><span class="field-names">*Image file:</span></td>
<td><input class="fields" type="file" name="file[]" value=""></td>
</tr>
<tr>
<td colspan='2' align='center'><br><input class="button" type="submit" name="upload" value="Upload"></td>
</tr>
</table>
</form>
BOX;
}

else {

if (empty($_FILES['file']['tmp_name'])) {
echo "EMPTY!<p>";
}

else {
echo "NOT EMPTY!<p>";
}

while (list($key,$value) = each($_FILES['file']['name'])) {

$filetemp = $_FILES['file']['tmp_name'][$key];
$filetype = $_FILES['file']['type'][$key];
$filesize = $_FILES['file']['size'][$key];

if ($filetemp == 'none' || $filetemp =='') {
echo "No file selected.<p>";
}}}

?>

And it doesn't work. Claims it's not empty even though I select no files in either upload box before submitting the form.

 

Can anyone advise please?

 

Thanks, James.

Link to comment
Share on other sites

Thanks for that, I've read the page and will implement the default errors that come with uploading files. Out of interest, do you know what was going wrong when I was checking to see if the variable was empty, yet it was saying it wasn't empty despite not having chosen a file in any of the upload fields?

Link to comment
Share on other sites

empty($_FILES['file']['tmp_name']

 

That is checking if anything is assigned to that specific index, that index will have details of every file you've attempted to upload. So if you've tried to upload 1 file, leaving 9 inputs empty (in a ten file input example) it would still come back true.

 

Also if that's your whole script you don't actually do anything, So I cant tell you what goes wrong with the missing code :)

Link to comment
Share on other sites

Hi, that isn't the whole script, just the part I was struggling with. I since found out myself that every field, used or not, gets assigned a key, and of course the error code 4 for every empty field. It was that error code that allowed me to overcome the problem. Here's the entirety of what I've come up with (took me ages, I'm still learning...), although I haven't included the code for other checks and actual copying of files to destination folder as I need to integrate this code to my original upload form. Any criticism welcome to help educate me, but please keep it light as I am learning and may have overcomplicated things. I've made the form so that the user can add upload fields up to a preset maximum, and the remaining number of fields that can be added before maximum reached is reflected in the choice from the dropdown select list. I wanted to ascertain which fields had actually been used for an image so that each empty field didn't come up with 'no file selected'; I only want that to come up if all fields are left empty. Seems to be working fine. Look forward to thoughts on it!

 

<?php

// Set these two variables to preference
$image_fields = 1;					// Number of fields to show initially
$total_fields = 10;					// Total number of fields allowed

if (isset($_POST['addfields'])) {			// If ADD button has been clicked
$current_fields = $_POST['current_fields'];		// Fields already displayed at time of adding
$add_fields = $_POST['add_fields'];			// Number of fields selected to add
$img_fields = $current_fields + $add_fields;		// New number of fields to display
}
else {
$img_fields = $image_fields;				// Show predefined number of fields at page load
}

if(!isset($_POST['upload'])) {				// If UPLOAD button has not been clicked
echo <<<JQB
<form name="upload" enctype="multipart/form-data" method="POST" action="{$_SERVER['PHP_SELF']}">
<table class="submitform" align='center' width="30%" cellspacing="0" cellpadding="0" border="1">
JQB;
$x = 0;
while ($x < $img_fields) {				// Repeat until img_fields value has been reached
echo <<<JQB
<tr>
<td><span class="field-names">Image file:</span></td>
<td><input class="fields" type="file" name="file[]" value=""></td>
</tr>
JQB;
++$x;							// Add 1 to previous value of $x each loop
}							// End of while loop
echo <<<JQB
<tr>
<td><input type="hidden" name="num_fields" value="{$img_fields}"></td> <!-- Number of fields for NO FILE check later-->
</tr>
<tr>
<td colspan='2' align='center'><input class="button" type="submit" name="upload" value="Upload"></td>
</tr>
</table>
</form>

<form name="addfields" enctype="multipart/form-data" method="POST" action="{$_SERVER['PHP_SELF']}">
<table class="submitform" align='center' width="30%" cellspacing="0" cellpadding="0" border="1">
<tr>
JQB;
if ($img_fields < $total_fields) {			// If displayed fields are less than total permitted
echo <<<JQB
<td>Add 
<select name='add_fields' size='1'>
JQB;
$xx = 0;
$current_fields = $img_fields;				// Copy displayed fields before while loop changes its value
while ($img_fields < $total_fields) {			// Repeat while displayed fields is less than total limit
++$xx;							// Add 1 to previous value of $xx each loop
echo <<<JQB
<option value=$xx>$xx</option>				// Use value of $xx at each loop for each option value shown
JQB;
++$img_fields;						// Add 1 to number of fields each loop so limit can be reached
}
echo <<<JQB
</select>
more upload fields</td>
</tr>
<tr>
<td><input type="hidden" name="current_fields" value="{$current_fields}"></td> <!-- Fields now going to be displayed-->
</tr>
<tr>
<td align='center'><br><input class="button" type="submit" name="addfields" value="Add"></td>
JQB;
}
else {								// If total permitted fields already displayed then
echo "<td>No more fields can be added</td>";			// display this instead of options
}
echo <<<JQB
</tr>
</table>
</form>
JQB;
}

else {								// If UPLOAD form has been submitted
$num_fields = $_POST['num_fields'];

while (list($key,$value) = each($_FILES['file']['name'])) {	// Assign a key to the name of each uploaded file
$fileerror = $_FILES['file']['error'][$key];			// Variable containing error numbers
if ($fileerror == 4) {				// Do this if error 4 (no file) given
$nofile[] = 'no file';				// Add the error to the variable array
}
}

$no_images = count($nofile);			// Count number of variables in array and put result in this variable

if ($no_images == $num_fields) {		// Do this if number of fields submitted with NO FILE error is equal to
echo "No file selected";			// total number of fields submitted (meaning all were empty)
}
else {							// If NO FILE fields submitted was less than total fields
$fields_used = $num_fields - $no_images;		// submitted then at least one field had a file selected
}

$y = 0;						// Keys start from zero ($y), but count must start from minimum number
$yy = 1;					// of fields used, which is one ($yy). 

while ($yy <= $fields_used) {			// Loop code until number $yy reaches number of fields used
$filename = $_FILES['file']['name'][$y];			// Variable for original name of file uploaded
$filetemp = $_FILES['file']['tmp_name'][$y];			// Variable for temporary file name given by server
$filetype = $_FILES['file']['type'][$y];			// Variable for type of file uploaded
$filesize = $_FILES['file']['size'][$y];			// Variable for size in bytes of file uploaded
$fileerror = $_FILES['file']['error'][$y];			// Variable for error codes assigned to upload

++$y;						// Add 1 to number each time loop runs for key number
++$yy;						// Add 1 to number each time loop runs for count towards fields used

// From here on, code to perform additional checks and then copy image to your destination folder can be put.

echo "Filename: $filename<br>";
echo "Temp Filename: $filetemp<br>";
echo "Filetype: $filetype<br>";
echo "Filesize: $filesize<br>";
echo "File Error: $fileerror<br><br>";
}
}

?>

 

Thanks for everyone's help, without it I wouldn't have got this far even.

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.