【问题标题】:Get content of file uploaded by user before saving [closed]在保存之前获取用户上传的文件内容[关闭]
【发布时间】:2013-05-29 03:08:10
【问题描述】:

看,我为我网站的用户提供了将他们自己的 PHP/HTML/TXT 文件上传到我的服务器的可能性,对吗?...但是我想获取该文件内容而不将其保存在我的服务器中并保存将内容转换为字符串或其他任何内容。

有可能吗?...如果没有,我应该在我的服务器中保存文件后如何获取内容?...请帮助我!

我不知道这是否有帮助,但这是我用来上传用户文件的代码。

$allowedExts = array("php", "html", "txt");
$tmp = explode(".", $_FILES["file"]["name"]);
$extension = end($tmp);

if 
((($_FILES["file"]["type"] == "application/octet-stream") 
|| ($_FILES["file"]["type"] == "text/php") 
|| ($_FILES["file"]["type"] == "text/html") 
|| ($_FILES["file"]["type"] == "text/plain")) 
&& ($_FILES["file"]["size"] < 50000) && in_array($extension, $allowedExts))
{
    if ($_FILES["file"]["error"] > 0)
    {
        echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
}
else
{
    echo "Invalid file";
}

【问题讨论】:

    标签: php file upload save


    【解决方案1】:

    是的,file_get_contents 是可能的

    $fileContent = file_get_contents($_FILES['upload_file']['tmp_name']);
    

    下面的链接也很有用

    php: file_get_contents is stripping out php code

    【讨论】:

      【解决方案2】:

      为此使用file_get_contents()

      echo "Upload: " . $_FILES["file"]["name"] . "<br>";
      echo "Type: " . $_FILES["file"]["type"] . "<br>";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
      echo "Stored in: " . $_FILES["file"]["tmp_name"];
      
      // store file content as a string in $str
      $str = file_get_contents($_FILES["file"]["tmp_name"]);
      

      请注意,如果您还需要该文件以供以后使用,则必须另外将其复制到目标位置。像这样:

       move_uploaded_file($_FILES["file"]["tmp_name"], 'contents/' . $_FILE['name']);
      

      【讨论】:

      • 这个。这就是我需要的。谢谢你先生(-:
      猜你喜欢
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 2018-11-11
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      • 2015-06-28
      相关资源
      最近更新 更多