I'm following this tutorial on this About.com page to create a simple file upload option for my site. http://php.about.com/od/advancedphp/ss/php_file_upload_4.htm

I got the script to work, but when I try to limit the file type it does not work.

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

//This is our size condition
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}

//This is our limit file type condition
if ($uploaded_type != "image/gif") {
echo "You may only upload GIF files.
";
$ok=0;
}

//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}

//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ".
basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>

It will not upload any files including GIFs. My ultimate goal here is to just make it so the script only allows image files to be uploaded.

Could you please help me make it so only image files can be uploaded (GIF, JPG, JPEG, BMP etc)

Thanks,
Noah

    Uh, where do you ever set the value of $uploaded_size?

      Do you mean this?

      //This is our size condition
      if ($uploaded_size > 350000)
      {
      echo "Your file is too large.<br>";
      $ok=0;
      }

      I don't have any coding experience, so I'm not sure what you mean?

      I've been really banging my head against the wall here trying to get this simple thing to work.

      I just want it to upload image files and nothing else.

      Thanks for your help.

        he means

        //This is our size condition
        if ($uploaded_size > 350000)
        {
        echo "Your file is too large.<br>";
        $ok=0;
        }
        
        
        

        needs this first

        $uploaded_size=$_FILES['uploadedfile']['file_size']; 
        
        //This is our size condition
        if ($uploaded_size > 350000)
        {
        echo "Your file is too large.<br>";
        $ok=0;
        }
        

        you must define a variable before using it

        meaning

        echo $blah;
        will give me and error without

        $blah = "hey";
        echo $blah;

          Ok thanks, I added that line of code to define that variable.

          How would I make it so only image files can be uploaded? GIF, JPG, JPEG, BMP etc)

          Thanks again,
          Noah

            well, the simples but not the safes way is to check file extensions

            So something like

            $Name=$_FILES['uploaded']['name']; 
            
            if (preg_match("/\.gif$/", $Name)) echo " this is a gif"; 
            else if (preg_match("/\.jpg$/", $Name)) echo " this is a jpg"; 
            else if (preg_match("/\.jpeg$/", $Name)) echo " this is a jpeg"; 
            else if (preg_match("/\.bmp$/", $Name)) echo " this is a bmp"; 
            

            something to this effect, as a beginner, regular expression and pattern matching might be out of your reach right now.

            $_FILE["name"]["type"] isnt very reliable

            doing somthing as simple as just reading the last 4 characters of a name might work, like

            $Ext=substr($Name, 0, -3);
            if ($Ext=="gif") echo "this is a gif";

            let me know if you need clarification on anything I said

              thanks again for all your help.

              ok, so added This code:

              $Name=$_FILES['uploaded']['name'];
              
              if (preg_match("/\.gif$/", $Name)) echo " this is a gif";
              else if (preg_match("/\.jpg$/", $Name)) echo " this is a jpg";
              else if (preg_match("/\.jpeg$/", $Name)) echo " this is a jpeg";
              else if (preg_match("/\.bmp$/", $Name)) echo " this is a bmp"; 

              But it is still uploading every file though; its not preventing any from being uploaded. I would like to make it so only the formats above are allowed to be uploaded.

              Thanks for taking the time with me. Below is the entire upload.php file.

              <?php
              $target = "upload/";
              $target = $target . basename( $_FILES['uploaded']['name']) ;
              $ok=1;
              
              $uploaded_size=$_FILES['uploadedfile']['file_size']; 
              //This is our size condition
              if ($uploaded_size > 350000)
              {
              echo "Your file is too large.<br>";
              $ok=0;
              }
              
              //This is our limit file type condition
              $Name=$_FILES['uploaded']['name'];
              
              if (preg_match("/\.gif$/", $Name)) echo " this is a gif";
              else if (preg_match("/\.jpg$/", $Name)) echo " this is a jpg";
              else if (preg_match("/\.jpeg$/", $Name)) echo " this is a jpeg";
              else if (preg_match("/\.bmp$/", $Name)) echo " this is a bmp"; 
              
              //Here we check that $ok was not set to 0 by an error
              if ($ok==0)
              {
              Echo "Sorry your file was not uploaded";
              }
              
              //If everything is ok we try to upload it
              else
              {
              if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
              {
              echo "The file ".
              basename( $_FILES['uploadedfile']['name']). " has been uploaded";
              }
              else
              {
              echo "Sorry, there was a problem uploading your file.";
              }
              }
              ?>

                ok, Copy paste this code, I've taken your funciton and made some modification. read the commetns they will help explain what is happening.

                Just make sure the the directory you are uploading to Exists and has write priviledges

                EDIT:

                For got to say why the code before didnt work

                That was just an example, it didnt do anything other then compare strings to a regular expression.

                Basically, programming comes down to... if you dont tell it to do something, it wont do anything. That may seem overly simple, but as you advance you'll find that programming is less to do with code and more to do in how you think of things

                <?PHP
                	// an array of allowed image extensions
                	$Allowed[]=".gif";
                	$Allowed[]=".jpeg";
                	$Allowed[]=".jpg";
                	$Allowed[]=".bmp";
                
                
                //// defining a function
                function CheckImage($name, $allowedarray){
                	   //// get the last period in the name, so a name like my.image.gif woudl work
                	   //// striipos is a case insensitive functiont that returns the last occurance of a needle in a string
                	   $pos=strripos($name, ".");
                
                	   /// get the characters that come after the last period
                	   // substr returns the characters in a string given it's parameters
                	   /// so i'm starting at $pos and going to the end of the name
                	   /// strlen returns the amount of charachters in a string
                	   $ext=substr($name, $pos, strlen($name));
                
                	   /// now we are goign to search the array, and check if it matches one of ours
                	   $find = array_search($ext, $allowedarray);
                
                	   /// if it was found $find will contain the key that holds the extension, so we return the array and key
                	   /// so we return that value, otherwise return false;
                	   if ($find !== false) return $allowedarray[$find];
                	   else return false;
                
                }
                
                /// this function will check to see if a file exists and if so, add a random number to the beging and return a full target path
                function randname($target){
                	/// check to see if the file exists
                	if (is_file($target))
                	{
                		/// if so cicle through a rand until there is no file with the name; 
                		/// first i'm going to seperate the directories from the name; 
                		$name=basename($target); 
                		//// if I erase the filename from the target I get the directories
                		$dirs=str_replace($name, "", $target); 
                		//// now I get a 4 digit number; 
                		$rand=rand(1000, 9999); 
                
                		while(is_file($dirs.$rand."_".$name)) 
                		{
                			$rand=rand(1000, 9999); 
                		}
                		/// now I have a new name, so I return the new name
                		return $dirs.$rand."_".$name;
                	}
                	/// if the file isnt already set, return the same path
                	else return $target; 
                
                
                }
                /// check to see if any file uploaded
                if ((isset($_FILES['uploaded'])) && (is_file($_FILES['uploaded']['tmp_name']))) 
                {
                	$target = "upload/";
                	$name=basename($_FILES['uploaded']['name']); 
                	$target = $target . $name ;
                	$ok=1;
                
                	$uploaded_size=$_FILES['uploaded']['size']; 
                	//This is our size condition
                	if ($uploaded_size > 350000)
                	{
                		echo "Your file is too large.<br>";
                		$ok=0;
                	}
                
                	//This is our limit file type condition
                	//// HERE I'm calling the function created, the function will return false if no match is found
                	/// else it will return the type of extension it matched
                	/// the function call for 2 variables, 1 being the name beign examined, 
                	//// the other the Array (an array is a variable that holds more then one set of information) 
                	$type=CheckImage($name, $Allowed); 
                	if ($type == false) {
                		echo "Your file must be either a gif, jpg, jpeg, or bmp<br>"; 
                		$ok=0; 
                	}
                
                	//Here we check that $ok was not set to 0 by an error
                	if ($ok==0)
                	{
                		Echo "Sorry your file was not uploaded<br>";
                	}
                
                	//If everything is ok we try to upload it
                	else
                	{
                
                	/// seetting up a rand number just in case
                	//// I always am careful not to replace files already on the server
                	//// so I do an loop to give it a single random number
                	///  if you choose to do this uncomment the next line, meaning delete the /// in front of it
                	//// $target=randname($target); 
                
                		if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
                		{
                			echo "The file ".
                			basename( $_FILES['uploadedfile']['name']). " has been uploaded";
                		}
                		else
                		{
                			echo "Sorry, there was a problem uploading your file.";
                		}
                	}
                }
                
                ?>
                
                  Write a Reply...