Jump to content

Recommended Posts

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());
}
?>

Link to comment
https://forums.phpfreaks.com/topic/271744-upload-multiple-images/
Share on other sites

<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']."'";

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>

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'
)
)

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'
    )
)

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'
		    )
)

<?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.

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;
}

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.