Link to home
Start Free TrialLog in
Avatar of pauledwardian
pauledwardian

asked on

PHP Recognize File Extension

I have this code that would find the extention and regnizes if the file is an image and then moves the selected file. But, it keeps failing when it tries to move a file. Also, it keeps giving an error about the $file = $_FILES['myFileUpload']; on line 4 that is not defined!!!
Please Help!!!!

Paul

<?php

$message = "";
$file = $_FILES['myFileUpload'];
$file_name = $file['name'];
$error = ''; // Empty
$ext = strtolower(substr(strrchr($file_name, "."), 1));
if(isset($_POST['postBack']))
      
{      
      
      if($_FILES['myFileUpload']['error'] > 0)
      {
            $message = "An error occurred $_FILES[myFileUplaod]";
      }
      else
      {
            
            $newfile = './uploads/' . $_FILES['myFileUpload']['name'];

            
            if($ext == 'jpg' || $ext == 'gif' || $ext == 'png')
            {
      
                  if(is_uploaded_file($_FILES['myFileUpload']['tmp_name']))                  
                  {
            
                        if(move_uploaded_file($_FILES['myFileUpload']['tmp_name'],$newfile))
                        {
                  
                              $message = "File $_FILES[myFileUpload][name] uploaded successfully";
                        }
                        else
                        {
                              $message = "File move failed";
                        }
                  }
                  else
                  {
                        $message = "Is_Uploaded_File returned false";
                  }
            }
            else
            {
                  $message = "Invalid file type";
            }
      }      
}
?>
<html>
<head>
      <title>Uploading files</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>">
      <input name="myFileUpload" type="file">
      <input name="postBack" type="hidden" value="yes"><br/>
      <input type="submit" value="Upload" />
</form>
<?php
echo "<h2>$message</h2>";
?>
</body>
</html>
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Put this

print_r($_FILES);

as the second line in you PHP page to see what you are actually getting.
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pauledwardian
pauledwardian

ASKER

Thanks!
You're welcome!