Jump to content

Recommended Posts

I have a user registration script that allows the user to upload a photo.  My problem is that I need the photo to be either a JPG, a GIF, or a PNG.  I think I have the right idea of how to do this, but I can't get my syntax to work.  This is my code for the if statement if its in the correct file format:

 

$photoname = $_FILES['photo']['name'];
   $phototype = $_FILES['photo']['type'];

   if(!($phototype == "image/jpeg") || !($phototype == "image/gif") || !($phototype == "image/png")){
   
   		echo "The photo you have chosen to upload is in an incorrect file format.  Allowed formats are jpeg, gif, and png.  You chose a $phototype.";
		$errors = $errors + 1;

}

 

And this is my code for moving the photo:

 copy( $_FILES['file']['name'], "/photos" ) or 
           die( "There was an error uploading your photo.  Please try again later.");

 

I get the error messages that I had echoed if there were an error.  This is it:

The photo you have chosen to upload is in an incorrect file format. Allowed formats are jpeg, gif, and png. You chose a .There was an error uploading your photo. Please try again later.

 

Thanks for any help.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/71093-registration-script-with-photo-upload/
Share on other sites

try this logic

if( ($phototype != "image/jpeg") && ($phototype != "image/gif") && ($phototype != "image/png") )
{

 

also it looks like the type isn't being set, can you post the form..

check the name is "photo" on the input type='file'

i don't like using image/jped or whatever else sometimes it bring problems what you can so is

$fileName = basename($_FILE['file']['name']);
$extension = strtolower(substr($fileName,-4));

switch($extension){

case '.jpg':
  echo 'good';
  break;
case '.gif':
  echo 'good';
  break;
case '.png':
  echo 'good';
  break;
default:
  echo "The photo you have chosen to upload is in an incorrect file format.  Allowed formats are jpeg, gif, and png.  You chose a $phototype.";
}

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.