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
https://forums.phpfreaks.com/topic/222944-image-extension-problem/
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';
}

?>

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();
}

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.

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

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.