Jump to content


Photo

php image upload


  • Please log in to reply
5 replies to this topic

#1 adi123

adi123

    Advanced Member

  • Members
  • PipPipPip
  • 50 posts

Posted 03 February 2013 - 12:13 PM

I need help with uploading an image to my website. I would like an error to be printed when an image is bigger than 1mb, but the nothing happens.

Any help please, been trying to figure out what the problem is for a couple of days know.

The code is below:

<?php 
error_reporting(0);


$error_msgs = array();
$errors = false;


//define ("MAX_SIZE","400");


//$max_size = "400";


function getExtension($str) {

$i = strrpos($str,".");

if (!$i) {
 
  return "";

}

$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}


if($_SERVER["REQUEST_METHOD"] == "POST") {
$image = $_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];
 
 
if ($image) {

  $filename = stripslashes($_FILES['file']['name']);
 
  $extension = getExtension($filename);
  $extension = strtolower($extension);
 
 
  if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
 
   echo "<p class=\"error\">Unknown Image extension</p>";
   $errors = true;
  
  } else {
  
   $size = filesize($_FILES['file']['name']);
 
 
   if ($size > 1024) {
   
    echo "<p class=\"error\">The image file size is larger than 1MB. Please reduce the size of the image and try again</p>";
    $errors = true;
   
   } else {
 
 
    if($extension=="jpg" || $extension=="jpeg" ) {
  
  
	 $uploadedfile = $_FILES['file']['tmp_name'];
	 $src = imagecreatefromjpeg($uploadedfile);
	
    } else if ($extension=="png") {
  
	 $uploadedfile = $_FILES['file']['tmp_name'];
	 $src = imagecreatefrompng($uploadedfile);
	
    } else {
	 
	 $src = imagecreatefromgif($uploadedfile);
	
    }
  
    echo $scr;
 
    list($width,$height)=getimagesize($uploadedfile);
 
  
    $newwidth=800;
    $newheight=($height/$width)*$newwidth;
    $tmp=imagecreatetruecolor($newwidth,$newheight);
  
  
    $newwidth1=200;
    $newheight1=($height/$width)*$newwidth1;
    $tmp1=imagecreatetruecolor($newwidth1,$newheight1);
	
	
    $newwidth2=50;
    $newheight2=($height/$width)*$newwidth2;
    $tmp2=imagecreatetruecolor($newwidth2,$newheight2);
	
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
	
    imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
   
    imagecopyresampled($tmp2,$src,0,0,0,0,$newwidth2,$newheight2,$width,$height);
  
  
    $filename = "images/". $_FILES['file']['name'];
 
    $filename1 = "images/small_". $_FILES['file']['name'];
	
    $filename2 = "images/thumb_". $_FILES['file']['name'];
	
	
	
    imagejpeg($tmp,$filename,100);
   
    imagejpeg($tmp1,$filename1,100);
	
    imagejpeg($tmp2,$filename2,100);
   
    imagedestroy($src);
    imagedestroy($tmp);
    imagedestroy($tmp1);
    imagedestroy($tmp2);
 
   }
 
  }

}


}



if (empty($_FILES['file']['name'])) {

if (isset($_POST['Submit'])) {

  echo "<p class=\"error\">Please select an image!</p>";
  $errors = true;
 
}

}



//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors) { 


header("Location: success.php");

}



?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta content="en-us" http-equiv="Content-Language">


<title>Image Upload</title>


<link href=".css" media="screen, projection" rel="stylesheet" type="text/css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>


<style type="text/css">


a:link {
color: #00F;
}


a:visited {
color: #00F;
}


.error {
color:#F00;
}
</style>


</head>


<body>


<form method="post" action="" enctype="multipart/form-data" name="form1">


<p>Picture</p> 
    
<input size="25" name="file" type="file" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt" class="box"/>
    
<p>Image maximum size <b>400 </b>kb</span></p>
    
<input type="submit" id="mybut" value="       Upload        " name="Submit"/>


</form>


<p>&nbsp;</p>


<p><a href="images/">Images Folder</a></p>
  
</body>


</html>


#2 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,964 posts
  • LocationCanada

Posted 03 February 2013 - 12:48 PM

That's because you are defining the size as false/0.

$size = filesize($_FILES['file']['name']);

The 'name' index is just the name of the file - not the location. Use 'tmp_name' to get the filepath to the file.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 adi123

adi123

    Advanced Member

  • Members
  • PipPipPip
  • 50 posts

Posted 03 February 2013 - 01:20 PM

I have changed the name to tmp_name. I now get an error message saying:

Notice: Undefined index: file in /upload.php on line 30

Notice: Undefined index: file in /upload.php on line 31

#4 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,964 posts
  • LocationCanada

Posted 03 February 2013 - 01:23 PM

Do a print_r() on $_FILES to ensure it contains what you think it does
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#5 adi123

adi123

    Advanced Member

  • Members
  • PipPipPip
  • 50 posts

Posted 03 February 2013 - 01:45 PM

It worked, thanks for your help. I changed the code to this:

if ($size > 1048576) {
   
echo "<p class=\"error\">The image file size is larger than 1MB. Please reduce the size of the image and try again</p>";
$errors = true;
   
} else if ($size < 1048576) { 

The file size was being checked in bytes, which was the mistake

#6 albertdiones

albertdiones

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 03 February 2013 - 09:52 PM

As far as I know you should filesize() the tmp_name not the name of the $_FIlE['file'] (with the capital l, dunno what's up with this keyboard)

Edit:

well, how to delete this? lol. I didn't saw it was already solved. sorry.


Anyway, just a tip adi123, on that construct you should do



if (is_int($size) && $size < 1048576) {
   // Script If the size is valid
}
else {
   // if the size isn't valid
}

Edited by albertdiones, 03 February 2013 - 10:02 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com