Jump to content

Image extension upload problem


rondog

Recommended Posts

I know how to upload images, but I am trying to check for the file type extension, and no matter what I do my function is returning false even if its jpg, jpeg, gif or png. Can you see anything I am doing wrong?

<?php
$uploaded_image = $_FILES['imgfield']['name'];
$name = $_POST['namefield'];
if(!$name || !$uploaded_image) {
    $error = "<span class=\"error\">Please fill in all fields.</a>";
} else {
    function open_image ($file) {
        // Get extension
        $extension = strrchr($file, '.');
        $extension = strtolower($extension);

        switch($extension) {
                case '.jpg':
                 $im = @imagecreatefromjpeg($file);
                        $image_type = ".jpg";
                        break;
                case '.jpeg':
                        $im = @imagecreatefromjpeg($file);
                        $image_type = ".jpeg";
                        break;
                case '.gif':
                        $im = @imagecreatefromgif($file);
                        $image_type = ".gif";
                        break;                        
                case '.png':
                        $im = @imagecreatefromgif($file);
                        $image_type = ".png";
                        break;

                default:
                        $im = false;
                        break;
        }
        return $im;
    }
    $image = open_image($uploaded_image);
    if ($image === false) {
        $error = "<span class=\"error\">error.$extension</a>";
    } else {
        $width = imagesx($image);
        $height = imagesy($image);
        $success = "<span class=\"success\">Successfully added.$width + $height</a>";
    }
?>

Link to comment
https://forums.phpfreaks.com/topic/103518-image-extension-upload-problem/
Share on other sites

$_FILES['imgfield']['name'] is not a file that exists. It is just the name of the file that you want to upload. So you cant create the image from a file that's not there :-/.

 

First you need to move_uploaded_file the file onto your server, then you can open it with your function.

 

            
                case '.png':
                        $im = @imagecreatefromgif($file);
                        $image_type = ".png";
                        break;

should be

            
                case '.png':
                        $im = @imagecreatefrompng($file);
                        $image_type = ".png";
                        break;

And you can shorten it to:


function open_image ($file) {
     // Get extension
     $extension = strrchr($file, '.');
     
     $extension = strtolower($extension);

     switch($extension) {
             case '.jpg':
             case '.jpeg':
                     $im = imagecreatefromjpeg($file);
                     $image_type = ".jpg";
                     break;
             case '.gif':
                     $im = imagecreatefromgif($file);
                     $image_type = ".gif";
                     break;
             case '.png':
                     $im = imagecreatefrompng($file);
                     $image_type = ".png";
                     break;

             default:
                     $im = false;
                     break;
     }
     return $im;
}

but if I do move_uploaded_file, isnt that uploading it? I dont want it to be uploaded unless its a specific size. That essentially what im trying to do.

 

$_FILES['imgfield']['name'] is not a file that exists. It is just the name of the file that you want to upload. So you cant create the image from a file that's not there :-/.

 

First you need to move_uploaded_file the file onto your server, then you can open it with your function.

 

            
                case '.png':
                        $im = @imagecreatefromgif($file);
                        $image_type = ".png";
                        break;

should be

            
                case '.png':
                        $im = @imagecreatefrompng($file);
                        $image_type = ".png";
                        break;

And you can shorten it to:


function open_image ($file) {
     // Get extension
     $extension = strrchr($file, '.');
     
     $extension = strtolower($extension);

     switch($extension) {
             case '.jpg':
             case '.jpeg':
                     $im = imagecreatefromjpeg($file);
                     $image_type = ".jpg";
                     break;
             case '.gif':
                     $im = imagecreatefromgif($file);
                     $image_type = ".gif";
                     break;
             case '.png':
                     $im = imagecreatefrompng($file);
                     $image_type = ".png";
                     break;

             default:
                     $im = false;
                     break;
     }
     return $im;
}

Oh sorry, I've lost my head.

 

You don't need to use move_uploaded_file.

 

$_FILES['imgfield']['name'] is the name of the file, but $_FILES['imgfield']['tmp_name'] is the actual file that does exist.

 

And no, move_uploaded_file doesn't upload the file. The file is already uploaded to the temporary directory, move_uploaded_file just moves and renames the temporary file so that it won't get deleted at the end of the script.

 

You could add a paramater to your function for the tmp_name.

 

<?php
function open_image ($file, $tmp) {
     // Get extension
     $extension = strrchr($file, '.');
     
     $extension = strtolower($extension);

     switch($extension) {
             case '.jpg':
             case '.jpeg':
                     $im = imagecreatefromjpeg($tmp);
                     $image_type = ".jpg";
                     break;
             case '.gif':
                     $im = imagecreatefromgif($tmp);
                     $image_type = ".gif";
                     break;
             case '.png':
                     $im = imagecreatefrompng($tmp);
                     $image_type = ".png";
                     break;

             default:
                     $im = false;
                     break;
     }
     return $im;
}
    $tempname = $_FILES['imgfield']['tmp_name'] ;
    $image = open_image($uploaded_image, $tempname);
?>

(not tested).

ohh ok I think I was just figuring the 'name' / 'tmp_name' out when you posted this. The server I am working on isnt letting me change the folder permissions to 777 through filezilla. It keeps reverting back to 666, so I cant even test this now..great  :o

 

thanks for the help.

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.