SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: File Upload Problem
-
Mar 25, 2009, 06:26 #1
- Join Date
- Mar 2008
- Posts
- 110
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
File Upload Problem
have a script which i found online,through google for uploading and resizing photos when they are uploaded.
It works well,the only problem is that there are some images when i upload them they are not detected as a file,but others are uploaded well.
for example image of this properties is not detected.
(Dimension:3264*2448
camera model,DSC-W150
type:JPEG Image.
size:2,70 MB.
)
below is the script:
Code:<?php $user=$_SESSION['user']; if(isset($_POST['submit'])) { include"config.php"; //make sure this directory is writable! $path_thumbs ='C:/wamp/www/website/thumb/'; //the new width of the resized image, in pixels. $img_thumb_width = 120; // $extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed) //List of allowed extensions if extlimit = yes $limitedext = array(".gif",".jpg",".png",".JPG",".JPEG",".jpeg",".bmp"); //the image -> variables $file_type = $_FILES['vImage']['type']; $file_name = $_FILES['vImage']['name']; $file_size = $_FILES['vImage']['size']; $file_tmp = $_FILES['vImage']['tmp_name']; //check if you have selected a file. if(!is_uploaded_file($file_tmp)){ echo "Error: Please select a file to upload!. <br><a href=profilephoto.php>back</a>"; exit(); //exit the script and don't process the rest of it! } //check the file's extension $ext = strrchr($file_name,'.'); $ext = strtolower($ext); //uh-oh! the file extension is not allowed! if (($extlimit == "yes") && (!in_array($ext,$limitedext))) { echo "Wrong file extension. <br>--<a href=profilephoto.php>back</a>"; exit(); } //so, whats the file's extension? $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; //create a random file name $rand_name = md5(time()); $rand_name= rand(0,999999999); //the new width variable $ThumbWidth = $img_thumb_width; ////////////////////////// // CREATE THE THUMBNAIL // ////////////////////////// //keep image type if($file_size){ if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){ $new_img = imagecreatefromjpeg($file_tmp); }elseif($file_type == "image/x-png" || $file_type == "image/png"){ $new_img = imagecreatefrompng($file_tmp); }elseif($file_type == "image/gif"){ $new_img = imagecreatefromgif($file_tmp); } //list the width and height and keep the height ratio. list($width, $height) = getimagesize($file_tmp); //calculate the image ratio $imgratio=$width/$height; if ($imgratio>1){ $newwidth = $ThumbWidth; $newheight = $ThumbWidth/$imgratio; }else{ $newheight = $ThumbWidth; $newwidth = $ThumbWidth*$imgratio; } //function for resize image. if (function_exists(imagecreatetruecolor)){ $resized_img = imagecreatetruecolor($newwidth,$newheight); }else{ die("Error: Please make sure you have GD library ver 2+"); } //the resizing is going on here! imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //finally, save the image ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext"); ImageDestroy ($resized_img); ImageDestroy ($new_img); } //ok copy the finished file to the thumbnail directory move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext"); /* Don't want to copy it to a separate directory? Want to just display the image to the user? Follow the following steps: 2. Uncomment this code: /* /* UNCOMMENT THIS IF YOU WANT */ //echo "IMG:<img src=\"$path_big/$rand_name.$file_ext\" />"; //exit(); //*/ //and you should be set! //success message, redirect to main page. //$msg = urlencode("$rand_name.$file_ext was uploaded! <a href=\"Resize.php\">Upload More?</a>"); //header("Location: Resize.php?msg=$msgn include"myprofile.php"; include"config.php"; $query="UPDATE login SET photo='$rand_name.$file_ext' WHERE user='$user'"; $result=mysql_query($query); $check="UPDATE profile SET photos='$rand_name.$file_ext' WHERE users='$user'"; $jibu=mysql_query($check); if(!$result||!$jibu){ echo"failed to send to database"; } exit(); }else{ //if there is a message, display it if(isset($_GET['msg'])) { //but decode it first! echo "<p>".urldecode($_GET['msg'])."</p>"; } //the upload form } ?>
Code://check if you have selected a file. if(!is_uploaded_file($file_tmp)){ echo "Error: Please select a file to upload!. <br><a href=profilephoto.php>back</a>"; exit(); //exit the script and don't process the rest of it! }
bcoz this is the biggest problem i`m experiencing.Last edited by mrcniceguy; Mar 25, 2009 at 21:07.
-
Mar 25, 2009, 09:15 #2
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
print_r($_FILES);
Match the error up here
http://www.php.net/manual/en/feature...oad.errors.php
-
Mar 25, 2009, 10:44 #3
- Join Date
- Mar 2008
- Posts
- 110
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thankx for the link,but still i dont know what to do with the code.
-
Mar 25, 2009, 10:52 #4
- Join Date
- Mar 2009
- Posts
- 98
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
i found online,through google for uploading and resizing photos when they are uploaded.
Is the script also uploading photo's? and isn't it about serveral extensions?
or file sizes?
-
Mar 25, 2009, 11:02 #5
- Join Date
- Mar 2008
- Posts
- 110
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
the extension is limited here,which is all image types
Code:$limitedext = array(".gif",".jpg",".png",".JPG",".JPEG",".jpeg",".bmp");
because it uploads some photos and others is like i mentioned above.
-
Mar 26, 2009, 07:54 #6
- Join Date
- Mar 2009
- Posts
- 98
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That is strange indeed is there absolutely no difference between the pictures size extensions etc?
-
Apr 21, 2009, 20:39 #7
- Join Date
- Aug 2004
- Location
- philippines
- Posts
- 574
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi guys, i just wanted to bump this thread, as I'm having the same problem.
I found this resizing function on php.net. and it works fine for jpg and png files, but I'm having problems with uploading and resizing JPG files. the funny thing is, if i rename the image, say DC0001.JPG -> DC0001.jpg, it will be accepted and resize just fine.
Here is the code.
Code:function createimage($name,$filetype,$filename,$new_w,$new_h) { //$filetype = strtolower($filetype); if ($filetype=='jpg' || $filetype=='jpeg' || $filetype=='JPG' || $filetype=='JPEG') $src_img = imagecreatefromjpeg($name); if ($filetype=='png') $src_img = imagecreatefrompng($name); if($src_img===false) return false; $orig_w = imageSX($src_img); $orig_h = imageSY($src_img); $new_w = ($orig_w > $new_w) ? $new_w : $orig_w; $new_h = ($orig_h > $new_h) ? $new_h : $orig_h; $dst_img = imagecreatetruecolor($new_w,$new_h); if($dst_img===false) return false; if(imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,$orig_w,$orig_h)===false) return false; if ($filetype=='png'){ if(imagepng($dst_img,$filename)===false) return false; } else { if(imagejpeg($dst_img,$filename)===false) return false; } imagedestroy($dst_img); imagedestroy($src_img); return true; }
If you won't dress like the
Victoria Secret girls,
don't expect us to act like soap opera guys.
-
Apr 21, 2009, 21:15 #8
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Use var_dump() on you variables to make sure they have the value you expect them to have. For example, checking the value of $filetype would be a good idea.
Bookmarks