views:

45

answers:

2

Hi everyone!

PHP question (new to PHP after 10 years of Perl, arrggghhh!).

I have a 100mb file that I want to send to another server.

I have managed to read the file in and "post" it without curl (cannot use curl for this app). Everything is working fine on smaller files.

However, with the larger files, PHP complains about not being able to allocate memory.

Is there a way to open a file, line by line, and send it as a post ALSO line by line?

This way nothing is held in ram thus preventing my errors and getting around strict limitations.

Chris

Here's my current code that errors with large files:

<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

$resp = do_post_request("/local/file.txt","http://www.mysite.com/receivedata.php");
exit;

function do_post_request($file,$url){
   $fileHandle = fopen($file, "rb");
   $fileContents = stream_get_contents($fileHandle);
   fclose($fileHandle);

   $params = array(
      'http' => array
      (
          'method' => 'POST',
          'header'=>"Content-Type: multipart/form-data\r\n",
          'content' => $fileContents
      )
   );

   $ctx = stream_context_create($params);
   $fp = fopen($url, 'rb', false, $ctx);

   $response = stream_get_contents($fp);
   return $response;
}
?>
A: 

yes it is. You can read by chunks of any size. check this tutorial http://www.tizag.com/phpT/fileread.php

Andrey
+1  A: 

You can use fopen and fgets (or fread alternatively) to read the file sequentially.

However if your only purpose is to flush the file to standard output, you can simply use readfile('filename') and it'll do exactly what you want.

reko_t
I want to post the data to a URL from a command line php script that runs from a cron job. Does that make a difference?
Chris Denman
Nope, for that you need to do it with fgets/fread, since readfile outputs to the PHP's output buffer.
reko_t
I've posted some code to explain what I mean. I have looked everywhere and there's nothing - or I am getting the wrong end of the stick? ;)
Chris Denman