Reputation: 31191
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.
Upvotes: 2
Views: 8164
Reputation: 1963
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.
Upvotes: 0
Reputation: 18514
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.
Upvotes: 0
Reputation: 1837
Not sure i understand you, but i will try to answer.
http://www.w3schools.com/PHP/php_file_upload.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.
Upvotes: 5