Jump to content

Excluding non-JPEG images


Leao

Recommended Posts

Hi,

I need a PHP script that excludes images sent from a form that aren't JPEGs. I originally tried this PHP script:

[code=php:0]
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']);
$ok=1;

if ($uploaded_type != 'image/jpeg') {
{
echo "Sorry your file needs to be a JPEG image.";
$ok=0;
}

if ($ok==0)
{
echo "Sorry your file was not uploaded";
}

else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], 'images/title.jpg'))
{
echo "Your image has been uploaded.";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
?>
[/code]

This script worked perfectly on most computers, but on those that hide file extensions it rejected even valid JPEG images. I tried the getimagesize() method below too as an alternative means of rejecting non JPEG files. It doesn't work either, can you help? Thanks – Leao

[code=php:0]
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']);
$ok=1;

list($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($_FILES['uploaded']['tmp_name']) ;

if ($ImportMimeType != 'image/jpeg')
{
echo "Sorry your file needs to be a JPEG image.";
$ok=0;
}

if ($ok==0)
{
echo "Sorry your file was not uploaded";
}

else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], 'images/title.jpg'))
{
echo "Your image has been uploaded.";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
?>
[/code]
Link to comment
Share on other sites

This might help you, http://php.robm.me.uk/ . Scroll down to the "file uploads" section. Here is a snippet of code from that site:
[code]
$validMimes = array(
    'image/png' => '.png',
    'image/x-png' => '.png',
    'image/gif' => '.gif',
    'image/jpeg' => '.jpg',
    'image/pjpeg' => '.jpg'
);

$image = $_FILES['image'];

if(!array_key_exists($image['type'], $validMimes)) {
    die('Sorry, but the file type you tried to upload is invalid; only images are allowed.');
}

// Get the filename minus the file extension:
$filename = substr($image['name'], 0, strrpos($image['name'], '.'));

// Append the appropriate extension
$filename .= $validMimes[$image['type']];

// Do something with the uploaded file
[/code]
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.