I've been experimenting as a newbie with the various php file handling functions, specifically

// Set filename 
$filename = 'http://www.domain.com/filename.htm'; 

// Set the handle 
$handle = fopen ($filename, "r"); 

// Put it into a string 
$data = fread ($handle, 120000);
//It makes no difference what size number is here, array1[7] still 
//cuts off half way through, if I leave out array1[7] the previous
//array segment is the last one; If I make array1[7] the first 
//segment, it still cuts off; if I leave it out and call array1[8] only,
//no output is generated at all

// Close the file 
fclose ($handle); 

// Explode it into an array 
$array1 = explode("</tr>",$data); 

The problem I experience is that the html generated from the above code works until half way through one of the generated array segments. In other words, only part of the file is created.

I am running out of ideas here. Possibilities, in descending order of likelihood, are:

  1. The fread function is ending early;

  2. The array construction process is incomplete;

  3. The conversion of the array into html is wrong.
    [/list=1]
    I have run out of ways to test which problem is to blame, and I may not have thought of all the possible problems anyway.

    I'd very much appreciate some tips.

    For information, the php file I am creating uses the PostNuke file blank.php as the skeleton, within which I wish to recreate the html file which I am trying to read from - created in Frontpage... aaargghhh!

    The size of the source file is about 105000 while the segment read is about 8500, ending on a < tag at the beginning of an <i> tag.

    TIA

    Mac

    What if you try:

    $data = fread ($handle, filesize($filename));

      I tried that, but it generates no output at all. Only this error message:

      Warning: filesize(): Stat failed for [url]
      [url]http://www.sourcedomain.com/Directory/filename.htm[/url][/url] 
      (errno=2 - No such file or directory) in /home/../../../file.php on line 26

      It seems to be looking for the source file inside the current directory of my php file ...?

        try to echo $data to find out if it has been read completely. And let us know

          OK, I wrote

          echo($data);

          and it stopped in exactly the same place as before, but it also echoed all the other stuff that preceded what I wanted. Does this mean the file reading is the problem?

            I did not get.. does

            echo $data

            output all the content of the remote file or not...

            $handle = fopen ($filename, "r");
            $content = "";
            while (!feof ($handle)) {
                $content .= fgets($handle, 4096);
            }
            fclose ($handle);
            echo $content;
            
            

              I forgot to say. Try the code I posted above.

                Yes! That worked! I now have the complete file on the page. How do I integrate this new code into my existing code? Do I just replace the echo statement with the explode function and print statements?

                One thing I don't understand is the 4096 reference - is this restricting each line of the source file to this size, or referring to the absolute size of the source file, which is actually over 100k?

                I'm learning, but it's complicated!

                I very much appreciate your help though. Thanks.

                  That is the size of the buffer. It's rather low in my example and you may increase it if yiu want. In other words the file is read by portions (4096 bits each). And all the portions are attached to the value of $content variable.

                  Now you have the content of the remove html file and it's up to you what to do with it. You may be want to rid off some part of it, make replacements or uotput it as it is.

                    Ah... I see. Any ideas what could have caused the other method to fail?

                    I reintegrated my code and got what I wanted. The page I am copying from is a list of concerts on in the area. The file changes each month.

                    I now have 73 print statements, which seems a little inefficient. I guess there is a way to count the number of elements in an array, and then use this figure in some sort of loop - either using if...while or a for "x=1 to n, do y" or some such method.

                    Which functions do I need for this operation do you think?

                    TIA

                    Mac

                      I would recommend you to read some PHP tutorial to learn the basic of the language.

                      And you can always refer to PHP manual here http://www.php.net/manual/en/ to find out the list of available functions.

                      For example, how to count elements in an array check out here: http://www.php.net/manual/en/function.count.php

                      "For" loop here: http://www.php.net/manual/en/control-structures.for.php

                      By the way, the file you are reading and had problem with, is it located somewhere on remote server or somewhere on the site you are developing? If second one, than the method you use is not the best one...

                        OK, I had a read of the online stuff and my PHP reference book and I'm still stuck.

                        count() I can understand.

                        The looping I cannot. I am guessing to loop through the array I need to use the foreach() function, but I can't work out how to get the function to begin with $array1[3] and not $array1[0]?

                        It should be possible to have the loop print all elements in the array from [3] up to the end of the array.

                        I am guessing that if I use the foreach() function I don't have to worry about using count() at all?

                        It also looks like the while() or do..while() loops would be good, but then there is also the for() loop. And all the examples (except for the foreach() loop) refer to non-array variables and I want to deal with an array... so I can't see how they work in practice.

                        Can you help?

                        TIA

                        Mac

                          $count = count($the_array);
                          // 1st element in array has index 0
                          // so to staring output from 3rd element
                          // start from index 2
                          for($i=2; $i<$count; $i++){
                             print $the_array[$i];
                          }
                          

                            Thanks for that, I knew about the array count beginning with 0, but some others reading the post will benefit from your reminding them. I actually needed element 4, which was named $array1[3] but I can understand your explanation. Due to timezone differentials I posted the question seperately with the correct heading (I was taking this thread offthread) and also got the same answer there.

                            Thanks a lot for your help - I hope you get sorted with work soon. At least you live in a nice place like Canada!

                            Cheers,

                            Mac

                              Thank you, I have a planty of work here.

                              But desparately looking for new another piece of job. I am starving for work....

                                Write a Reply...