Jump to content

image extension problem


merylvingien

Recommended Posts

Hi folks, just upgrading one of my sites with a new script which i thought i had working, well it did at least on my local server.

 

Its an image upload script.

It fails at the get image extension part, which when echoed out is blank. Which is why its failing.

 

Can anyone see what i have missed?

 

if($_POST){
$image =$_FILES["image"]["name"];
$img = $_FILES['image']['tmp_name'];
$size = filesize($_FILES["image"]["tmp_name"]);
$maxsize= 400;
if(isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH']>409600){
echo 'Upload FAILED, file is too large !';
exit();
}
if ($size > $maxsize*1024){
echo "File too big";
exit();
} 
else
{
if(file_exists($existingimage)){
    unlink($existingimage);
    } 
function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

$extension = getExtension($image);
		$extension = strtolower($extension);

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
		{
	  echo "The extension is:" . $extension ." ";
			echo'Unknown Image extension';
exit();
		}

 

Ive ended the script here at the part where it fails to reduce the amount of uneeded code to sift through.

It fails no matter what type of image is used to upload.

Link to comment
Share on other sites

I'd do the extension part like this:

 

<?php

function getExtension($filename)
{
$out = false;

$pos = strrpos($filename, '.');

if($pos !== false && $pos > 0)
{
	// filename has a . and the . is not the first char
	$out = substr($filename, $pos + 1);
}

return $out;
}


$file = 'test.jpg';

$allowed = array(
'jpg',
'png',
'gif',
'bmp'
);

$ext = getExtension($file);

if($ext !== false)
{
// it's a valid extension, does it match a known one?

if(in_array(strtolower($ext), $allowed))
{
	// $ext is one of the allowed extensions
	echo 'allowed';
}
else
{
	// $ext is valid but is not in the allowed list
	echo 'valid but no allowed';
}
}
else
{
// extension is not valid
echo 'not valid';
}

?>

Link to comment
Share on other sites

You can also do like this.

 

$file_type = $_FILES['image']["type"];

if ($file_type != "image/jpg" || $file_type != "image/jpeg" || $file_type != "image/pjpg" || $file_type != "image/gif" || $file_type != "image/png" || $file_type != "image/bmp" || $file_type != "image/x-icon") {
echo "$file_type not allowed ";
exit();
}

Link to comment
Share on other sites

You can also do like this.

 

$file_type = $_FILES['image']["type"];

if ($file_type != "image/jpg" || $file_type != "image/jpeg" || $file_type != "image/pjpg" || $file_type != "image/gif" || $file_type != "image/png" || $file_type != "image/bmp" || $file_type != "image/x-icon") {
echo "$file_type not allowed ";
exit();
}

 

Thanks for that, i tried it, but when trying to upload a jpg image its still says not allowed. It wont even give me the jpg extension on the front of the error.

 

requinix not sure how to impliment your answer.

Link to comment
Share on other sites

Thanks for the reply, ok the result from that is:

 

Array

(

    [photo] => Array

        (

            [name] => _MG_4145.jpg

            [type] => image/jpeg

            [tmp_name] => /tmp/phpUwG3fN

            [error] => 0

            => 27055

        )

 

)

 

It all looks ok to me...

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.