显示来自$ _FILES的上传文件 [英] Displaying an uploaded file from $_FILES

查看:37
本文介绍了显示来自$ _FILES的上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having some issues with uploading files from HTML to a PHP based web server. The following is my HTML code. No issues here I believe.

<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中工作.我一直在尝试几种不同的方式,上面的代码只是我通过将值存储到新变量中来解决这个问题的最新拼命尝试.我要执行的操作如下:

I can't figure out how to make it work in PHP. I've been trying several different ways and the above code is just my latest desperate attempt at figuring it out by trying to store the value into a new variable. What I want to do is the following:

  • 限制可以上传的文件的类型和大小.
  • 文件是图片吗?显示图片.
  • 这是纯文本文件吗?显示文件的内容.
  • 这是不允许的文件吗?显示文件的名称,类型和大小.

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

Any kind of hint or help in any way would be greatly appreciated. Thanks!

PS.它没有上线,因此目前没有任何安全问题.

PS. It's not going live, so any security issues are irrelevant at the moment.

推荐答案

好的.经过全面测试和注释的代码段.

Allright. Totally tested and commented piece of code.

upload.php

define ('MAX_FILE_SIZE', 1000000);

$permitted = array('image/gif', 'image/jpeg', 'image/png', 'image/pjpeg', 'text/plain'); //Set array of permittet 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>

这篇关于显示来自$ _FILES的上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆