Marc Rasmussen
Marc Rasmussen

Reputation: 20555

PHP get file content without saving

I have the following php code that runs when someone uploads a file:

        if ($_FILES['files']['error'] === UPLOAD_ERR_OK) {
        die("Upload failed with error " . $_FILES['file']['error']);
    }
    $namme = $_FILES['files']['name'][0];
    $namme = substr($namme, strpos($namme,"."), strlen($namme));
    $ok = false;
    switch ($namme) {
        case '.docx':
        case '.txt':
        case '.pdf':

        break;
        default:
            die("Unknown/not permitted file type");
    }
    $filename = $_FILES['files']['tmp_name'][0];
    $file = _file_get_contents('./'.$_FILES['files']['name'][0], true);

I want to print all of the content out but i don't wish to save the file first and then open it.

My question is: Is it possible to open the text file print the content without saving the file first?

Upvotes: 0

Views: 295

Answers (1)

Russell Zahniser
Russell Zahniser

Reputation: 16354

When the user uploads a file, it gets saved in a temp location. That's just how the HTTP server works, before your PHP script ever gets called. It would be bad if the server held all uploaded files in memory; what if the user is uploading a dozen files that are all 10 MB?

The only way to avoid that is to not use file upload (for example, have the user paste the file contents into a text area that gets submitted), or write your own HTTP server and don't use PHP (not a practical solution, most likely).

Upvotes: 2

Related Questions