Jump to content

[SOLVED] image upload script


adam291086

Recommended Posts

I have this upload script that uploads an image and resizes it. At the moment it keeps echoing out

upload not successful
. I have echoed out the information where the file is store temporarily and all is present and correct. Any ideas?

<?php

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

     /*== upload directory where the file will be stored 
          relative to where script is run ==*/
    
$dir = dirname(__FILE__)."/pictures/";
$path= $dir;


    /*== 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] > 250) || ($imgsize[1] > 200)) 
    {
        $tmpimg = tempnam("/tmp", "MKUP");

        
        /*== 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 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");

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

    }

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

       /*== move file to proper directory ==*/

if(move_uploaded_file($final_filename, $path))
    {
      echo "Magazine Updated!";
    }
else
{
echo "Upload not Successful";
unlink(@$imgfile);

}
}
}
?>

 

Function

<?php
    /*== FUNCTIONS ==*/

    function getFileExtension($str) {

        $i = strrpos($str,".");
        if (!$i) { return ""; }

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

        return $ext;
}

Link to comment
Share on other sites

ok, i have change the script to use copy instead

 

<?php

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

     /*== upload directory where the file will be stored 
          relative to where script is run ==*/
    
$dir = dirname(__FILE__)."/pictures/";
$path= $dir;


    /*== 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] > 250) || ($imgsize[1] > 200)) 
    {
        $tmpimg = tempnam("/tmp", "MKUP");

        
        /*== 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 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$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"))
    {
echo "Upload not Successful";
unlink(@$imgfile);
      
    }
else
{
echo "Magazine Updated!";
}
}
}

 

I am still getting the same problem

Link to comment
Share on other sites

ok, all of a sudden my upload script has stopped working. The page loads with no error. I echo out the variable and they are present. But nothing is upload to my directory and the success message isn't displayed. Anyone know why?

 

<?php

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

     /*== upload directory where the file will be stored 
          relative to where script is run ==*/
    
$dir = dirname(__FILE__)."/pictures/";
$path= $dir;


    /*== 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] > 250) || ($imgsize[1] > 200)) 
    {
        $tmpimg = tempnam("/tmp", "MKUP");

        
        /*== 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 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$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 = $path . "/$final_filename";	
    
    /*== do extra security check to prevent malicious abuse==*/
    if (is_uploaded_file($imgfile))
    {

       /*== move file to proper directory ==*/

if (!copy($imgfile,"$newfile"))
    {
echo "Upload not Successful";
unlink(@$imgfile);
      
    }
else
{
echo "Magazine Updated!";
}
}
}

?>

<?php
    /*== FUNCTIONS ==*/

    function getFileExtension($str) {

        $i = strrpos($str,".");
        if (!$i) { return ""; }

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

        return $ext;
}
?>

Link to comment
Share on other sites

ok sort of got it work.

 

  if ($pext ! == 'jpg' || $pext !== 'jpeg'){

 

I want to say when $pect doesn't = jpg but at the moment i keep getting the error

 

Parse error: parse error, unexpected '!' in /homepages/12/d214897219/htdocs/adam/cycling/admin/upload/uploadimage.php on line 15

 

line 15 being the code above

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.