I have this code segment to upload a file. I get this this error when I upload big files like (300MB). Files below 20 MB work fine.
Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 813695000 bytes) in C:\xampp\htdocs\PHP\processor.php on line 219
Note: I already did lot SO and google search, but failed to fix this.
Reason I found: Reason - file_get_contents()
fetches entire string of data into variable. that variable is stored in hosts memory. When string is greater than the size dedicated to PHP process
$data = file_get_contents( $path );
//echo $data;
$data = mysqli_real_escape_string( $con, $data );
$query_insert = "INSERT INTO Upload(Name,Type,Size,Content) VALUES('Tour Photo','jpg',234, '$data')";
$result_insert = mysqli_query( $con, $query_insert ) or die ("<hr>Couldn't execute INSERT query: ". mysqli_error($con));
echo "<h4>New sport added to the database</h4>";
UPDATED code for @RedPoppy
$fo = fopen ( $path, 'r' );
if( is_readable($path) ){
echo "*** Uploaded file is readable ***<br><br>";
}
$data = fread ( $fo, filesize($path) ); //read entire file
$data = mysqli_real_escape_string ( $con, $data );
fclose( $fo );
The Problem ist that you're about to store files in your DB. That's insane. The only (logical) solution to avoid this error is to upload the file to your filesystem and save the path/reference/key in your database. Otherwise, you will keep getting that error plus your DB perfomance is beyond oblivion.
Or you try this: ini_set('memory_limit', '-1');
but I don't recommend this.
EDIT: You can also edit your .htaccess file: add php_value memory_limit xxxM
while xxx is your maximal size in Megabytes
assuming that your Content
is a binary blob and that you actually have a reason for storing files in the database (there can be a few, most generally it's not recommended for operational reasons), you could allow large file uploads by reading pieces of the file and inserting with an update clause (or just doing an insert followed by updates). Use fopen
, fread
and fclose
. Please be aware that with very large files you might have trouble from other areas, like the webserver becoming unresponsive etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.