Jump to content

Upload and resize


supanoob

Recommended Posts

When i use the following script to upload and resize an image, it throws out errors (see after script)

<?php 
$image = $_FILES['image']['name'];

    
    $uploaddir = "./images/$gender/";
    
    $pext = getExtension($image);
    $pext = strtolower($pext);
    if (($pext != "jpg")  && ($pext != "jpeg") && ($pext != "gif"))
    {
        print "<h1>ERROR</h1>Image Extension Unknown.<br>";
        print "<p>Please upload only an image with the extension .jpg or .jpeg or .gif ONLY<br><br>";
        print "The file you uploaded had the following extension: $pext</p>\n";

        unlink($imgfile);
        exit();
    }


    $imgsize = GetImageSize($image);

    /*== check size  0=width, 1=height ==*/
    if (($imgsize[0] > 460) || ($imgsize[1] > 345)) 
    {
        $tmpimg = tempnam("/tmp", "MKUP");

        system("djpeg $image >$tmpimg");
        
        system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");

        unlink($tmpimg);

    }

$rand = rand(0,9999999);
$date = DATE("d.m.y");
$image_name=$date.$id.$rand.'.'.$extension;
$final_filename = str_replace(" ", "_", $image_name);
$newfile = $uploaddir . "/$final_filename";
    
    if (is_uploaded_file($image))
    {

       if (!copy($imgfile,"$newfile")) 
       {
          print "Error Uploading File.";
          exit();
       }
     }
    unlink($image);

    
    print("<img src=\"$newfile\">");
?>

 

errors:

Warning: getimagesize(®Photo-0027.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /home/bumwarsc/public_html/images.php on line 77

 

Warning: unlink(®Photo-0027.jpg) [function.unlink]: No such file or directory in /home/bumwarsc/public_html/images.php on line 126

 

Link to comment
Share on other sites

i meant .. removing the Slash ( / ) at the end.. if you are receiving "No such file or directory" error.. that only comes up when path is wrong. I faced same problem while ago, removing slash at the end worked great for me ;)

 

That didnt work, the pictures are uploading, it appears to be the temp and resizing that doesnt work :/

Link to comment
Share on other sites

Hi there,

<?php 
$image = $_FILES['image']['name'];
$ext = strtolower($_FILES['image']['type']);
$uploaddir = "./images/$gender/";

if (($ext != "image/jpg") || ($ext != "image/jpeg") || ($ext != "image/gif")){

 

This makes your script a little more secure, as this will help against people masking exe or the like when uploading, as this is the *actual file type* and not just the one after the full stop...

 

Also, copy() and rename() technically do the same thing: See this

 

And your filetype evaluator needs to be logical or not and, because what you are evaluating is not amounting to the sum of all conditions...

 

Rw

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.