Jump to content

upload problem


adam291086

Recommended Posts

I have an upload script sort of working. One file always uploads anything else wont. Can anyone tell me why

 

<?php
error_reporting(E_ALL);


if ($_SERVER['REQUEST_METHOD'] == "POST")
{
  	$pext = getFileExtension($imgfile_name);
    $pext = strtolower($pext);
      if ($pext == 'jpg' || $pext == 'jpeg' || $pext == 'png' || $pext == 'PNG' || $pext == 'gif' || $pext == 'GIF'){

//big pic
$smallpath= dirname(__FILE__)."/pictures/picture";
  $smallfinal_filename = str_replace(" ", "_", $imgfile_name);
    $newfile = $smallpath ."/$smallfinal_filename";
    
    if (is_uploaded_file($imgfile))
    {


       if (!copy($imgfile,"$newfile")) 
       {
     
          echo "Error Uploading File.";
          @unlink($imgfile);
       }
}


//small picture 

$path= dirname(__FILE__)."/pictures/thumbnail";

//-- RE-SIZING UPLOADED IMAGE

   
    $imgsize = GetImageSize($imgfile);
if (($imgsize[0] > 100) || ($imgsize[1] > 100)) 
    {
        $tmpimg = tempnam("/tmp", "MKUP");
        system("djpeg $imgfile >$tmpimg");     
        system("pnmscale -xy 100 100 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");
        unlink($tmpimg);

    }

    $final_filename = str_replace(" ", "_", $imgfile_name);
    $newfile = $path ."/$final_filename";
    
    if (is_uploaded_file($imgfile))
    {


       if (!copy($imgfile,"$newfile")) 
       {
     
          echo "Error Uploading File.";
          @unlink($imgfile);
       }
else
{
echo "success";
}
     }
}
else
{
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";
        	unlink($imgfile);
        	exit();
}
}

?>
<html>
</head>
<body bgcolor="#FFFFFF">

    <h2>Upload and Resize an Image</h2>

    <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>

</body>
</html>

<?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

Your code has two basic problems. It is dependent on register globals being on and it is not checking the $_FILES['imgfile']['error'] element to even make sure that the upload worked before accessing the uploaded file information.

 

Reading this will help - http://www.php.net/manual/en/features.file-upload.php

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.