PHP and file creation

software development

#1

I haven’t played on a *nix system in ages, so this may turn out to be something simple, like a permissions issue.

I have a PHP script I use for my own site counters (this predates my recent hosting here, so I wanted to stick with the old system). The first time the script runs, I want it to check to see if the data file exists. If it doesn’t (which is the case the first time it runs), I want it to create the basic data file for use with subsequent executions. (I want to avoid writing a dummy ‘starter’ data file to that directory.)

The script runs fine when the data file already exists. It finds it, reads it, updates it and writes it back to disk. It’s been running fine for the month I’ve been hosted here.

The script just doesn’t seem to want to create the new data file when it doesn’t already exist. The file_exists() test and file-create fopen() are in the code before the first attempt to load the data via file().

The PHP script, itself, has permissions of 644.

Thanks.

-JPB


#2

Make sure you are using relative paths and not remote paths beginning in http://


Simon Jessey
Keystone Websites | si-blog


#3

Not sure what you’re referring to. Do you mean in the HTML that calls the PHP script?

Here’s an example of what calls my script:

[Displays the graphic-style count value]

The script is in a subdirectory one level under where the HTML (actually, it’s PHP-generated HTML) data resides.

This allows each of my collections to have it’s own unique counter set, instead of forcing the whole site to use One Big Datafile with well over a thousand counter values being read/reread/written/rewritten all the time. (I admit it leaves me with tons of counter data files and subdirectories, but it cuts down on the read/write load per dataset.)

The counter script and the configuration and data files all reside in the same subdirectory. The graphic imagery for the digits reside below that directory level.

The relevant code snippet:

if (file_exists($datfile)) {
$data = “”;
} else {
$data = “1\n” . $link . “1”;
$fp2 = fopen($datfile, 'r ');
fwrite($fp2, $data);
fclose($fp2);
}
$lines = file ($datfile);

The variable $datfile contains the local file name string literal “counter.dat”, no path.

I originally tested with a (!(file_exists($datfile)) but when that kept failing, I changed it to what you see above, with an explicit acton for pass/fail.

I’ve also tried fopen() with the ‘w’ and 'w ’ options, with the same result.

Once the file exists, it finds the pre-existing file in that location fine. It just doesn’t seem to want to create it and I can’t quite see where I’m messing it up. :slight_smile:

Thanks.

-JPB


#4

Thanks for your reply, I discovered the problem after posting my last reply to you.

The unedited code snippet I put up was:

if (file_exists($datfile) {
$data = “”;
} else {
$data = “1\n” . $link . “1”;
$fp2 = fopen($datfile, 'r ');
fwrite($fp2, $data);
fclose($fp2);
}
$lines = file ($datfile);

Note that there is one close-paren missing from the file_exists() test. I saw that when I reread the live post.

Oddly, as the above snip shows, it looks like I incorrectly open the file in “r” read-only mode, but the “+” for read/write-mode is missing. It was in the original code-paste, I swear! :slight_smile:

That still didn’t work, so I changed the mode to “w+” and that seems to have done the trick.

I was making these script edits in a simple text editor (simple edit/simple editor), but if I had been using my context-sensitive editor, I’d have caught that earlier.

Thanks, again.

-Joe
http://jiltanith.thefifthimperium.com