I am trying to read a large (32M😎 remote file with fread and then write it to our server where we can parse it. It is a fixed-width text file with inventory information for our shopping cart. When I run the script though it will only read a portion of the file. The amount it reads seems to vary each day (new file) but is the same every time on the same file. Today it will read 2562 bytes of the file, yesterday was 3750 bytes. Anyway, any ideas on what is going on? also, the filesize() function in the fread returns an error so I have just been putting in some moderately large value like 50000 or something for testing. I have also set the timeout value on the script to 60, 600, 0 to see if that was the problem.

(i've replaced some of the sensitive info with * but the URL is valid and will output the full file to a browser)


set_time_limit (0);

$loc = "https://****.unitedstationers.com/access/services.asp?txtServer=****&txtUID=*****&txtPWD=****cmdSubmit=GET&srcFile=INVPOS&srcFolder=*****&txtType=ASCII&RequestType=*";
$fp = fopen($loc, "r");


$fstring = fread($fp, 500000);

$nfp = fopen('testwrite.txt', "w");
fwrite($nfp, $fstring);

    If you just want to get it in one "gulp", you could simply use [man]file_get_contents/man to read it, then [man]file_put_contents/man to write it.

      Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 33649991 bytes) in /home/mymonkey/public_html/... on line 7

      hmm... I can probably just parse through the string from the file_get_contents() since that does read the entire file to a string successfully instead of writing it to a file and then re-opening it.

      Thanks NogDog 🙂

        You might also try:

        $loc = 'https://url';
        $remote_fp = fopen($loc, 'r');
        $local_fp = fopen('testwrite.txt', 'w');
        
        while(!feof($remote_fp))
            fwrite($local_fp, fread($remote_fp, 8192));
        
        fclose($remote_fp);
        fclose($local_fp);
          Write a Reply...