yan
yan

Reputation: 480

PHP. Upload .gif file to server

My assignment requires me to let users upload .gif file extensions to a folder, and display them after they upload it.

I got the form and processing page, but I cant seem to get them to work. I set the permissions on the directory to rwxrwxrwx to test it out.

Upload file code:

<html>
  <head>
<title>Assignment 7 Part II -- Art Gallery</title>
  </head>
  <body>
    <form method="post" action="a7a2.php" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="50000">
    Your file: <input type="file" name="uploadFile" /><br />
    <input type="submit" value="Upload it">
  </form>
</body>
</html>

Code:

<html>
      <head>
        <title>Assignment 7 Part II -- Art Gallery Results</title>
      </head>
      <body>

      <?php
      $uploadDir = 'images/';
      $uploadFileDir = $uploadDir . basename($_FILES['uploadFile']['name']);                                                   
      if(isset($_FILES['uploadFile'])) {
          if($_FILES['uploadFile']['error'] != UPLOAD_ERR_OK ||
          $_FILES['uploadFile']['mime'] != "image/gif") {
          print "<p>File not uploaded successfully!</p>";
          print "<p>Please make sure the file has the correct file extension: .gif</p>"; 
          print "<p><a href='a7p2.php'>Try uploading again</a>";
          var_dump($_FILES['uploadFile']['error']);
          die;
          } else {
          move_uploaded_file($_FILES['uploadFile']['name'], $uploadFileDir) or die("Can't move file to $uploadFileDir");   
          print "<p>Success!</p>";
          }
      }

      $uploadedFiles = glob("/students/ryan/php/images/*.gif");
      if($uploadedFiles != false) {
          print "<p>Here are pictures from the art gallery: </p>";
          foreach($uploadedFiles as $file) {
          $url = "http://hills.ccsf.edu/~ryan/images/" . substr($file, strrpos($file, '/') + 1);
          print "<p><img src=\"$url\"></p>";
          }
      } else {
          print "There are no pictures in the art gallery";
      }
      ?>

      </body>
    </html>

or same code, link to the code below:

http://pastebin.com/JNxwy323

Upvotes: 0

Views: 5512

Answers (2)

Yeak
Yeak

Reputation: 2538

You need to use tmp_name instead of name for the move_uploaded_file it should be

 move_uploaded_file($_FILES['uploadFile']['tmp_name'], $uploadFileDir) or die("Can't move file to $uploadFileDir");

also instead of $_FILES['uploadFile']['mime'] != "image/gif") {

it should be $_FILES['uploadFile']['type'] != "image/gif") {

Upvotes: -1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

It is not safe to rely on the MIME type that the browser tells you the file is. Instead, you should check to see if the file is what it is supposed to be. In this case:

if( !@imagecreatefromgif($_FILES['uploadFile']['tmp_name'])) {
    // file is not a valid GIF image
}

Upvotes: 3

Related Questions