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
Perfidus
Forum Contributor
Posts: 114 Joined: Sun Nov 02, 2003 9:54 pm
Post
by Perfidus » Thu Jun 30, 2005 3:44 am
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="e;userfileї]"e; type="e;file"e; /><br />
<input name="e;userfileї]"e; type="e;file"e; /><br />
<input name="e;userfileї]"e; type="e;file"e; /><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 » Thu Jun 30, 2005 3:49 am
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 » Thu Jun 30, 2005 3:51 am
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 » Thu Jun 30, 2005 4:02 am
-
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
-
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Thu Jun 30, 2005 4:09 am
print_r($_FILES) and look what's in ['error']... if it's 1 something went wrong.
By the way...
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 » Thu Jun 30, 2005 4:22 am
$userfile comes from the form:
Code: Select all
<input name="e;userfileї]"e; type="e;file"e; /><br />
<input name="e;userfileї]"e; type="e;file"e; /><br />
<input name="e;userfileї]"e; type="e;file"e; /><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 » Thu Jun 30, 2005 4:50 am
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
!
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Thu Jun 30, 2005 5:43 am
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 » Thu Jun 30, 2005 6:02 am
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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Thu Jun 30, 2005 6:14 am
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 » Thu Jun 30, 2005 7:41 am
Nothing reported by
error_reporting(E_ALL);
Perfidus
Forum Contributor
Posts: 114 Joined: Sun Nov 02, 2003 9:54 pm
Post
by Perfidus » Thu Jun 30, 2005 8:35 am
by the way, this is how my code looks like by now.
Uploaded files = 0;
Desperation = 10++;
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;
}}}