So I have an 9MB file that is uploaded. I'm going to store the content of the file in a mysql table.

I have the content in a variable. I run mysql_real_escape_string and I end up with the following error:

Fatal error: Allowed memory size of 26214400 bytes exhausted (tried to allocate 17842413 bytes) in upload.php on line 68

(line 68 being the mysql_real_escape_string)

Here is the stripped code:

$fp = fopen($tmpName, 'rb');
	if (!$fp)
	{
		exit();
	}
$content = fread($fp, filesize($tmpName));
fclose($fp);

$content = mysql_real_escape_string($content);

I tried using addslashes instead but it leads to the same error. I don't really get the error since I have more memory than the script is using. Please help, I spent more than 5 hours on this and I can not figure out what to do!

    It's telling you that the current memory being used plus the 17842413 additional bytes it wanted to allocate would exceed the allowed limit of 26214400. The quick fix is to use ini_set() to set the desired value for the memory_limit setting.

      Is that really the best way to do it? Can I conserve the memory? I mean it's a 9mb file but the amount of memory I have set is 26MB.

      And yes it does work fine when I set the memory limit to 64MB. So thank you for that!

        Without understanding exactly what's going on, what type of data you're dealing with in that file, and why you want to store such a huge chunk of bytes in your database, it's hard for me to answer with a "better" way. Is this 9 megs of actual text being inserted into a single TEXT type field, or are we talking about binary data (e.g. an image file) be saved in a BLOB type field? And does it really make sense to store such a large item in the DB, or would you be better served saving it in the file system and just storing data about that file in the DB?

          It's a Binary BLOB field specifically a PDF file. I'm storing everything in the database simply because it's easier to manage.

          Thank you for the replies NogDog.

            You might want to look into using the [man]mysqli[/man] interface and then make use of the mysqli_stmt::send_long_data() method to bind that parameter straight from the file data (see the example).

            PS: Thanks for teaching me something new, that's the first time I looked at that function. 🙂

              Wow that actually looks pretty awesome! I'll give it a try. Thank you (and you're welcome :-) )

                Write a Reply...