Jump to content

Upload and Resize to folder


phpbeginner

Recommended Posts

What I have here is an image upload and resize form. I do not want to maintain aspect ratio as I am using this in a slideshow, so I want to maintain exact width and height with all images. The problem with the script is that it outputs that the file has been uploaded successfully but upon checking the filename is there but its at 0 bytes ! Can someone tell me why this is happening ?

 

<?php

if ($_SERVER['REQUEST_METHOD'] == "POST")
{

    /* SUBMITTED INFORMATION - use what you need
     * temporary filename (pointer): $imgfile
     * original filename           : $imgfile_name
     * size of uploaded file       : $imgfile_size
     * mime-type of uploaded file  : $imgfile_type
     */

     /*== upload directory where the file will be stored 
          relative to where script is run ==*/
    
    $uploaddir = "slideshow/";
    

    /*== get file extension (fn at bottom of script) ==*/
    /*== checks to see if image file, if not do not allow upload ==*/
    $pext = getFileExtension($imgfile_name);
    $pext = strtolower($pext);
    if (($pext != "jpg")  && ($pext != "jpeg"))
    {
        print "<h1>ERROR</h1>Image Extension Unknown.<br>";
        print "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>";
        print "The file you uploaded had the following extension: $pext</p>\n";

        /*== delete uploaded file ==*/
        unlink($imgfile);
        exit();
    }


    //-- RE-SIZING UPLOADED IMAGE

    /*== only resize if the image is larger than 250 x 200 ==*/
    $imgsize = GetImageSize($imgfile);

    /*== check size  0=width, 1=height ==*/
    if (($imgsize[0] > 292)) 
    {
        /*== temp image file -- use "tempnam()" to generate the temp
             file name. This is done so if multiple people access the 
            script at once they won't ruin each other's temp file ==*/
        $tmpimg = tempnam("/tmp", "MKUP");

        /*== RESIZE PROCESS
             1. decompress jpeg image to pnm file (a raw image type) 
             2. scale pnm image
             3. compress pnm file to jpeg image
        ==*/
        
        /*== Step 1: djpeg decompresses jpeg to pnm ==*/
        system("djpeg $imgfile >$tmpimg");
        

        /*== Steps 2&3: scale image using pnmscale and then
             pipe into cjpeg to output jpeg file ==*/
        system("pnmscale -xy 292 196 $tmpimg | cjpeg -smoo 10 -qual 80 >$imgfile");

        /*== remove temp image ==*/
        unlink($tmpimg);

    }

    /*== setup final file location and name ==*/
    /*== change spaces to underscores in filename  ==*/
    $final_filename = str_replace(" ", "_", $imgfile_name);
    $newfile = $uploaddir . "$final_filename";
    
    /*== do extra security check to prevent malicious abuse==*/
    if (is_uploaded_file($imgfile))
    {

       /*== move file to proper directory ==*/
       if (!copy($imgfile,"$newfile")) 
       {
          /*== if an error occurs the file could not
               be written, read or possibly does not exist ==*/
          print "Error Uploading File.";
          exit();
       }
     }

    /*== delete the temporary uploaded file ==*/
    unlink($imgfile);

    
    print("$final_filename uploaded successfully");

    /*== DO WHATEVER ELSE YOU WANT
         SUCH AS INSERT DATA INTO A DATABASE  ==*/

}
?>

 

Here is the form

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="50000">

    <p>Upload Image: <input type="file" name="imgfile"><br>
    <font size="1">Click browse to upload a local file</font><br>
    <br>
    <input type="submit" value="Upload Image">
    </form>

 

On images that do not require resizing, the image is uploaded successfully but files that require resizing upload at 0 bytes.

 

Any help with the above code or solution is very much appreciated !

Link to comment
https://forums.phpfreaks.com/topic/42690-upload-and-resize-to-folder/
Share on other sites

Ok I read this in another thread and then the guy finally got a reply, so I am going to give it a whirl and see if it helps........

 

Come on guys, can someone help or is this like webdev forums ? And then amazingly he got a response, so we'll see if it works for me.  ;D

I can't seem to find the line where you define $imgfile_name, which means this $final_filename = str_replace(" ", "_", $imgfile_name); will set $final_filename to "" and then $newfile will become "slideshow/" which in the end means your filename will be ""....

It's defining the file name as it should and the file goes to the slideshow folder. For undersized or exact size files it is working fine with the filename appearing in the slideshow folder and showing up as correct file size.

 

The problem is an oversized image which is needing to be resized. The filename still appears in the slideshow folder but is 0 bytes.

 

Again, the only problem I am having with this is the files that use this

 

if (($imgsize[0] > 292))

 

and filename is appearing but 0 bytes. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.