views:

582

answers:

3

With Apache/PHP5, is it possible to get the contents of an uploaded file directly without having it written to the file system?

Not much on Google about this, but it appears that files are always written to a temporary directory once they are uploaded.

+3  A: 

Not sure i understand you, but i will try to answer.

http://www.w3schools.com/PHP/php%5Ffile%5Fupload.asp

What you can learn here is that every file is uploaded to php's temp directory. Then it is up to your script to move/copy that file to some permanent web accessible directory, because file that was uploaded to php's temp dir is deleted after the script end executing.

GaVrA
You understood correctly - I'm only using the file read the data in it, and I didn't want to store the file on the file system if I didn't have to.
jimyi
Well then i dont think you have to anything else beside upload file and exectute the script. After execution - file is deleted. :)
GaVrA
A: 

On Linux you can create filesystem partitions in memory. If you can ensure that the uploaded file is written to the partition in memory, if will be stored in memory but act as if it were stored in the filesystem, making both Apache/PHP5 and you happy.

The problem is that any solution which writes files to memory rather than to the filesystem requires severe limitations on the size and quantity of the files. You will see a speed boost by avoiding writing to disk, but if you use enough memory to push other data into the pagefile or swap, your "optimization" will become counterproductive very quickly.

Imagist
A: 

You can:

<?php
if (!empty($_FILES))
{
    echo "<pre>";
    print_r($_FILES);
    echo file_get_contents($_FILES['file']['tmp_name']);
    echo "</pre>";
}
?>
<form id="myForm" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="Send" />
</form>

Output:

Array
(
    [file] => Array
        (
            [name] => mytextfile.txt
            [type] => text/plain
            [tmp_name] => D:\PHP\wamp\tmp\php1283.tmp
            [error] => 0
            [size] => 1473
        )

)
My text file My text file My text file My text file My text file My text file 
My text file My text file My text file My text file 
My text file My text file My text file My text file My text file My text file 
My text file My text file My text file 
My text file My text file My text file My text file My text file

I don't know if it's restricted by some php.ini variable.

inakiabt
That reads the file from the filesystem -- Clearly this doesn't solve his problem.
Craig
I know how to read files - my question was about reading the files without having it written to the file system. With what you posted, the uploaded file is written to D:\PHP\wamp\tmp\
jimyi
My mistake so, but your question was not pretty clear.
inakiabt