Uploading several pictures with loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Uploading several pictures with loop

Post by Perfidus »

From a form like this one, I'm trying to upload and thumbnail 3 pictures in 1 shot. The form is more or less like follows but the things do not work:

Code: Select all

<input name=&quote;userfileї]&quote; type=&quote;file&quote; /><br /> 
<input name=&quote;userfileї]&quote; type=&quote;file&quote; /><br /> 
<input name=&quote;userfileї]&quote; type=&quote;file&quote; /><br />
And the PHP is this way:

Code: Select all

$totalimages=count($userfile);
if($totalimages!=0){
for($a=0;$a<$totalimages;$a++){
$msg = "";
switch(!strcasecmp($_SERVER['REQUEST_METHOD'], "POST")) {

   case true:

	$tmp = getcwd()."/".$_FILES['userfile[$a]']['name'];

	if(!@move_uploaded_file($_FILES['image[$a]']['tmp_name'], $tmp)) {

$msg = "";
		break;
	}

	$fp = fopen($tmp, "rb");
	$str = fread($fp, filesize($tmp));

	fclose($fp);
	unlink($tmp);

	$im1 = ImageCreateFromString($str);

	$imgname = $ref."_thumb_".$a;
	$maxwidth =250;
	$maxheight = 200;
	$width1 = ImageSX($im1);
	$height1 = ImageSY($im1);
	$width2 = $maxwidth;
	$height2 = floor(($width2 * $height1) / $width1);

	if($maxheight > 0 && $height2 > $maxheight) {

		$height2 = $maxheight;
		$width2 = floor(($height2 * $width1) / $height1);
	}

	$im2 = ImageCreateTrueColor($width2, $height2);
	ImageCopyResampled($im2, $im1, 0, 0, 0, 0, $width2, $height2, $width1, $height1);

	ImageJpeg($im2, "thumb/".$imgname.".jpg");
	$msg = "Ok";
	$im3 = ImageCreateFromString($str);

	$imgname2 = $ref."_full_".$a;
	$maxwidth2 = 500;
	$maxheight2 = 300;

	$width12 = ImageSX($im3);
	$height12 = ImageSY($im3);
	$width22 = $maxwidth2;
	$height22 = floor(($width22 * $height12) / $width12);

	if($maxheight2 > 0 && $height22 > $maxheight2) {

		$height22 = $maxheight2;
		$width22 = floor(($height22 * $width12) / $height12);
	}

	$im4 = ImageCreateTrueColor($width22, $height22);
	ImageCopyResampled($im4, $im3, 0, 0, 0, 0, $width22, $height22, $width12, $height12);

	ImageJpeg($im4, "full/".$imgname2.".jpg");
	ImageDestroy($im1);
	ImageDestroy($im2);
	ImageDestroy($im3);
	ImageDestroy($im4);
	break;
}}
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

so what exactly does not work, at which position?
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

It doesn't upload any picture at all!
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
So what about trying to find out which steps work and which don't!?

First of all try to upload one file.
Then if you know how to upload try to upload several files.
Next and last step is resizing, AFTER upload of multiple files works.


You mix names:
$_FILES['userfile[$a]']['name'];
$_FILES['image[$a]']['tmp_name']

What is this, does this work?
switch(!strcasecmp($_SERVER['REQUEST_METHOD'], "POST")) {
switch(! <--???


IS move_uploaded_file() file TRUE? $msg="not uploaded" instead of ""

djot
-
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

print_r($_FILES) and look what's in ['error']... if it's 1 something went wrong.

By the way...

Code: Select all

$totalimages=count($userfile);
I assume you're not relying on register_globals being on for this? $userfile is defined somewhere above it?
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

$userfile comes from the form:

Code: Select all

<input name=&quote;userfile&#1111;]&quote; type=&quote;file&quote; /><br /> 
<input name=&quote;userfile&#1111;]&quote; type=&quote;file&quote; /><br /> 
<input name=&quote;userfile&#1111;]&quote; type=&quote;file&quote; /><br />
Maybe the array is bad formed?
I will try with print_r($_FILES) to see what's up...
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

I have echoed $totalimages and I get "3", so array seems to work
If I echo $userfile[1] I get: \tmp\phpMVqZAI

This means nothing to me, maybe is the way PHP calls the image when it is in temp??? Sorry, but not used to work with images.
The upload and thumbnail script works fine with only 1, is the loop or the array what really seems to crash it.

Some hints, please :oops: !
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Oh.... I missed something here...

This is wrong...

Code: Select all

$tmp = getcwd()."/".$_FILES['userfile[$a]']['name'];
     if(!@move_uploaded_file($_FILES['image[$a]']['tmp_name'], $tmp)) {
It should be

Code: Select all

$tmp = getcwd()."/".$_FILES['userfile'][$a]['name'];
     if(!@move_uploaded_file($_FILES['userfile'][$a]['tmp_name'], $tmp)) {
By the way... using $userfile directly from <input type="file" name="userfile"> is bad.... It's depending upon register_globals being turned on in php.ini... use count($_FILES['userfile']) instead ;)
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

If I use count($_FILES['userfile']) and I echo it, I get "5" as the value, but I'm just uploading 3 files. The fact is that I'm using the form to upload also user's data.
I have change notation to access arrays:

$_FILES['userfile'][$a]

But I'm still not uploading anything...

Dont' know what else to check out, I'm getting so frustrated
:cry:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

error_reporting(E_ALL);
On the top line of your page... what are we getting in errors?
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

Nothing reported by

error_reporting(E_ALL);
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

by the way, this is how my code looks like by now.
Uploaded files = 0;
Desperation = 10++;
:oops:

Code: Select all

<? 
error_reporting(E_ALL);
if($intro==1){
$totalimages=count($userfile);
if($totalimages >= 1){
for($a=0;$a<$totalimages;$a++){
switch($_SERVER['REQUEST_METHOD']) {

   case true:
   
   if(!isset($_FILES['userfile'][$a]) || $_FILES['userfile'][$a] == "none" || $_FILES['userfile'][$a] == "") {
		$msg = "Press browse to select a picture";
		break;
	}


	$tmp = getcwd()."/".$_FILES['userfile'][$a]['name'];

	if(!@move_uploaded_file($_FILES['userfile'][$a]['tmp_name'], $tmp)) {

$msg = "<font color='#0000FF' size='1' face='Arial, Helvetica, sans-serif'>Ha ocurrido un error al subir la imagen</font>";
		break;
	}

	$fp = fopen($tmp, "rb");
	$str = fread($fp, filesize($tmp));

	fclose($fp);
	unlink($tmp);

	$im1 = ImageCreateFromString($str);

	$imgname = $ref."_thumb_".$a;
	$maxwidth =250;
	$maxheight = 200;

	$width1 = ImageSX($im1);
	$height1 = ImageSY($im1);
	$width2 = $maxwidth;

	$height2 = floor(($width2 * $height1) / $width1);

	if($maxheight > 0 && $height2 > $maxheight) {

		$height2 = $maxheight;
		$width2 = floor(($height2 * $width1) / $height1);
	}

	$im2 = ImageCreateTrueColor($width2, $height2);
	ImageCopyResampled($im2, $im1, 0, 0, 0, 0, $width2, $height2, $width1, $height1);

	ImageJpeg($im2, "thumb/".$imgname.".jpg");
	$msg = "Ok";
	$im3 = ImageCreateFromString($str);

	$imgname2 = $ref."_full_".$a;
	$maxwidth2 = 500;
	$maxheight2 = 300;
	$width12 = ImageSX($im3);
	$height12 = ImageSY($im3);
	$width22 = $maxwidth2;
	$height22 = floor(($width22 * $height12) / $width12);

	if($maxheight2 > 0 && $height22 > $maxheight2) {

		$height22 = $maxheight2;
		$width22 = floor(($height22 * $width12) / $height12);
	}

	$im4 = ImageCreateTrueColor($width22, $height22);
	ImageCopyResampled($im4, $im3, 0, 0, 0, 0, $width22, $height22, $width12, $height12);

	ImageJpeg($im4, "full/".$imgname2.".jpg");
	ImageDestroy($im1);
	ImageDestroy($im2);
	ImageDestroy($im3);
	ImageDestroy($im4);
	break;
}}}
Post Reply