1

I have a simple HTML form that allows a user to select multiple text files. I want to loop through them and echo each one. I have the following code:

Upload text document: <br/><br>
<form action="output.php" method="post" enctype="multipart/form-data">
    <input type="file" name="documents[]" size="50" multiple="multiple" />
    <br />
    <input type="submit" value="Upload File" />
</form>

Then in output.php when I want to echo through each file and echo its contents. I tried this code:

$filenames = array();

if(isset($_FILES["documents"]))
{
   for($i=0; $_FILES['documents']['name'][$i] != '' ; $i++)
   {
       $filenames[$i] = $_FILES['documents']['name'][$i];

       $myfile = fopen("$_FILES['documents']['name'][$i]", "r") or die("Unable to open file!");

       echo fread($myfile,filesize("$_FILES['documents']['name'][$i]"));

       fclose($myfile);

    }

}

I get the following error:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp\www\php_sandbox\ValenceV1\ValenceV1_output.php

How can I fix this code, or if there is a better way to do this please do provide it. Thanks.

1
  • ditch the wrapping double quotes "$_FILES['documents']['name'][$i]" it doesn't make sense having that Commented Dec 5, 2014 at 2:29

2 Answers 2

1

In you code, that error was triggered on those lines which has been wrapped with double quotes and then wrapping array indices with the single one. If you expect to read those files without using move_upload_file(), use the tmp_name index instead to read the temp file:

<?php

$contents = '';
if(isset($_FILES['documents'])) {
    $num_of_files = count($_FILES['documents']['name']);
    for($i = 0; $i < $num_of_files; $i++) {
        $tmp = $_FILES['documents']['tmp_name'][$i];
        $contents .= file_get_contents($tmp);
    }
    echo $contents;
}

?>

If you decided the foreach flavor directly pointing to tmp_name:

$contents = '';
if(isset($_FILES['documents'])) {
    foreach($_FILES['documents']['tmp_name'] as $tmp) {
        $contents .= file_get_contents($tmp);
    }
    echo $contents;
}
Sign up to request clarification or add additional context in comments.

4 Comments

worked like a charm. thanks for that. this is more straightforward. @Ghost
@modarwish sure no prob glad this helped
Why use count(), and not just a foreach loop?
@PHPglue im just feeding off from what the OP has used, could use foreach, no harm in using this anyways
1

Why you enclosed the vars by double quotes? Remove the quotes & can try what I posted.

$filenames = array();
if(isset($_FILES["documents"]))
{
    for($i=0; $_FILES['documents']['name'][$i] != '' ; $i++)
    {
        $filenames[$i] = $_FILES['documents']['name'][$i];
        $myfile = fopen($_FILES['documents']['name'][$i], "r") or die("Unable to open file!");
                       /*^^^*/                       /*^^^*/ 
        echo fread($myfile,filesize($_FILES['documents']['name'][$i]));
        fclose($myfile);
    }
}

1 Comment

Thanks! I am getting warning: fopen(file1.txt): failed to open stream: No such file or directory in C:\wamp\www\php_sandbox\ValenceV1\output.php. I selected two text files, clicked upload, and then the error I got. Any idea why? @void main

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.