There is a form that works and sends files, but when I try to send a file of more than 34 MB, I get an error

Fatal error: Allowed memory size of 134217728 by exhausted (tried to allocate Y bytes)

Raising the memory_limit directive to 256 did not help!

Error refers to string

$encoded_content = chunk_split(base64_encode($content)) 

which translates a file into code by type

 OEJQUwABAAAAAAAAAAMAABdiAAAGKAAIAAMAAAAAAAFhWDhCSU0EBAAAAAABVxwBWgADGyVHHAFa AAMbJUccAVoAAxslRxwBWgADGyVHHAFaAAMbJUccAVoAAxslRxwBWgADGyVHHAFaAAMbJUccAVoA 

Well, respectively, in this form, the file is more than 128 MB. If someone knows how to convert a file differently, please tell me!

Further, $encoded_content will be sent to the post office.

 $body .= "--$boundary\r\n"; $body .="Content-Type: $file_type; name=".$file_name."\r\n"; $body .="Content-Disposition: attachment; filename=".$file_name."\r\n"; $body .="Content-Transfer-Encoding: base64\r\n"; $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; $body .= $encoded_content; 
  • in php.ini increase memory_limit - VikiMayson
  • Wow what are you attentive! Reread the question! - Oleg
  • If by code, then `$ body. =" - $ boundary \ r \ n "; $ body. = "Content-Type: $ file_type; name =". $ file_name. "\ r \ n"; $ body. = "Content-Disposition: attachment; filename =". $ file_name. "\ r \ n"; $ body. = "Content-Transfer-Encoding: base64 \ r \ n"; $ body. = "X-Attachment-Id:" .rand (1000,99999). "\ r \ n \ r \ n"; $ body. = $ encoded_content; `etc. And if the meaning, then send by mail to gmail! - Oleg
  • @ Oleg and if memory_limit -1 do? - Manitikyl
  • Manitikyl, honestly not sure whether you can do this. the site is located on a hosting with a specific value of the directive 128 MB - Oleg

2 answers 2

You need to encode and send the file in blocks.

Here, as option (the code of sending the letter is taken from here )

 function xxmail($to, $subject, $body, $headers) { $smtp = stream_socket_client('tcp://smtp.yourmail.com:25', $eno, $estr, 30); $B = 8192; $c = "\r\n"; $s = 'myapp@someserver.com'; fwrite($smtp, 'helo '.$_ENV['HOSTNAME'].$c); $junk = fgets($smtp, $B); // Envelope fwrite($smtp, 'mail from: '.$s.$c); $junk = fgets($smtp, $B); fwrite($smtp, 'rcpt to: '.$to.$c); $junk = fgets($smtp, $B); fwrite($smtp, 'data'.$c); $junk = fgets($smtp, $B); // Header fwrite($smtp, 'To: '.$to.$c); if (strlen($subject)) fwrite($smtp, 'Subject: '.$subject.$c); if (strlen($headers)) fwrite($smtp, $headers); // Must be \r\n (delimited) fwrite($smtp, $headers.$c); // Body if (strlen($body)) fwrite($smtp, $body.$c); send_file($smtp); fwrite($smtp, $c . '.' . $c); $junk = fgets($smtp, $B); // Close fwrite($smtp, 'quit'.$c); $junk = fgets($smtp, $B); fclose($smtp); } function send_file($smtp, $lineLen = 76) { $file = fopen($_FILES['userfile']['tmp_name'], 'rb'); if (!$file) return; $lineLen = ($lineLen | 3) - 3; // $lineLen Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π±Ρ‹Ρ‚ΡŒ ΠΊΡ€Π°Ρ‚Π½ΠΎ 4 $bytes = $lineLen / 4 * 3; // ΠŸΡ€ΠΈ ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠΈ Π² base64 ΠΈΠ· ΠΊΠ°ΠΆΠ΄Ρ‹Ρ… 3 Π±Π°ΠΉΡ‚ ΠΏΠΎΠ»ΡƒΡ‡Π°ΡŽΡ‚ΡΡ 4 $buflen = floor(4096 / $bytes) * $bytes; // Π§ΠΈΡ‚Π°Π΅ΠΌ Ρ„Π°ΠΉΠ» Π±Π»ΠΎΠΊΠ°ΠΌΠΈ порядка 4ΠΊΠ±, ΠΊΡ€Π°Ρ‚Π½Ρ‹ΠΌ Π΄Π»ΠΈΠ½Π΅ строки while (!feof($file)) { $data = fread($file, $buflen); // Ρ‡ΠΈΡ‚Π°Π΅ΠΌ Π±Π»ΠΎΠΊ Π΄Π°Π½Π½Ρ‹Ρ… $data = chunk_split(base64_encode($data)); // ΠΊΠΎΠ΄ΠΈΡ€ΡƒΠ΅ΠΌ Π΅Π³ΠΎ fwrite($smtp, $data); // ΠžΡ‚ΠΏΡ€Π°Π²ΠΈΠ»ΠΈ Π² сокСт } fclose($file); } 

    Option using threads. Data is encoded and written to a file. Can and not to file. @see Conversion Filters

     <?php // Π‘Ρ‚Π°Π²ΠΈΠΌ Π»ΠΈΠΌΠΈΡ‚ памяти 1 Мб ini_set('memory_limit', 1*1024*1024); echo ini_get('memory_limit') . PHP_EOL; // Π‘ΠΎΠ»ΡŒΡˆΠΈΠ΅ Π΄Π°Π½Π½Ρ‹Π΅ $content = 'My long long file'; $content = str_repeat($content, 50000); // Π­Ρ‚Π° строчка Π²Ρ‹Π΄Π°Π΅Ρ‚ ΠΎΡˆΠΈΠ±ΠΊΡƒ "Fatal error: Allowed memory size ..." - ΠΊΠ°ΠΊ Ρƒ Π’Π‘ // $encoded_content = chunk_split(base64_encode($content)); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ Π²Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹ΠΉ Ρ„Π°ΠΉΠ» $encoded_content_file = tempnam('/tmp/', 'enc_'); $fp = fopen($encoded_content_file, 'w'); // Π€ΠΈΠ»ΡŒΡ‚Ρ€ для ΠΏΠΎΡ‚ΠΎΠΊΠ° $param = array('line-length' => 76, 'line-break-chars' => "\r\n"); stream_filter_append($fp, 'convert.base64-encode', STREAM_FILTER_WRITE, $param); // Π­Ρ‚Π° строка Ρ‚Π°ΠΊΠΆΠ΅ Π²Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ ΠΏΠ΅Ρ€Π΅ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ памяти, поэтому Ρ€Π°Π·Π±ΠΈΠ²Π°Π΅ΠΌ Π½Π° ΠΏΠΎΡ€Ρ†ΠΈΠΈ ΠΈ пишСм // fwrite($fp, $content); $chunk = 1024; for($i = 0; $i <= strlen($content); $i += $chunk) { fwrite($fp, substr($content, $i, $chunk)); } fclose($fp); // Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Π½Π°Π·Π²Π°Π½ΠΈΠ΅ Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠ³ΠΎ Ρ„Π°ΠΉΠ»Π° echo $encoded_content_file . PHP_EOL;