Forum Moderators: coopster

Message Too Old, No Replies

Multiple Image Upload Script

         

xboxingman

10:02 pm on Jan 21, 2012 (gmt 0)

10+ Year Member



Well I have been trying to get this to work for a few days now. Since then I have pulled out too much hair, so I am here now for some guru advice which is much appreciated.

This is just a portion of the script, the part I cannot for the life of me get to work. I have tried several different version of it and no luck across the board.

The problem begins when I try to get the extension, that just doesn't work I guess and says unknown extension. I tried to print it and it seems empty.

Image Upload Pat:
if((isset($_GET['snd'])) && (isset($_POST['img_upl_send']))) {
$amt2upl = $_GET['amt'];
$xu = 1;
$imgerrors=0;
$mainimgnum = $_POST['main_img'];
$sn_imgup = $_SESSION['stagename'];
define('GW_UPLOADPATH', '../'.$sn_imgup.'/images/');
define('GW_MAXFILESIZE', 432768);
$uploadArray= array();
while($xu <= $amt2upl) {
$uploadArray[] = $_POST['image'.$xu];
$xu++;
}
foreach($uploadArray as $file) {
$memimgname = $_FILES["$file"]['name'];
$extension = substr(strrchr($memimgname, "."), 1);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
echo "<div class=\"titlebar\">Unknown File Extension!";
echo $extension;
echo "<br> <a href='javascript:history.back(1);'>&lt;&lt; Back</a></div>";
echo "</div></div><div id=\"footer\">";
include("includes/footer.php");
exit;
$imgerrors=1;
} else {
$memimgsize=filesize($_FILES["$file"]['tmp_name']);
if ($memimgsize > GW_MAXFILESIZE){
echo "<div class=\"titlebar\">You have exceeded the size limit!";
echo "<br> <a href='javascript:history.back(1);'>&lt;&lt; Back</a></div>";
echo "</div></div><div id=\"footer\">";
include("includes/footer.php");
exit;
$imgerrors=1;
} else {
$cld = generateCode($length=3);
$image_name=$cld.'image.'.$extension;
$newname=GW_UPLOADPATH.$image_name;
$copied = move_uploaded_file($_FILES["$file"]['tmp_name'], $newname);
if (!$copied) {
echo "<div class=\"titlebar\">Image Upload Error!";
echo "<br> <a href='javascript:history.back(1);'>&lt;&lt; Back</a></div>";
echo "</div></div><div id=\"footer\">";
include("includes/footer.php");
exit;
$imgerrors=1;
} else {
echo "Image OK"; //will be inserting into database here
}
}
}
}
}


Form:
if((isset($_GET['sel'])) && (isset($_POST['img_cnt']))) {
$all_amt = $_POST['amt_des'];
$xu = 1;
echo "<div class=\"titlebar\"><h2>Images to upload:</h2>";
echo "<form action=\"ood_images.php?n=y&snd=y&amt=".$all_amt."\" method=\"post\" name=\"imgs_upl\"><br />";
echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
echo "<tr class=\"top\"><td>Picture #</td><td>Select Image</td><td>Main Image?</td></tr>";
echo "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
while($xu <= $all_amt) {
echo "<tr><td>Picture ".$xu.":</td><td><input type=\"file\" name=\"image".$xu."\" /></td><td><input type=\"radio\" name=\"main_img\" value=\"".$xu."\"/></td></tr>";
$xu++;
}
echo "<tr><td>&nbsp;</td><td>Main Picture Already Uploaded:</td><td><input type=\"radio\" name=\"main_img\" value=\"none\"/></td></tr>";
echo "<tr><td>&nbsp;</td><td>&nbsp;</td><td><input type=\"submit\" name=\"img_upl_send\" value=\"Upload Pictures\" /></td></tr>";
echo "</table></form></div>";
}


I could really use some help thanks.
Mike

rainborick

11:26 pm on Jan 21, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try:

$extension = strtolower(substr($memimgname, strrchr($memimgname, ".") + 1));

Overall , best practice would likely be to use a function like exif_imagetype() or getimagesize() to determine the file type, rather than rely on the file name extension. Users often rename files without regard to the extension, let alone upper/lower case.

And your HTML needs some changes to support file uploading:

echo "<form enctype=\"multipart/form-data\" action=\"ood_images.php?n=y&snd=y&amt=".$all_amt."\" method=\"post\" name=\"imgs_upl\"><br />";
echo "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"432768\">\n";

Good luck!

xboxingman

1:40 am on Jan 22, 2012 (gmt 0)

10+ Year Member



Thanks for the input but that didn't make any difference, still does the same thing.

rainborick

4:56 am on Jan 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's hard to make any further suggestions with just the code fragments you posted. I'd suggest inserting some code to display critical variables as your program runs so that you can get a clear picture of why it's failing.

Check is_uploaded_file() to see if the user file was sent to the server. Do print_r($_POST) to see what's there. That should help a lot.

<rant>
It may just be me and forgive the nitpicking, but relying on $_GET when a page is requested with POST seems like a potential problem waiting down the road. I know lots of folks do this, but if your <form> tag specifies method="POST" then it seems to me that it's best to simply put all of the parameters in <input> tags and access them rather than add a query string to the URL. Should PHP someday stop automatically filling $_GET regardless of the HTTP request method, you'll have to rewrite such code.
</rant>

Post the results and I'll try to help more.

xboxingman

7:07 pm on Jan 23, 2012 (gmt 0)

10+ Year Member



I changed a few things up to see what was going on and it seems that the file inputs are going through empty.

Here is what I added to get an idea of what was going on:

$uploadArray = array();
while($xu <= $amt2upl) {
$imgnum = "image".$xu;
$uploadArray[] = $_POST[$imgnum];
$xu++;
}
echo "<div class=\"titlebar\">Errors and output:<br />";
print_r($_POST);
echo "<br />";
print_r($uploadArray);
echo "<br />";
if(is_uploaded_file($_POST['image1'])) { // just choose the first one to bypass the array
echo "Yes it IS";
} else {
echo "No it's NOT";
}
echo "</div>";


Here is what the result of that ended up being:

Errors and output:
Array ( [MAX_FILE_SIZE] => 432768 [img_upl_send] => Upload Pictures )
Array ( [0] => [1] => [2] => )
No it's NOT


Also then the first error on the foreach() statement comes up (unknown file extension) but I will assume that is because the inputs are empty.

Thanks a lot for the help, this is really driving me crazy.

Mike

---
The only reason I am using the _GETS is because it's a multi page script and not every thing is done though a form a lot of it is links. I understand where you are coming from and its a valid point, but I couldn't imagine PHP ever doing away with it as widely used as it is. I personally am not a big fan of them as a user can try to manually enter things in the url.

xboxingman

8:43 pm on Jan 23, 2012 (gmt 0)

10+ Year Member



I also added error_reporting(E_ALL); to the top of the page and when I do that it says "Notice: Undefined index: image1" , image2 , etc.

Why would the file inputs be posting empty?
Mike

rainborick

1:52 am on Jan 24, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It sounds like there are no corresponding <input>s for those values. The results of print_r($_POST) should show what's being sent by the form. You should see entries for image1, image2, etc. If not, then look at your form.

xboxingman

2:25 am on Jan 24, 2012 (gmt 0)

10+ Year Member



I have looked over the form, several times. I have googled this issue all day long, I have other image upload scripts (one single image) that work flawlessly on the same server. I have looked over the php.ini or phpinfo() and nothing seems to stand out as a red flag. I have checked the tmp folder. I just can't seem to grasp with the inputs are not being submitted.

Here is the form when printed (page source).


<form enctype='multipart/form-data' action="ood_images.php?n=y&snd=y&amt=5" method="post" name="imgs_upl"><br /><input type="hidden" name="MAX_FILE_SIZE" value="432768">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr class="top"><td>Picture #</td><td>Select Image</td><td>Main Image?</td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>Picture 1:</td><td><input type="file" name="image1" /></td><td><input type="radio" name="main_img" value="1"/></td></tr>
<tr><td>Picture 2:</td><td><input type="file" name="image2" /></td><td><input type="radio" name="main_img" value="2"/></td></tr>
<tr><td>Picture 3:</td><td><input type="file" name="image3" /></td><td><input type="radio" name="main_img" value="3"/></td></tr>
<tr><td>&nbsp;</td><td>Main Picture Already Uploaded:</td><td><input type="radio" name="main_img" value="none"/></td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td><input type="submit" name="img_upl_send" value="Upload Pictures" /></td></tr></table></form>


I tried changing the enctype to single quotes because I read someone had a issue due to the double quotes. The form is outside of the table so there should be no problem there. The "main_img" radio submits fine. I just can't understand why it isn't working as all the code looks just fine to me.

This is really trouble for me. I would like the user to be able to upload up to 10 pictures at once without using javascript or flash. I worry I am going to have to make it so they have to upload one at a time and hit next for each one.
thanks a lot.
Mike

rainborick

6:22 am on Jan 24, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OK, so what did print_r($_POST) show?

xboxingman

6:28 am on Jan 24, 2012 (gmt 0)

10+ Year Member



It printed empty, I didn't change anything I was just showing that all my form inputs are proper. That is I was asking if you had any other suggestions why it was posting empty. I even tried erasing all the array values and doing it as a single input with the name "image" and it still posted as an empty value. All the code looks right to me so I cannot for the life of me understand why its not working.

rocknbil

4:28 pm on Jan 24, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Stupid Q but have you pulled phpinfo() on the site and verified file uploads are allowed?

rainborick

6:07 pm on Jan 24, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, if print_r shows no data then there's some reason that the form data isn't reaching your script. You might try pointing the <form> to a form dumper script just to be sure that the form isn't sending data, but it's unlikely. It sounds like a more fundamental problem with the HTML mark-up for the form itself.

xboxingman

7:21 pm on Jan 24, 2012 (gmt 0)

10+ Year Member



I tried removing all the gets and printing the form out in HTML instead of PHP and it still doesn't work..
This is a separate image script I have on the same server and works just fine.


<?php
define('GW_MAXFILESIZE', 432768);
define('GW_UPLOADPATH', 'test/images/');

if(isset($_POST['submit'])) {
$image = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];

if(!empty($image)){
if((($image_type == 'image/gif') || ($image_type == 'image/jpeg') ||
($image_type == 'image/pjpeg') || ($image_type == 'image/png')) &&
($image_size >0) && ($image_size <= GW_MAXFILESIZE)) {
if($_FILES['image']['error'] == 0) {
//Move the file to the target upload folder
$target= GW_UPLOADPATH . $image;
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)){

//Confirm success with the user
echo '<p>The following news was added successfully:</p>';
echo '<img src="'.GW_UPLOADPATH . $image . '"alt="New Image" /></p>';

}else{
echo 'Sorry, there was a problem uploading your image.';
}
}
} else{
echo 'The image must be a GIF, JPEG, or PNG image no ' . 'greater than ' . (GW_MAXFILESIZE / 1024) . ' KB in size.';
}
// Try to delete the temporary image file
@unlink($_FILES['image']['tmp_name']);
} else{
echo '<p>Add Your Image</p>';
}
}
?>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="image">Upload Image:</label>
<input type="file" id="image" name="image" />
<input type="submit" value="Post" name="submit" />

xboxingman

7:23 pm on Jan 24, 2012 (gmt 0)

10+ Year Member



okay I just added print_r($_FILES) and all the images show up, so it is being posted by the form.

xboxingman

9:21 pm on Jan 24, 2012 (gmt 0)

10+ Year Member



Well I ended up combing the two scripts and getting it to work. Here is the final version:

define('GW_MAXFILESIZE', 768000);
if((isset($_GET['n'])) || (isset($_POST['new']))) {
if(isset($_POST['img_upl_send'])) {
$amt2upl = $_POST['amt'];
$xu = 1;
$mainimgnum = $_POST['main_img'];
$sn_imgup = $_SESSION['stagename'];
define('GW_UPLOADPATH', $sn_imgup.'/images/');
while($xu <= $amt2upl) {

$image_asso = "image".$xu;
$image = $_FILES[$image_asso]['name'];
$image_tmp = $_FILES[$image_asso]['tmp_name'];
$image_type = $_FILES[$image_asso]['type'];
$image_size = $_FILES[$image_asso]['size'];

if(!empty($image)){
if((($image_type == 'image/gif') || ($image_type == 'image/jpeg') ||
($image_type == 'image/pjpeg') || ($image_type == 'image/png')) &&
($image_size >0) && ($image_size <= GW_MAXFILESIZE)) {
if($_FILES['image']['error'] == 0) {
//Move the file to the target upload folder
$extension = strtolower(substr($image, strrchr($image, ".") + 1));
$cld = generateCode($length=3);
$image2 = $cld.'image'.$extension;
$target= GW_UPLOADPATH . $image2;
if (move_uploaded_file($image_tmp, $target)){

//Confirm success with the user
$Message = "Successfully added ".$amt2upl." images";
// DB INSERTION
}else{
$Message = "Sorry, there was a problem uploading your image.";
exit;
}
}
} else{
$Message = "The image must be a GIF, JPEG, or PNG image no " . "greater than " . (GW_MAXFILESIZE / 1024) . " KB in size.";
exit;
}
// Try to delete the temporary image file
@unlink($img_tmp);
}
$xu++;
}
echo "<div class=\"titlebar\"><strong>".$Message."</strong></div>";
echo "</div></div><div id=\"footer\">";
include("includes/footer.php");
exit;
}


The extension thing doesn't work, it just removes the first letter of the image name, which doesn't much matter as it will still be a unique image name.

thanks