I have this script that I am using that work to upload a file and to make sure the file is either jpg, jpeg, or gif. However, I want to restrict the user with the image width no larger than 150px. Is there an easy way to implement in the code I already have below?

if(isset($_POST['imageInsert']))
{
$current_image=$_FILES['image']['name'];
$imageextension = substr(strrchr($current_image, '.'), 1);
if (($imageextension!= "jpg") && ($imageextension != "jpeg") && ($imageextension != "gif")) 
{
die('Unknown extension. Only jpg, jpeg, and gif files are allowed. Please hit the back button on your browser and try again.');
}
$time = date("fYhis");
$imagecaption = $_POST["imagecaption"];
$new_image = $imagecaption . "." . $imageextension;
$destination="../logos/".$current_image;
$action = copy($_FILES['image']['tmp_name'], $destination);
if (!$action) 
{
die('File copy failed. Please hit the back button.');
}else{

After the else it is just MySQL code to insert the image into the database providing the extension is correct.

Any suggestions or tips? Thanks!

Member Avatar

TechySafi

This simple code wont let u put a image wider than 150px.

$sizearray = getimagesize("img_name.jpg");
    $width= $size[0];
if($width>150)
{
echo "its wider than 150px";
}

I tried implementing it but perhaps I implemented it in the wrong area. Can you let me know where to put it in the code that I have posted? Thanks!

Member Avatar

TechySafi

if(isset($_POST['imageInsert']))
{
$current_image=$_FILES['image']['name'];
$imageextension = substr(strrchr($current_image, '.'), 1);
$sizearray = getimagesize('$current_image'); //seems we have a var to retrieve the name
$width= $size[0];
if (($imageextension!= "jpg") or ($imageextension != "jpeg") or ($imageextension != "gif")) //changed to OR 
{
die('Unknown extension. Only jpg, jpeg, and gif files are allowed. Please hit the back button on your browser and try again.'); //if this test is passed
}
elseif($width>150) //now check the width....
{
die ('its wider than 150px');
}
else //everythings fine...now proceed, i put an extra "}" at the end for this extra else condition (it wasnt in ur code)
{
$time = date("fYhis");
$imagecaption = $_POST["imagecaption"];
$new_image = $imagecaption . "." . $imageextension;
$destination="../logos/".$current_image;
$action = copy($_FILES['image']['tmp_name'], $destination);
if (!$action) 
{
die('File copy failed. Please hit the back button.');
}
else{
}
}

That should work fine...does it?

Still does not work. Well, the file works but I can still upload an image with a width greater than 150. It seems to ignore the width part. However, if I try to upload a PNG for example, that part works. Just not the width part. Seems to be ignored.

I have gone back and tried your code again. The image is uploading to the server fine but now I keep getting an error:

Warning: getimagesize($current_image) [function.getimagesize]: failed to open stream: No such file or directory

if(isset($_POST['imageInsert']))
{
$current_image=$_FILES['image']['name'];
$imageextension = substr(strrchr($current_image, '.'), 1);
echo $sizearray = getimagesize('$current_image'); //seems we have a var to retrieve the name
$width= $size[0];
if (($imageextension!= "jpg") or ($imageextension != "jpeg") or ($imageextension != "gif")) //changed to OR 
{
die('Unknown extension. Only jpg, jpeg, and gif files are allowed. Please hit the back button on your browser and try again.'); //if this test is passed
}
elseif($width>150) //now check the width....
{
die ('its wider than 150px');
}
else //everythings fine...now proceed, i put an extra "}" at the end for this extra else condition (it wasnt in ur code)
{
$time = date("fYhis");
$imagecaption = $_POST["imagecaption"];
$new_image = $imagecaption . "." . $imageextension;
$destination="../logos/".$current_image;
$action = copy($_FILES['image']['tmp_name'], $destination);
if (!$action) 
{
die('File copy failed. Please hit the back button.');
}
else{