The mysql implementation I've got does not allow the local load data infile. so I've been trying to work with php's fopen and fget commands to see if I can eventually post data to my database that way.

the code I'm using just as a functionality test even before the sql interface is:

<?php

$filename = $_FILES['userfile']['tmp_name'];
$handle = fopen($filename, "rbt");

while (!feof($handle)) {
$buffer = fgets($handle);
echo $buffer . "<br>";
}
fclose($handle);

?>

Now this works just fine for small files txt files. However, when I try and upload a file with 4875kb (small for the data I actually want to import) I start getting the following errors looping over and over ( I assume for each line of the file):

Warning: feof(): supplied argument is not a valid stream resource in /home/domains/cintiva.com/web/number.php on line 6

Warning: fgets(): supplied argument is not a valid stream resource in /home/domains/cintiva.com/web/number.php on line 7

Any insights into why this is happening would be greatly appreciated. It seems to me that it trys running the while loop before the file is even finished loading, and thus has nothing to work with.....

    10 days later

    Or, it may be that the file is never making it at all. Different hosts have varying limits on the a.) max size of files allowed to be uploaded and b.) max time limit that scripts are allowed to run for.

    Right before your 'while' loop, add some code that checks for the file:

    if(!$handle) {
      echo 'could not open '.$filename;
      exit();
    }

      Yeah,

      It seems that the big files, when placed on the actual website, rather than using a post, works like a charm.....

      So it's looking like the post hands off to php before finishing loading the file, PHP looks for an EOF that doesn't exist and throws an error.

      I suppose I can work around since I have ftp access to the server to place the files, but this does put a crimp in a plan for user upload later on.

      Is there any way to force php to wait for the full post upload, or to force POST to wait until the file is fully present before calling it's action?

      thanks

        Do you have access to the PHP.INI file? If so, you could try the things discussed at this page:

        http://forums.invisionpower.com/lofiversion/index.php/t65615.html

        Also, I am not sure you understood my other comment. It looks to me like the file is not getting uploaded at all for some reason. You are getting that error over and over because you are not testing to see if that file was opened successfully, so of course it will never find the EOF.

        Does that help? Hope so... have fun! :-)

          Yeah, I understood. Thanks.

          Testing for the file name is fine, and a good idea.....

          However, my concern is actually finding out why the form POST is being handed off to my PHP before the file as actually fully loaded in the first place.

          Small files get transmitted just fine, but large files choke, unless I completely bypass an upload form, and just drop the file at the server locally and have $filename= "path/filename.name" rather than the $_FILE.

          I'll look at the php.ini and see what's what there.

          Thanks!

            Hey designguy79,

            Thanks for that link, I think that's exactly what is going to solve my problem. Fortunately, I'm working on my own cobalt box here and have full access to the php.ini....so a resolution is in sight.

              3 months later

              I have a problem with reading files. Mine script is a bit bigger but I copied the part with errors in a new file and test it separatly.

              	$uploaded_file = fopen("upfiles/$nume_fisier", "r");  
              if(!$uploaded_file){die("<b style=\"color:#FF0000;\">Could not open $nume_fisier</b>");} echo fgets($uploaded_file, 1024); while(!feof($uploded_file)){ $s = fgets($uploaded_file, 1024); echo "Citeste : $s"; } fclose($uploaded_file);

              The problem is that I am seeing an error on every loop from that while, like here :

              Here is the line readed corectly from the file
              here the error Warning: feof(): supplied argument is not a valid stream resource in c:\program...\export-3.php on line 67
              Here is the NEXT line readed corectly from the file
              here the error Warning: feof(): supplied argument is not a valid stream resource in c:\program...\export-3.php on line 67
              Here is the NEXT line readed corectly from the file
              here the error Warning: feof(): supplied argument is not a valid stream resource in c:\program...\export-3.php on line 67
              ....
              this till the end of the every line from my file
              after the last line from the file is printed I see only erors like this :
              Warning: feof(): supplied argument is not a valid stream resource in c:\program ...\export-3.php on line 67
              till the maxim execution time ocurs (30 s in my php.ini)

              Any Ideea whats wrong ?
              any help will be appreciated

                It looks like you have two "fgets" statements -- one inside the loop to test for the end of file (feof), and the other before it! Remove the first one that is outside the loop...

                $uploaded_file = fopen("upfiles/$nume_fisier", "r");   
                if(!$uploaded_file){die("<b style=\"color:#FF0000;\">Could not open $nume_fisier</b>");} while(!feof($uploded_file)){ $s = fgets($uploaded_file, 1024); echo "Citeste : $s"; } fclose($uploaded_file);

                HTH

                  Write a Reply...