3

I am looking for a way to allow for file uploads but rather than writing the file to disk on the web-server, I would like to store the binary contents in a variable. I've found a simple example to actually perform uploads but I cannot find a way to prevent writing to file and instead keep it in memory.

Is it possible to do this and if so how?

<?php

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo "<p>";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Upload failed";
}

echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";

?>

<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

1 Answers1

2

If you only need the uploaded file for php runtime then

$uploadfile = file_get_contents($_FILES['userfile']['tmp_name']);

Later if you need to store it on MySQL DB, you can always do 'addslashes()' to it.

Gopakumar Gopalan
  • 1,187
  • 14
  • 22
  • 1
    Use prepared statements if you're going to a database! Or at least use `mysql_real_escape_string` Simply relying on `addslashes` will not cut it. – musicin3d Jul 02 '15 at 22:41