如何从上传的文本文件中读取

时间:2014-09-09 11:31:37

标签: php file-get-contents

当我上传文件时,我该如何阅读它的内容,以及如何知道它的位置 我知道我可以使用file_get_contents,但我不知道如何使用它

4 个答案:

答案 0 :(得分:1)

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

编辑:,也请阅读本教程click

答案 1 :(得分:1)

提交后,您可以从超级全球$ _FILES获取所有信息,这些信息将始终返回" name"," type"," size"," tmp_name","错误":

Array (
    [file_input_name] => Array (
        // the name of the uploaded file
        [name] => uploaded_file.jpg

        // the type of the uploaded file
        [type] => image/jpeg

        // the name of the temporary copy of the file stored on the server
        [tmp_name] => /tmp/phpcOGiTD

        // the error code resulting from the file upload
        [error] => 0

        // the size in bytes of the uploaded file
        [size] => 766379
    )
)

您可以使用move_uploaded_file将其移至其他名称,然后使用file_get_content

echo file_get_contents($_FILES['file_input_name']['tmp_name']);

注意:以上是单个文件上传的示例。如果是多个文件上传,您需要指定要查看的文件([0][1] ...):

echo file_get_contents($_FILES['file_input_name']['tmp_name'][1]);

答案 2 :(得分:0)

请查看功能参考 here

你必须使用类似的东西:

$content = file_get_contents("/path/to/your/file.txt")

答案 3 :(得分:0)

Clearifications such task would require you to use a server side programming language like PHP; how to open, read, and close a file on the server.

// upload file with a button name: upload_file
$fileContent = file_get_contents($_FILES['upload_file']['tmp_name']);

//Path to your file:
if (isset($_POST['submit'])) {

    $tmp = $_FILES['filename']['tmp_name'];
    $name = $_FILES['filename']['name'];


    //Can be any full path, just don't end with a /. That will be added in in the path variable
    $uploads_dir = '/path/to/your/file.txt';

    $path = $uploads_dir.'/'.$name;

    if(move_uploaded_file($tmp, $path)){
        $msg='". $name ."';


// PHP Open File - fopen()

A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.
The first parameter of fopen() contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened. The following example also generates a message if the fopen() function is unable to open the specified file:
Example
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>