tags:

views:

203

answers:

4

i have problem to check if the picture is jpg

if ($_FILES["fname"]["error"] > 0) {

      **$imgData = " hyperlink /holder.jpg";**
      }

 else {
   $imgData ="hyperlink/".$_FILES["fname"]["name"];

}

// only accept jpg images pjpeg is for Internet Explorer.. should be jpeg

   if (!($_FILES["fname"]["type"] == "image/pjpeg") ) {

      print "I only accept jpg files!";

      exit(0);
       }

when it goes to first statment in the first if statmenet (the one that is bold) it always give I only accept jpg files!

how can i fix it ?

thanks in advancee

+2  A: 

Not an answer, but I felt a comment was nec.

PHP has such good image-type support, i wonder why you are restricting your app. in just a couple lines of code you can deal with any input format and convert to jpeg, if that is a requirement...

$im = imagecreatefrompng(input_filename)
imagejpeg($im, output_filename);
Scott Evernden
+3  A: 

Try the exif_imagetype image function.

Example:

if(exif_imagetype($filepath) != IMAGETYPE_JPEG){
    echo 'Not a JPEG image';
}
MitMaro
A: 

When using $_FILES, you are relying on informations sent by the client, which is not the best thing to do (you've seen it's not always the same, and, if I remember correctly, $_FILES['...']['type'] can be faked)

If you are using PHP >= 5.3 (or can install PECL packages), maybe you can give a look to the extension Fileinfo. If you are using an older version, what about mime_content_type ?

And, as said by Scott, why allow only jpeg ?

Looking about the code better : when you are in the first case (error > 0), you are assigning a default file to $imgData ? Why the spaces arround "hyperlink" ? And why do you always use to check the content-type, even if there was an error a couple of lines before ?

To finish, did you have a look at the manual ( Handling file uploads ) ?

Pascal MARTIN
A: 

Why don't you try creating an array of exceptions (the files you want the user to be able to upload).

// Hyperlink for your website
$hyperlink = "http://www.yourwebsitehere.com";

if($_FILES['fname']['error'] > 0)
{
    $image= $hyperlink . "/holder.jpg";
} 
else
{
    $image = $hyperlink . "/" . $_FILES['fname']['name'];
}

// Only accept files of jpeg format
$exceptions = array("image/jpg", "image/jpeg", "image/pjpeg");

foreach($exceptions as $value)
{
    if($_FILES['fname']['type'] != $value)
    {
        echo "I only accept jpeg images!";
        break; // Or exit();
    }
}