【问题标题】:Getting file contents in php在php中获取文件内容
【发布时间】:2015-10-01 06:03:01
【问题描述】:

我想获取我通过页面浏览的文件的内容。所以我写了一个 php 脚本来浏览它。 代码如下

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="my-file" size="50" maxlength="25"> <br>
<input type="submit" name="upload" value="Upload">

它的php代码是

if (is_uploaded_file($_FILES['my-file']['tmp_name']) && $_FILES['my-file']['error']==0) { 
echo "The file name was: " . $_FILES['my-file']['name'] . "<br>";
echo "The file type is: " . $_FILES['my-file']['type'] . "<br>";
$file=$_FILES['my-file'];
$out = file_get_contents($file);

然后我想打印这个文件的内容, 我也想打印“任何有计算机这个词的句子”

但它不会打印我浏览过的文件的内容,但它会打印文件名和文件类型的详细信息。

有什么方法可以使用 file_get_contents() 打印文件的值

在.txt文件中获取文件的结果,它是可下载的格式

【问题讨论】:

  • 你想要$_FILES['my-file']['tmp_name']而不是$_FILES['my-file']

标签: php file file-get-contents


【解决方案1】:

试试这个

 if (is_uploaded_file($_FILES['my-file']['tmp_name']) && $_FILES['my-file']['error']==0) { 
    echo "The file name was: " . $_FILES['my-file']['name'] . "<br>";
    echo "The file type is: " . $_FILES['my-file']['type'] . "<br>";
    $file=$_FILES['my-file'];
    $out = file_get_contents($file['tmp_name']);
    print_r($out);
    }

【讨论】:

  • 是否可以将其结果以可下载格式存储在文本文件中
  • 使用 file_put_contents() 可以将数据存储到文本文件中。
【解决方案2】:
if (is_uploaded_file($_FILES['my-file']['tmp_name']) && $_FILES['my-file']['error']==0) { 
echo "The file name was: " . $_FILES['my-file']['name'] . "<br>";
echo "The file type is: " . $_FILES['my-file']['type'] . "<br>";
$file=$_FILES['my-file'][''];//use ['tmp_name']
$out = file_get_contents($file);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    相关资源
    最近更新 更多