Jump to content


Photo

Upload Multiple Images


  • Please log in to reply
8 replies to this topic

#1 justlukeyou

justlukeyou

    Advanced Member

  • Members
  • PipPipPip
  • 966 posts

Posted 08 December 2012 - 06:11 AM

Hi,

I have some code which uploads one image into one row. I am now trying to adapt it so that it uploads multiple images.

However it currently uploads one image into the same rows "image, imageone". Although Im also confused how to upload multiple images and move onto the next empty row. Any suggestions please?


<div class="imageuploadarea">
<div class="imageuploadinfo">
Select a JPG, GIF, PNG or TIF File:
  </div>

<?php // upload.php
echo <<<_END
<div class="user-area">
<form method='post' action='upload.php' enctype='multipart/form-data'>
<input type='file' name='filename' class="browse"   />
<input type="submit" value="Upload" class="submit-button" />
  </div>
</form>
<div class="user-area">
<form method='post' action='upload.php' enctype='multipart/form-data'>
<input type='file' name='filename' class="browse"   />
<input type="submit" value="Upload" class="submit-button" />
  </div>
</form>

  </div>
_END;

if ($_FILES)
{
$name = $_FILES['filename']['name'];

switch($_FILES['filename']['type'])
{
case 'image/jpeg': $ext = 'jpg'; break;
case 'image/gif': $ext = 'gif'; break;
case 'image/png': $ext = 'png'; break;
case 'image/tiff': $ext = 'tif'; break;
default:   $ext = ''; break;
}

if ($ext)
{
$n = uniqid().".$ext";
move_uploaded_file($_FILES['filename']['tmp_name'], "images/".$n);
}
else echo "The image must be a jpeg, gif, png or tiff file format.";
}
else echo "No image has been uploaded";

$sql = "UPDATE users SET logo = '". mysql_real_escape_string($n) ."' WHERE id='".$_SESSION['userID']."'";
$sql1 = "UPDATE users SET logoone = '". mysql_real_escape_string($n) ."' WHERE id='".$_SESSION['userID']."'";
if ($n <> "") {
$result = mysql_query($sql) or die("MySQL Error" . mysql_error());
$result = mysql_query($sql1) or die("MySQL Error" . mysql_error());
}
?>


#2 DavidAM

DavidAM

    Advanced Member

  • Gurus
  • 1,974 posts
  • LocationSpring, TX USA

Posted 08 December 2012 - 09:41 AM

<form method='post' action='upload.php' enctype='multipart/form-data'>
<input type='file' name='filename' class="browse"   />
<input type="submit" value="Upload" class="submit-button" />
  </div>
</form>
<div class="user-area">
<form method='post' action='upload.php' enctype='multipart/form-data'>
<input type='file' name='filename' class="browse"   />
<input type="submit" value="Upload" class="submit-button" />
  </div>
</form>


You have TWO separate forms there. When a submit button is clicked, only the form containing THAT button is submitted and only the fields IN THAT FORM are submitted.

To upload multiple files, you have to have multiple FILE-type fields in the form with different names (or as an array). See the manual Multiple File Uploads

<form method='post' action='upload.php' enctype='multipart/form-data'>
<input type='file' name='filename1' class="browse"   />
<input type='file' name='filename2' class="browse"   />
<input type="submit" value="Upload" class="submit-button" />
  </div>
</form>

On a side note:

$sql = "UPDATE users SET logo = '". mysql_real_escape_string($n) ."' WHERE id='".$_SESSION['userID']."'";
$sql1 = "UPDATE users SET logoone = '". mysql_real_escape_string($n) ."' WHERE id='".$_SESSION['userID']."'";

I don't see any reason for this to be done in TWO separate queries:
$sql = "UPDATE users SET logo = '". mysql_real_escape_string($n) ."', " .
       " logoone = '" . mysql_real_escape_string($n) ."' " .
       "WHERE id='".$_SESSION['userID']."'";

-- I haven't lost my mind, it's backed up on tape ... somewhere!

#3 justlukeyou

justlukeyou

    Advanced Member

  • Members
  • PipPipPip
  • 966 posts

Posted 08 December 2012 - 11:30 AM

Thanks.

I changed the update code and this works fine. However I have tried to change the form fields from the PHP site but it doesn't appear to work.. When I try userfile[] this just provides a box. There is no method of browsing for the file. The PHP site looks simple. Does it need anything adding to it?


<?php // upload.php
echo <<<_END
<div class="user-area">
<form method='post' action='upload.php' enctype='multipart/form-data'>
<input type='userfile[]' name='filename1' class="browse"   />
<input type='file' name='filename2' class="browse"   />
<input type="submit" value="Upload" class="submit-button" />
  </div>
</form>

Should it be used like this...

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  Send these files:<br />
  <input name="userfile[]" type="file" /><br />
  <input name="userfile[]" type="file" /><br />
  <input type="submit" value="Send files" />
</form>


#4 scootstah

scootstah

    Advanced Member

  • Gurus
  • 2,889 posts
  • LocationUSA

Posted 08 December 2012 - 11:38 AM

Should it be used like this...

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  Send these files:<br />
  <input name="userfile[]" type="file" /><br />
  <input name="userfile[]" type="file" /><br />
  <input type="submit" value="Send files" />
</form>


Yes, that's right. This will turn the input into an array. It works a little bit funky for $_FILES, in that each index will be an array, rather than an array of indexes. So it will look something like:
$_FILES['userfile'] = array(
	'name' => array(
		'name1.jpg',
		'name2.jpg'
	)
)

while(!$succeed = try());

#5 justlukeyou

justlukeyou

    Advanced Member

  • Members
  • PipPipPip
  • 966 posts

Posted 08 December 2012 - 12:41 PM

Hi,

When I use this the userfile[] it does not allow me to browse for any file. It just allows me to click in the box.

<input type='userfile[]' name='filename1' class="browse"   />
<input type='userfile[]' name='filename2' class="browse"   />

If I use this does it apply each image to a row? When I use this it creates an error.

$_FILES['userfile'] = array(
	    'name' => array(
			    'name1.jpg',
			    'name2.jpg'
	    )
)


#6 Pikachu2000

Pikachu2000

    I hate everything.

  • Staff Alumni
  • 11,378 posts
  • LocationFuture Independent Republic of Texas
  • Age:106

Posted 08 December 2012 - 01:13 PM

You aren't paying attention to syntax. Look at the differences in the html tag attributes.
"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *

Random Quote: "

#7 justlukeyou

justlukeyou

    Advanced Member

  • Members
  • PipPipPip
  • 966 posts

Posted 08 December 2012 - 03:28 PM

Could you add a bit more detail. Im not sure how the browse function doesn't work.

I dont understand this part also. Will this insert these two image name into the folder?

'name1.jpg',
'name2.jpg'

echo <<<_END
<div class="user-area">
<form method='post' action='upload.php' enctype='multipart/form-data'>
<input type='userfile[]' name='filename1' class='browse'   />
<input type='userfile[]' name='filename2' class='browse'   />
<input type="submit" value="Upload" class="submit-button" />
  </div>
</form>
  </div>
_END;

if ($_FILES)
{
$name = $_FILES['filename']['name'];

switch($_FILES['filename']['type'])
{
case 'image/jpeg': $ext = 'jpg'; break;
case 'image/gif': $ext = 'gif'; break;
case 'image/png': $ext = 'png'; break;
case 'image/tiff': $ext = 'tif'; break;
default:   $ext = ''; break;
}

$_FILES['userfile'] = array(
			    'name' => array(
							    'name1.jpg',
							    'name2.jpg'
			    )
)


#8 DavidAM

DavidAM

    Advanced Member

  • Gurus
  • 1,974 posts
  • LocationSpring, TX USA

Posted 08 December 2012 - 05:05 PM

<?php // upload.php
echo <<<_END
<div class="user-area">
<form method='post' action='upload.php' enctype='multipart/form-data'>
<input type='userfile[]' name='filename1' class="browse"   />
<input type='file' name='filename2' class="browse"   />
<input type="submit" value="Upload" class="submit-button" />
  </div>
</form>


Look closely at that. There is no "userfile[]" type for an INPUT tag. You need to have multiple "INPUT type=file" tags, all with the same name; a name that ends with the square-brackets. This will produce an array in the $_FILES array with (essentially) an entry for each file that is uploaded. You will have to walk through that array and process each file.

I really don't see how this is going to work. You are UPDATEing the users record for the current user. So no matter how many files you allow them to upload ONLY THE LAST ONE will be in the database when all is said and done.
-- I haven't lost my mind, it's backed up on tape ... somewhere!

#9 justlukeyou

justlukeyou

    Advanced Member

  • Members
  • PipPipPip
  • 966 posts

Posted 08 December 2012 - 06:00 PM

Thanks, thats clearer. I now have it like the piece of code of below. However when I insert this:

$_FILES['userfile'] = array(
							    'name' => array(
															    'name1.jpg',
															    'name2.jpg'
							    )
)

It creates the following error: Parse error: syntax error, unexpected T_IF in /home/ukhomefu/eventvital.com/test/upload.php on line 131

When I remove the code above it does not allow me to browse for files.

This is the code I have so far:


<?php // upload.php
echo <<<_END
<div class="user-area">
<form method='post' action='upload.php' enctype='multipart/form-data'>
<input type='userfile[]' name='image1' class="browse"   />
<input type='userfile[]' name='image2' class="browse"   />
<input type="submit" value="Upload" class="submit-button" />
  </div>
</form>
  </div>
_END;

$_FILES['userfile'] = array(
							    'name' => array(
															    'name1.jpg',
															    'name2.jpg'
							    )
)

if ($_FILES)
{
$name = $_FILES['filename']['name'];

switch($_FILES['filename']['type'])
{
case 'image/jpeg': $ext = 'jpg'; break;
case 'image/gif': $ext = 'gif'; break;
case 'image/png': $ext = 'png'; break;
case 'image/tiff': $ext = 'tif'; break;
default:   $ext = ''; break;
}

if ($ext)
{
$n = uniqid().".$ext";
move_uploaded_file($_FILES['filename']['tmp_name'], "images/".$n);
}
else echo "The image must be a jpeg, gif, png or tiff file format.";
}
else echo "No image has been uploaded";

$sql = "UPDATE users SET logo = '". mysql_real_escape_string($n) ."', " .
		   " logoone = '" . mysql_real_escape_string($n) ."' " .
		   "WHERE id='".$_SESSION['userID']."'";
if ($n <> "") {
$result = mysql_query($sql) or die("MySQL Error" . mysql_error());
$result = mysql_query($sql1) or die("MySQL Error" . mysql_error());
}
?>

This is the part that creates the error.

[b]if ($_FILES)[/b]
{
$name = $_FILES['filename']['name'];

switch($_FILES['filename']['type'])
{
case 'image/jpeg': $ext = 'jpg'; break;
case 'image/gif': $ext = 'gif'; break;
case 'image/png': $ext = 'png'; break;
case 'image/tiff': $ext = 'tif'; break;
default:   $ext = ''; break;
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com