dongnan1899 2012-10-21 23:19
浏览 105

PHP。 将.gif文件上传到服务器

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

  • 写回答

2条回答 默认 最新

  • dongyan8896 2012-10-21 23:26
    关注

    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
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助