Jump to content

PHP4 image upload script no longer works in PHP5


Recommended Posts

So this script did work in previous versions oh php but in php5 I get the error, "Sorry, uploaded images must be of type jpg or jpeg, (not )."

So it looks like it's not recognizing my file as a jpeg??

 

Any contributions towards a fix are GREATLY appreciated!!!  If there are clean, nice (EASY) php5 scripts available for this simple image uploading and thumbnail creation I would consider starting this over.

 

 

//Listing page code:

 

echo "<form name=\"addCat\" enctype=\"multipart/form-data\" action=\"preview.php\" method=\"post\">";

<INPUT type="file" name="pic1" size="50" value="">

<INPUT type="file" name="pic2" size="50" value="">

<INPUT type="file" name="pic3" size="50" value="">

<INPUT type="file" name="pic4" size="50" value="">

<input type="submit" value="Upload Images">

 

//preview.php page code:

 

$pic1 = $_FILES['pic1'];

$pic2 = $_FILES['pic2'];

$pic3 = $_FILES['pic3'];

$pic4 = $_FILES['pic4'];

 

function ResizeImage($im,$maxwidth,$maxheight,$name){

$width = imagesx($im);

$height = imagesy($im);

if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){

if($maxwidth && $width > $maxwidth){

$widthratio = $maxwidth/$width;

$RESIZEWIDTH=true;

}

if($maxheight && $height > $maxheight){

$heightratio = $maxheight/$height;

$RESIZEHEIGHT=true;

}

if($RESIZEWIDTH && $RESIZEHEIGHT){

if($widthratio < $heightratio){

$ratio = $widthratio;

}else{

$ratio = $heightratio;

}

}elseif($RESIZEWIDTH){

$ratio = $widthratio;

}elseif($RESIZEHEIGHT){

$ratio = $heightratio;

}

    $newwidth = $width * $ratio;

        $newheight = $height * $ratio;

if(function_exists("imagecopyresampled")){

      $newim = imagecreatetruecolor($newwidth, $newheight);

      imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

}else{

$newim = imagecreate($newwidth, $newheight);

      imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

}

        ImageJpeg (($newim),$name . ".jpg");

ImageDestroy ($newim);

}else{

ImageJpeg ($im,$name . ".jpg");

}

}

 

 

// Set up some variables

flush();

set_time_limit(210);  // set process page limit to 3.5 minutes

$maxfilesize = "102400"; //# individual file size limit - in bytes (102400 bytes = 100KB)

$path="2012images";

$thumbpath="thumbs";

 

// two data pulls to rename images - would be easier to have a permanent counter...fix later

$mysql[getpicname] = mysql_query("select pic from picname");

$z = mysql_fetch_array($mysql[getpicname]);

$new_filename=$z[pic];

$new_z[pic]=$z[pic]+$total_pics;

if(@mysql_query("UPDATE picname SET pic = '$new_z[pic]' "))  echo "";

 

 

// Create thumbnail from imgurl and store in thumbs

 

// Height to reszie image to (in pixels)

$RESIZEHEIGHT=200;

// Width to reszie image to (in pixels)

$RESIZEWIDTH=200;

//thumbnail to thumbs folder, named same as 1st image

$FILENAME=$thumbpath."/".$new_filename;

$thumb_name=$thumbpath."/".$new_filename.".jpg";

 

// create a thumbnail from the first image only

$im = imagecreatefromjpeg($_FILES['pic1']['tmp_name']);

if($im)

{

if(file_exists("$pic_1.jpg")) {

unlink("$pic_1.jpg");

}

if (ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME))

ImageDestroy ($im);

}

 

 

//now upload the images.....

for ($i=1;$i<5;$i++)

{

//check if blank or image

 

$xpic="pic".$i;

if (($$xpic<>"none") and ($$xpic<>"")) {

 

  // check if jpg

  $cert1 = "image/pjpeg"; //jpg

  $cert2 = "image/jpeg"; //jpg (for MAC)

  $xtype =$HTTP_POST_FILES[$xpic]['type'];

$xsize =$HTTP_POST_FILES[$xpic]['size'];

 

  if (($xtype <> $cert1) AND ($xtype <> $cert2)) {

echo "<DIV class=\"errorsubheading\"><br><br>Alert - Missing or Invalid Information</DIV><br>";

echo "<DIV class=\"requiredtext\">Sorry, uploaded images must be of type jpg or jpeg, (not $xtype).</DIV><BR>";

echo "<BR><br><br>";

include ("footer.php");

exit();

}

if ($xsize > $maxfilesize) {

echo "<DIV class=\"errorsubheading\"><br><br>Alert - Missing or Invalid Information</DIV><br>";

echo "<DIV class=\"requiredtext\">Sorry, uploaded images must be under the maximum size requirements.</DIV><BR>";

echo "<BR><br><br>";

include ("footer.php");

exit();

}

 

  else {

 

// upload to server

 

$dest=$path."/".$new_filename.".jpg";

If (move_uploaded_file($$xpic, $dest)) {

$photo[$i]=$dest;

$new_filename+=1;

  }

  else  {

echo "<DIV class=\"errorsubheading\"><br><br>Alert - Upload Failed!</DIV><br>";

echo "<BR><br><br>";

include ("footer.php");

exit();

    } // upload sucessful

 

} // end type check

} // end check if blank

}

 

 

 

Link to comment
Share on other sites

Adding Code tags:

 

So this script did work in previous versions oh php but in php5 I get the error, "Sorry, uploaded images must be of type jpg or jpeg, (not )."

So it looks like it's not recognizing my file as a jpeg??

 

Any contributions towards a fix are GREATLY appreciated!!!  If there are clean, nice (EASY) php5 scripts available for this simple image uploading and thumbnail creation I would consider starting this over.

 


<?

//Listing page code:

echo "<form name=\"addCat\" enctype=\"multipart/form-data\" action=\"preview.php\" method=\"post\">";
<INPUT type="file" name="pic1" size="50" value="">
<INPUT type="file" name="pic2" size="50" value="">
<INPUT type="file" name="pic3" size="50" value="">
<INPUT type="file" name="pic4" size="50" value="">
<input type="submit" value="Upload Images">


?>

 


<?
//preview.php page code:

$pic1 = $_FILES['pic1'];
$pic2 = $_FILES['pic2'];
$pic3 = $_FILES['pic3'];
$pic4 = $_FILES['pic4'];

function ResizeImage($im,$maxwidth,$maxheight,$name){
   $width = imagesx($im);
   $height = imagesy($im);
   if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
      if($maxwidth && $width > $maxwidth){
         $widthratio = $maxwidth/$width;
         $RESIZEWIDTH=true;
      }
      if($maxheight && $height > $maxheight){
         $heightratio = $maxheight/$height;
         $RESIZEHEIGHT=true;
      }
      if($RESIZEWIDTH && $RESIZEHEIGHT){
         if($widthratio < $heightratio){
            $ratio = $widthratio;
         }else{
            $ratio = $heightratio;
         }
      }elseif($RESIZEWIDTH){
         $ratio = $widthratio;
      }elseif($RESIZEHEIGHT){
         $ratio = $heightratio;
      }
       $newwidth = $width * $ratio;
        $newheight = $height * $ratio;
      if(function_exists("imagecopyresampled")){
            $newim = imagecreatetruecolor($newwidth, $newheight);
            imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
      }else{
         $newim = imagecreate($newwidth, $newheight);
            imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
      }
        ImageJpeg (($newim),$name . ".jpg");
      ImageDestroy ($newim);
      }else{
      ImageJpeg ($im,$name . ".jpg");
      }
}


// Set up some variables
flush();
set_time_limit(210);  // set process page limit to 3.5 minutes
$maxfilesize = "102400"; //# individual file size limit - in bytes (102400 bytes = 100KB)
$path="2012images";
$thumbpath="thumbs";

// two data pulls to rename images - would be easier to have a permanent counter...fix later
$mysql[getpicname] = mysql_query("select pic from picname");
$z = mysql_fetch_array($mysql[getpicname]);
$new_filename=$z[pic];
$new_z[pic]=$z[pic]+$total_pics;
if(@mysql_query("UPDATE picname SET pic = '$new_z[pic]' "))  echo "";


// Create thumbnail from imgurl and store in thumbs

// Height to reszie image to (in pixels)
$RESIZEHEIGHT=200;
// Width to reszie image to (in pixels)
$RESIZEWIDTH=200;
//thumbnail to thumbs folder, named same as 1st image
$FILENAME=$thumbpath."/".$new_filename;
$thumb_name=$thumbpath."/".$new_filename.".jpg";

// create a thumbnail from the first image only
$im = imagecreatefromjpeg($_FILES['pic1']['tmp_name']);
if($im)
   {
   if(file_exists("$pic_1.jpg")) {
      unlink("$pic_1.jpg");
      }
   if (ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME))
      ImageDestroy ($im);
   }


//now upload the images.....
for ($i=1;$i<5;$i++)
   {
    //check if blank or image

    $xpic="pic".$i;
    if (($$xpic<>"none") and ($$xpic<>"")) {
   
        // check if jpg
        $cert1 = "image/pjpeg"; //jpg
        $cert2 = "image/jpeg"; //jpg (for MAC)
        $xtype =$HTTP_POST_FILES[$xpic]['type'];
      $xsize =$HTTP_POST_FILES[$xpic]['size'];

        if (($xtype <> $cert1) AND ($xtype <> $cert2)) {
         echo "<DIV class=\"errorsubheading\"><br><br>Alert - Missing or Invalid Information</DIV><br>";
         echo "<DIV class=\"requiredtext\">Sorry, uploaded images must be of type jpg or jpeg, (not $xtype).</DIV><BR>";
         echo "<BR><br><br>";
         include ("footer.php");
         exit();
         }
       if ($xsize > $maxfilesize) {
         echo "<DIV class=\"errorsubheading\"><br><br>Alert - Missing or Invalid Information</DIV><br>";
         echo "<DIV class=\"requiredtext\">Sorry, uploaded images must be under the maximum size requirements.</DIV><BR>";
         echo "<BR><br><br>";
         include ("footer.php");
         exit();
         }
         
        else {
         
            // upload to server
            
            $dest=$path."/".$new_filename.".jpg";
            If (move_uploaded_file($$xpic, $dest)) {
               $photo[$i]=$dest;
               $new_filename+=1;
                 }
              else  {
                  echo "<DIV class=\"errorsubheading\"><br><br>Alert - Upload Failed!</DIV><br>";
                  echo "<BR><br><br>";
                  include ("footer.php");
                  exit();
                    } // upload sucessful
         
         } // end type check
      } // end check if blank
   }

?>

 

 

Link to comment
Share on other sites

Yes - the variables pic1, pic2 etc... look like this pic1 = Array - which I assume is a good thing and now needs 'parsing'?

 

Only issue is I get 'pic1 = Array' regardless of whether the field is submitted empty or with a value - in my old code I was checking for a value to determine if there was an image but now there is a 'value' regardless!

 

Link to comment
Share on other sites

The problem I noticed when testing was related to the MOVE line.

If (move_uploaded_file($$xpic, $dest)) {

 

Where I would normally go something like

If (move_uploaded_file($_FILES[$$xpic]["tmp_name"], $dest))

 

I however didn't have time to debug the issue further or see if that resolves the problem.

Link to comment
Share on other sites

Hey Drummin!

 

 

Thanks but the problem must occur before that code because it doesn't reach that code.  I know the code is close because it uploads the thumbnail to my thumbnail directory after resizing it just fine - it just doesn't upload the main image.

 

By my echoing the filename it appears that my image isn't getting the ".jpg" appended to the end of the filename - but I'm not sure where to append it??

 

 

Link to comment
Share on other sites

OK - Narrowed down the issue to these last two lines:

 

  $xtype =$HTTP_POST_FILES[$$xpic]['type'];

$xsize =$HTTP_POST_FILES[$$xpic]['size'];

 

Both return nothing, so my script fails here.

 

Right before these two lines I do a:

print_r($$xpic);

and get:

Array ( [name] => summer_reg.jpg [type] => image/jpeg [tmp_name] => /tmp/phpEcPGE2 [error] => 0 => 17997 )

 

We are almost there!! 

Link to comment
Share on other sites

Glad you got it figured out.  Is it uploading four images at a time?

Anyway, I made my own version.  Different but seems to be working.

<?php
//preview.php page code:
if (isset($_POST['submit'])){
foreach($_FILES as $a => $b){
	if(!empty($b['name'])){
	$maxfilesize = "102400";
		if ($b['size'] > $maxfilesize) {
        echo "<DIV class=\"requiredtext\">Sorry, uploaded images must be under the maximum size requirements.</DIV><BR>";
        echo "<BR><br><br>";
        include ("footer.php");
        exit();
        }
	$filename = stripslashes($b['name']);
	$allowed_filetypes = array('.jpg','.jpeg','.JPEG','.JPG');
	$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
		if(in_array($ext,$allowed_filetypes)){
		$uploadedfile = $b['tmp_name'];
		$src = imagecreatefromjpeg($uploadedfile);
		list($width,$height)=getimagesize($b['tmp_name']);
			if($height>$width){
			$newheight=200;
			$newwidth=($width/$height)*$newheight;
			}
			else{
			$newwidth=200;
			$newheight=($height/$width)*$newwidth;
			}
		$tmp=imagecreatetruecolor($newwidth,$newheight);
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

		$path="2012images";
		$thumbpath="thumbs/".$b['name'];
		imagejpeg($tmp,$thumbpath,100);
		move_uploaded_file($b["tmp_name"], $path ."/". $filename);
			$filename=mysql_real_escape_string($filename);
			$sql="INSERT INTO picname (pic) VALUES ('$filename')";
			$result=mysql_query($sql) or die(mysql_error());
		}//if(in_array($ext,$allowed_filetypes))
		else{
		echo "<DIV class=\"requiredtext\">Sorry, uploaded images must be of type jpg or jpeg, (not $ext).</DIV><BR>";
		echo "<BR><br><br>";
		include ("footer.php");
		exit();
		}
	}//if(!empty($b['name']))
}//foreach($_FILES as $a => $b)
}//if (isset($_POST['submit']))
?>

Link to comment
Share on other sites

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.