Jump to content

image upload


jakebur01

Recommended Posts

Hello,

 

I just moved my image uploader script from a windows server to linux.  Somehow I guess php was grabbing the post variables automatically on the windows server.

 

Any way,  my problem is that it is not reading the file on the upload page.  It throws the error "Error: No File Specified".  I tried putting $img1_name=$_FILES['img1_name'];  then I was getting a file extension error. 

 

form

  <FORM ACTION="/imageuploader/image_receiver.php" METHOD="POST" NAME="udata" ENCTYPE="multipart/form-data">
				  
				  <FONT SIZE=-1>
				  <p class="m_text">Your post may not contain any foul content! <br />Your ip address is <?php echo $ip; ?> and will be stored for our records.</p>
				  <p><B>Enter Photo Upload Code:</B> 
				    <INPUT TYPE="password" NAME="pass" SIZE=10>
				    <img src="images/secretcode.jpg" alt="secret code" width="150" height="45"></p>
				  <p><B>Enter File Name:</B> 
				    <INPUT TYPE="file" NAME="img1_name" id="img1_name" SIZE="30">
				  </p>
				  <p><B>Name:</B> 
				    <label>
				    <input name="name" type="text" id="name" value="" size="30" maxlength="30">
				    </label>
				  </p>
				  <p><B>Description:</B> 
				    <label>
				    <input name="description" type="text" id="description" value="" size="50" maxlength="80">
				    </label>
				  </p>
				  <p>
				    
			        <INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Upload File">
				    
				    </p>
				  </FORM>

 

 

image upload page

<?php
$name = $_POST['name'];
$description = $_POST['description'];
$pass = $_POST['pass'];

////////////////// Image Uploader Receiver ///////////////////
// Copyright (c) 2003 CarTest Software, All Rights Reserved //
//////////////////////////////////////////////////////////////

include "image_receiver_header.html";
require "image_config.php";

// check password
if($require_password_to_upload && $pass != $password) die("Invalid Password Entered");		

// file specified?
if($img1_name == "") die("Error: No File Specified");

// allowed file types
$ext1=".jpg";
$ext2=".gif";		
$ext=strtolower(substr($img1_name, strlen($img1_name)-4)); 
$imageType=0;
if($ext == $ext1) {
   $imageType=1;
} elseif ($ext == $ext2) { 
   $imageType=2;
} else {
   die("Error: $ext1 or $ext2 File Types Only Please");
}
$ran = rand () ;
$img1_name = $ran.date(MDY)."$ext";

$db = new mysqli('localhost', 'tuser', 'user', 'table');

if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}

$ip=$_SERVER['REMOTE_ADDR'];
$url= "http://www.mysite.com/imageuploader/uploads/$img1_name";
$query = "insert into extremephoto values
('0', '$url', '$name', '$description', '$ip', 'no')";

$result = $db->query($query) or die(mysqli_error());
if ($result)
echo $db->affected_rows.' message inserted into database. ';


$db->close();



// file size limit
if($file_size_limit >= 0 && $img1_size > $file_size_limit) die ("File Size Exceeds $file_size_limit Byte Limit");

// replace certain characters with _    
$img1_name=strtr($img1_name," ","_");
$img1_name=strtr($img1_name,"#","_");
$img1_name=strtr($img1_name,"/","_");
$img1_name=strtr($img1_name,"\\","_");  // need to escape the backslash character
$img1_name=strtr($img1_name,"%","_");


// check if file already exists
$fullpath_to_image=$path_to_images.$img1_name;
if(file_exists($fullpath_to_image)) {
   die("Error: File Already Exists");
}

// check for banned ip		
if(strlen($banned_ip_list_file_name) > 0) {
   $fo=fopen($banned_ip_list_file_name,"rt"); 
   if($fo != 0) {
      while(!feof($fo)) {     
         $ipaddr=fgets($fo);
         $ipaddr=substr($ipaddr, 0, strlen($ipaddr)-1);   // strip \n character at end automatically read in
         $l=strlen($ipaddr);
         if($l > 0 && substr($REMOTE_ADDR, 0, $l) == $ipaddr) die("Uploader Closed");		
      }
      $fc=fclose($fo);
   }
}

// full paths to thumbnails
if(strlen($path_to_thumbnails) > 0 && $thumbnail_width > 0 && $thumbnail_height > 0) {
   $fullpath_to_thumbnail=$path_to_thumbnails.$img1_name;
   $thumbs=true;
} else {
   $thumbs=false;
}   

// save image, @ symbol means ignore errors generated 
copy($img1 , $fullpath_to_image) or die("Error: Couldn't Upload Your File");

// add file name, ip address, and timestamp to log file
// open, lock the file, seek file pointer to end, write record, close (unlocks) 
if(strlen($upload_log_file_name) > 0) {
   $fo=fopen($upload_log_file_name,"a+t"); 
   $fl=flock($fo,LOCK_EX);
   $fs=fseek($fo, 0, SEEK_END);
   $fw=fputs($fo,$img1_name." ".$REMOTE_ADDR." ".date("M d Y, H:i")."\n");
   $fc=fclose($fo);
}

//  resize? 
if($max_image_width > 0 && $max_image_height > 0) {
   $resize=true;
} else {
   $resize=false;
}

// resize image to (maxwidth x maxheight) preserving existing aspect ratio (unless already smaller) and resave with fixed compression level
// and create thumbnail .jpeg only 
if($imageType == 1) {

// create images		   
   if($thumbs) $imgthumb=@imageCreateTrueColor($thumbnail_width, $thumbnail_height) or die ("Error: Cannot Create Thumbnail Image");

// need an image resource for resizing, do this even if not resizing to check for valid jpeg format file	
   $im=@imageCreateFromJpeg($fullpath_to_image);
   if($im === false) {
   @unlink($fullpath_to_image) or die("Error: Improper Jpeg Image, Couldn't Delete File");  
   die ("Error: Improper Jpeg Image, Image Deleted");  
   }

// original image size
   $sx=imageSx($im);
   $sy=imageSy($im);

   if($thumbs) {
//       use Resampled rather than Resize otherwise thumbnail is terrible	
      @imageCopyResampled($imgthumb, $im, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $sx, $sy) or die("Error: Error Resizing for Thumbnail");

//       write to subdirectory with same name as large file
      @imageJpeg($imgthumb, $fullpath_to_thumbnail, $image_quality) or die("Error: Couldn't Save Jpeg Thumbnail");
   }

   if($resize) {
//       delete initial image 
      @unlink($fullpath_to_image) or die("Error: Couldn't Delete Original Image");  

//       resize original image 
      if($sx > $max_image_width || $sy > $max_image_height) {
         $rx=round($max_image_width);
         $ry=round($rx/($sx/$sy));
         if($ry > $max_image_height) {
            $ry=round($max_image_height);
            $rx=round($ry*($sx/$sy));
         }
         $imresized=@imageCreateTrueColor($rx, $ry) or die ("Error: Cannot Create Resized Image");
         @imageCopyResampled($imresized, $im, 0, 0, 0, 0, $rx, $ry, $sx, $sy) or die("Error: Error Resizing Image");
         @imageJpeg($imresized, $fullpath_to_image, $image_quality) or die("Error: Couldn't Save Resampled/Resized Jpeg");
      } else {	  
         @imageJpeg($im, $fullpath_to_image, $image_quality) or die("Error: Couldn't Save Resampled Jpeg");
      }
   }

} else {

// image size only for .gif
   $as = Array();
   $as=getImageSize($fullpath_to_image);   	   
   $sx=$as[0];    // image width
   $sy=$as[1];    // image height
}

echo "<BR><BR><BR>";
echo "<H1>File Upload Successful!</H1>";
//echo "<P>Successfully Sent: $img1_name, a $img1_size byte, $sx x $sy (original size) file with the extension type of $img1_type"; 
echo "<BR>";

if($resize && $imageType == 1) {    
   $newsize=filesize($fullpath_to_image);
   if($sx > $max_image_width || $sy > $max_image_height) {
  // echo "<P>Image was resized to $rx x $ry and recompressed to $newsize bytes<BR>";   
   } else {
      //echo "<P>Image was recompressed to a file size of $newsize bytes<BR>";  
   }
}

Link to comment
Share on other sites

Please bear in mind that $_FILES is a multi-dimensional array.

 

http://www.w3schools.com/PHP/php_file_upload.asp

 

$_FILES['img1_name']['size']
$_FILES['img1_name']['name']
$_FILES['img1_name']['type']
$_FILES['img1_name']['tmp_name']
$_FILES['img1_name']['error']

 

etc...

 

I can't see anywhere in your code where you use these.

Link to comment
Share on other sites

I have tried all three extensions.  I think the problem may be that I am not properly posting the file to the page.  I am currently using: $img1_name = $_FILES['img1_name'];

 

is the a full code designed by Copyright © 2003 CarTest Software, All Rights Reserved

 

somthing is missing

 

if ($HTTP_POST_VARS['submit']) 
{
/////////////code
}

Link to comment
Share on other sites

Please bear in mind that $_FILES is a multi-dimensional array.

 

http://www.w3schools.com/PHP/php_file_upload.asp

 

$_FILES['img1_name']['size']
$_FILES['img1_name']['name']
$_FILES['img1_name']['type']
$_FILES['img1_name']['tmp_name']
$_FILES['img1_name']['error']

 

etc...

 

I can't see anywhere in your code where you use these.

 

This is what I was looking for.  How can I store all of these into the $img1_name variable?

Link to comment
Share on other sites

I tried this:

$img1_name=$_FILES['img1_name']['size'];

$img1_name.=$_FILES['img1_name']['name'];

$img1_name.=$_FILES['img1_name']['type'];

$img1_name.=$_FILES['img1_name']['tmp_name'];

$img1_name.=$_FILES['img1_name']['error'];

 

But,....

I get this:

Error: .jpg or .gif File Types Only Please

Link to comment
Share on other sites

Then there's a problem with your error handling in your code, rather than a server issue.

 

Also, bear in mind that it won't work if you concatenate all of the elements into a single string.

 

$img1_name = $_FILES['img1_name']['name']; // This must remain just for the name, not size etc..

Link to comment
Share on other sites

The $img1_name variable is what is in $_FILES['img1_name']['name'];

 

The $img1_size variable is what is in $_FILES['img1_name']['size'];

 

The $img1 variable is what is in $_FILES['img1_name']['tmp_name];

 

It's not a matter of trying to store all those values into $img1_name, it's a matter of correcting your code to use each value where it needs to be. That code is almost 8 years out of date.

Link to comment
Share on other sites

why this ?

$ext=strtolower(substr($img1_name, strlen($img1_name)-4));

 

 

$whitelist = array(".jpg");
   foreach ($whitelist as $ending) {

   if(substr($filename, -(strlen($ending))) != $ending) {
echo "error msg";
   }

 

Even to make it more secure

 

read first fev characters of file and check valid jpg image file

 

$f = @fopen($_FILES['file']['tmp_name'],'r');
   $s = @fread($f,4);
   @fclose($f);
   if($s != "&#255;&#216;&#255;&#224;"){

Link to comment
Share on other sites

why to go in such complicated manner if you have alternative easy solution for that

 

For JPG

      $thumbWidth ="128";
      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$filename}" );
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save thumbnail into a file
      imagejpeg( $tmp_img, "{$pathToThumbs}{$filename}" );

 

For GIF and even with animated gif

 

$thumbWidth ="128";
$im = new Imagick("$pathToImages/$filename");
$height=$im->getImageHeight();
$width=$im->getImageWidth();

$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );

// Resize all frames
foreach ($im as $frame) {

    $frame->thumbnailImage("$new_width", "$new_height");

// Set the virtual canvas to correct size I obeserved that somtimes it wont work in case of more than 5 frames of image
    $frame->setImagePage("$new_width", "$new_height", 0, 0);
   }

$im->writeImages("128/$filename", true);

Link to comment
Share on other sites

I have an error on the gif resizer:  Fatal error: Class 'Imagick' not found

 

You have to install ImageMagic and Imagick extension on server. If you have root access then install ImageMagick if you are on centos try with rpmforge

like

yum -y install ImageMagick

 

for Imagick extension

 

# cd /usr/local/src
# wget http://pecl.php.net/get/imagick-2.2.2.tgz
# tar zxvf ./imagick-2.2.2.tgz
# cd imagick-2.2.2
# phpize
# ./configure
# make
# make test
# make install

 

If on shared hosting ask your hosting co. for extension.

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.