【问题标题】:Displaying an uploaded file from $_FILES显示从 $_FILES 上传的文件
【发布时间】:2013-01-06 07:24:38
【问题描述】:

我在将文件从 HTML 上传到基于 PHP 的 Web 服务器时遇到了一些问题。 以下是我的 HTML 代码。我相信这里没有问题。

<html>
 <body>

  <form action="upload.php" method="post"
   enctype="multipart/form-data" action="2.2.2.cgi">
   <input type="file" name="file" id="file" size="35"><br>
   <input type="submit" name="submit" id="submit" value="Submit">
  </form>

 </body>
</html>

PHP 代码:

<?php

define ('MAX_FILE_SIZE', 1000000);

$permitted = array('image/gif', 'image/jpeg', 'image/png', 'image/pjpeg', 'text/plain');

if  ($_FILES['file']['type'] == $permitted 
    && $_FILES['file']['size'] > 0 
    && $_FILES['file']['size'] <= MAX_FILE_SIZE) {
      move_uploaded_file($_FILES, $uploadedfile);
      echo ('<img src="'.$uploadedfile'" />');
}
?>

我不知道如何使它在 PHP 中工作。我一直在尝试几种不同的方法,上面的代码只是我最近一次绝望的尝试,试图通过将值存储到一个新变量中来解决这个问题。我想要做的是以下内容:

  • 限制可上传文件的类型和大小。
  • 文件是图片吗?显示图片。
  • 是纯文本文件吗?显示文件的内容。
  • 它是未经许可的文件吗?显示文件的名称、类型和大小。

我们将不胜感激任何形式的提示或帮助。谢谢!

PS。它没有上线,所以目前任何安全问题都无关紧要。

【问题讨论】:

  • 你为什么有action="2.2.2.cgi" ??
  • 请阅读manual。将文件从临时位置复制到公共目录,然后创建正确的链接

标签: php html web-applications


【解决方案1】:

认为事情发生了一些变化,所以这里有一个受 Jonas 代码启发的 sn-p,它已经过全面测试

    foreach ($_FILES as $File) {

        $file_ext  = explode('/', $File['type'])[1];
        $file_type = explode('/', $File['type'])[0];

        // Validate type
        if(!in_array($File['type'], $allowed_types)) {
            echo "File type not permitted";
            continue;
        }

        // Validate size
        if  ($File['size'] >= $max_size) {
            echo "File is too large.";
            continue;
        }

        if($file_type == 'image') {
            $imageData = file_get_contents($File['tmp_name']); // path to file like /var/tmp/...
            echo '<img src="data:image/'.$file_ext.';base64,'.base64_encode($imageData).'" />';
        }
        
    }

【讨论】:

    【解决方案2】:

    好的。完全测试和注释的代码。

    上传.php

    define ('MAX_FILE_SIZE', 1000000);
    
    $permitted = array('image/gif', 'image/jpeg', 'image/png', 'image/pjpeg', 'text/plain'); //Set array of permitted filetypes
    $error = true; //Define an error boolean variable
    $filetype = ""; //Just define it empty.
    
    foreach( $permitted as $key => $value ) //Run through all permitted filetypes
    {
      if( $_FILES['file']['type'] == $value ) //If this filetype is actually permitted
        {
            $error = false; //Yay! We can go through
            $filetype = explode("/",$_FILES['file']['type']); //Save the filetype and explode it into an array at /
            $filetype = $filetype[0]; //Take the first part. Image/text etc and stomp it into the filetype variable
        }
    }
    
    if  ($_FILES['file']['size'] > 0 && $_FILES['file']['size'] <= MAX_FILE_SIZE) //Check for the size
    {
        if( $error == false ) //If the file is permitted
        {
            move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); //Move the file from the temporary position till a new one.
                  if( $filetype == "image" ) //If the filetype is image, show it!
                  {
                    echo '<img src="upload/'.$_FILES["file"]["name"].'">';
                  }
                  elseif($filetype == "text") //If its text, print it.
                  {
                    echo nl2br( file_get_contents("upload/".$_FILES["file"]["name"]) );
                  }
    
        }
        else
        {
            echo "Not permitted filetype.";
        }
    }
    else
    {
      echo "File is too large.";
    }
    

    与此表格一起使用 form.html

      <form action="upload.php" method="post"
       enctype="multipart/form-data" action="upload.php">
       <input type="file" name="file" id="file" size="35"><br>
       <input type="submit" name="submit" id="submit" value="Submit">
      </form>
    
     </body>
    </html>
    

    【讨论】:

    • 不,你的答案是错误的。没有定义$uploadedfile,并且move_uploaded_file的使用不当,因为他应该指向tmp_name
    • 谢谢彼得,来晚了。编辑了我的答案。
    • 感谢各位的帮助。 @PeterSzymkowski:我可能应该更仔细地阅读手册。我对此很陌生,所以我仍在学习中。
    • 那里有漂亮的评论编码!
    猜你喜欢
    • 2011-04-29
    • 1970-01-01
    • 2015-10-11
    • 2021-04-30
    • 2016-10-15
    • 1970-01-01
    • 2015-01-01
    • 2012-08-02
    • 2011-11-19
    相关资源
    最近更新 更多