We help IT Professionals succeed at work.

PHP Recognize File Extension

pauledwardian
pauledwardian used Ask the Experts™
on
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>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Put this

print_r($_FILES);

as the second line in you PHP page to see what you are actually getting.
Fixer of Problems
Most Valuable Expert 2014
Commented:
I borrowed code from this page http://www.w3schools.com/php/php_file_upload.asp to try to do what it appears you are trying to do and it seems to work.

The upload form:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP File Upload</title>
</head>
<body>
<h1>PHP File Upload</h1>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="myFileUpload" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

Open in new window

And the PHP code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
print_r($_FILES);
$tfile = $_FILES["myFileUpload"];
if ($_FILES["myFileUpload"]["error"] > 0)
  {
  echo "Error: " . $_FILES["myFileUpload"]["error"] . "<br />";
  }
else
  {
	echo "<br /><br />";
  echo "Upload: " . $_FILES["myFileUpload"]["name"] . "<br />";
  echo "Type: " . $_FILES["myFileUpload"]["type"] . "<br />";
  echo "Size: " . ($_FILES["myFileUpload"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["myFileUpload"]["tmp_name"] . "<br /><br />";
  echo "tUpload: " . $tfile["name"] . "<br />";
  echo "tType: " . $tfile["type"] . "<br />";
  echo "tSize: " . ($tfile["size"] / 1024) . " Kb<br />";
  echo "tStored in: " . $tfile["tmp_name"];
  }

?> 
</body>
</html>

Open in new window

Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
The error is because PHP is finding $file = $_FILES['myFileUpload']; on the page before the form is even submitted.  At that point, $_FILES['myFileUpload'] does not yet exist and it won't until after the form is submitted.

Author

Commented:
Thanks!
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
You're welcome!