Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP file upload

Newbie
 
Join Date: Oct 2007
Posts: 22
#1: Jun 22 '08
Hi guys,

I have managed to code a file that uploads a file, resizes it to 100 x 100 px, and then generates a random number and renames the image (so i do not get images over writing each other), what i am stuck on is trying to block uploads that are over 200kb, and if there is no file to upload (as there will be no need sometimes to upload a file) to skip the code all together in order to process some more code i got going on after this piece, can anyone help! i have looked around various websites and am just absoulty stuck on this!

Any help is much appriciated

The Code:

<?php
if(isset($_POST['submit'])){
if (isset ($_FILES['file'])){
imagename = $_FILES['file']['name'];
$random_digit=rand(0000,9999);
$new_file_name=$random_digit.$imagename;

$source = $_FILES['file']['tmp_name'];
$target = "Articleimages/".$new_file_name;
move_uploaded_file($source, $target);


$imagepath = $new_file_name;
$save = "Articleimages/" . $imagepath;
$file = "Articleimages/" . $imagepath;

list($width, $height) = getimagesize($file) ;

$modwidth = 100;

$diff = $width / $modwidth;

$modheight = 100;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

imagejpeg($tn, $save, 100) ;

$save = "Articleimages/Thumbs/" . $imagepath;
$file = "Articleimages/" . $imagepath;

list($width, $height) = getimagesize($file) ;

$modwidth = 100;

$diff = $width / $modwidth;

$modheight = 100;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

imagejpeg($tn, $save, 100) ;

}
}
}
?>



Cheers

Jay!

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,938
#2: Jun 22 '08

re: PHP file upload


You can obtain the size of the file in the $_FILES array.

$_FILES['file']['size']

Then use an IF check to compare sizes.
Newbie
 
Join Date: Jun 2008
Posts: 25
#3: Jun 24 '08

re: PHP file upload


move the code to resize and rename the image in a new function. something like this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /* function to resize and rename the uploaded image */
  3. function resize_and_rename($filename) {
  4.   /* ...some code here...*/
  5. }
  6.  
  7. /* process uploaded image */
  8. if (is_uploaded_file($_FILES['file']['tmp_name'])) {
  9.   /* check if uploaded image size < 200kb */
  10.   if ($_FILES['file']['size'] >= 200000) {
  11.     die("Image size should be < 200kb!");
  12.   } else {
  13.     /* resize and rename the uploaded image */
  14.     resize_and_rename($_FILES['file']['tmp_name']);
  15.   }
  16. }
  17. ?>
  18.  
Newbie
 
Join Date: Oct 2007
Posts: 22
#4: Jul 3 '08

re: PHP file upload


Hi Guys,

Thank you very much for your replies, i managed to get the code working!

Jay!
Reply