Page 1 of 1

Bizarre fopen problem

Posted: Tue Apr 22, 2003 6:23 am
by McGruff
I have a very, very strange problem with fopen in a page caching script. This is the file writing bit of the code:

Code: Select all

<?php

    // capture output for writing to file
    ob_start();
    include("html/property.htm");
    // copy buffer to string
    $browser_output = ob_get_contents();
    // silently clear buffer
    ob_end_clean();
    // write file
    $filename = WRITE_PATH . $cid . '/prop/' . htmlspecialchars(strtolower(str_replace($search, $replace, $property))) . '.htm';
    echo $filename . '<br>'; #DEBUG
    $fp = fopen($filename, "wb");
    fputs($fp, $browser_output);
    fclose($fp);

// WRITE_PATH is a constant - ftp://username:password@www.whoownsscot ... age_cache/

?>
The script loops through several hundred db rows, writing a file for each row. On my local server I omit the WRITE_PATH; on the live site this is needed since folders are owner writable only.

Everything works fine on my local server (apache, Win, php 4.2) but it behaves VERY strangely when I tested it live (Linux, php 4.1.2).

------------------------------------
(1) Sometimes no files are created at all and I get "success" (?!) error messages for each row in the loop:

Warning: fopen("ftp://...@www.whoownsscotland.org.uk/pa ... uccoth.htm", "wb") - Success in /www.whoownsscotland.org.uk/page_cache/cache_builder.php on line 269

Warning: Supplied argument is not a valid File-Handle resource in /www.whoownsscotland.org.uk/page_cache/cache_builder.php on line 270

Warning: Supplied argument is not a valid File-Handle resource in /www.whoownsscotland.org.uk/page_cache/cache_builder.php on line 271

(fopen is on line 269, fputs on 270, and fclose on 271)

--------------------------------
(2) Other times, some files will be written - but always just FOUR files out of the several hundred that should be created.

As the script runs through the loop, first off I get these error messages as it tries to write over files which already exist:

Warning: File already exists in /www.whoownsscotland.org.uk/page_cache/cache_builder.php

Warning: fopen("ftp://...@www.whoownsscotland.org.uk/pa ... irsaid.htm", "wb") - File exists in /www.whoownsscotland.org.uk/page_cache/cache_builder.php

Warning: Supplied argument is not a valid File-Handle resource in /www.whoownsscotland.org.uk/page_cache/cache_builder.php

Next, the script will write just four files before spewing out more error messages for the rest of the loop (same error as in (1) above):

Warning: fopen("ftp://...@www.whoownsscotland.org.uk/pa ... uccoth.htm", "wb") - Success in /www.whoownsscotland.org.uk/page_cache/cache_builder.php

Warning: Supplied argument is not a valid File-Handle resource in /www.whoownsscotland.org.uk/page_cache/cache_builder.php

Warning: Supplied argument is not a valid File-Handle resource in /www.whoownsscotland.org.uk/page_cache/cache_builder.php

-------------------------

Wierd, eh?

It's not a timeout problem (only takes about 12s to get through the loop).

If it failed consistently I'd be happier because then I could try to isolate the problem. The erratic behaviour - sometimes churning out "success" errors, and sometimes writing four files (why just four?) with different error messages before and after the four files are written - has me stumped.

If it's at all relevant, CHMOD'ing folders on the site also produces strange results: after changing settings the folder just refuses to open in my ftp program giving a 550 message. Can't delete it either - 550 again. Same deal even when I change back to the original settings - so I'm stuck with a bunch of dud folders!

Php is usually well-behaved and logical but I think my host has installed a "giggling madman" version.

The script works perfectly locally: maybe I should abandon the website and just invite everyone who wants to view it round to my home :)

Posted: Tue Apr 22, 2003 7:00 am
by volka
yes, weird
But why not using the ftp-api?

Posted: Tue Apr 22, 2003 7:21 am
by McGruff
Thanks Volka :)

Haven't really messed about with file-writing or ftp much - will try the ftp functions.

Posted: Tue Apr 22, 2003 5:20 pm
by McGruff
Actually ftp fns won't help since I don't have a file to ftp: just a string captured from the output buffer.

Tried popen(): error messages gone but no files being written.

Time to look for a new host I think.