获取 php 中的文件内容

Getting file contents in php

我想获取我通过页面浏览的文件的内容。所以我写了一个 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);

然后我要打印这个文件的内容, 我也想打印 "any sentence that have the word computer "

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

有没有什么方法可以使用 file_get_contents()

来打印文件的值

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

试试这个

 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);
    }
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);