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
https://forums.phpfreaks.com/topic/81245-upload-problem/
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
https://forums.phpfreaks.com/topic/81245-upload-problem/#findComment-412412
Share on other sites

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.