Any idea why this PHP fails to write a file?

Miscellaneous Forums/General Discussion/Any idea why this PHP fails to write a file?

Hiya, I've got this php on my side:
<?php

// base url location of files - MUST USE TRAILING SLASH!!
$base_url	= "http://www.greyaliengames.com/boo/";

// list of filenames - DO *NOT* PREFIX WITH SLASH!!
$files		= array("test1.zip", "test2.zip");

// all the boring processing stuff below...
$id = $_GET['id'];
if ($id >= 0 && $id < sizeof($files) && is_numeric($id)) {
	$log_filename = $files[$id] . ".log";
	$redirect_url = $base_url . $files[$id];
	$handle = @fopen($log_filename, "r");
	if ($handle) {
		$curr_count = fgets($handle);
		fclose($handle);
	}
	if (!is_numeric($curr_count))
		$curr_count = 1;
	else
		$curr_count++;
	$handle = @fopen($log_filename, "w");
	if ($handle) {
		fwrite($handle, $curr_count);
		fclose($handle);
	}
	header('Location: ' . $redirect_url);
	print('<html><body><a href="' . $redirect_url . '">Click here if you are not transfered automatically.</a></body></html>');
} else {
	echo '<html><body>Error: Invalid file specified</body></html>';
	exit();
}

?>


Which delivers the zip file fine but it doesn't actually write the .log.txt file with the same name that should contain the number of downloads. Does the php script need to be given certain permissions or something?

Thanks for any help...

Yes you need to set the mod, owner and group permissions on the file you are trying to write to - the script owner may need to be "root" or "nobody"

The following Unix commands are the ones you need to lookup :

chmod - Sets File attributes
chown - Sets File Ownership
chgrp - Sets File Group Ownership

For more detail on the error you should refer to your webserver logs.

Indiepath: Thanks. I can use CHMOD in my FTP program, so maybe I'll give the PHP full permissions. I can't set permissions on the file because when you first try it, it doesn't exist so it gets created.

b*gger. Set the php file to max permissions with CHMOD (rwxrwxrwx) but it still doesn't make any files...Have no idea how to logon to my webspace as Unix, I just use and FTP program...

Make sure that you have permission to write to files. Some hosts (Dreamhost is who I use and they did this) turned off local file writing permissions in their PHP config for security reasons. You can turn it back on but you have to set your PHP settings locally or something. Not very specific, I know, but this might be what you're seeing.

Hmm OK interesting. I have a CGI script that writes to a counter file OK, but maybe that's different...

Can't find anything in my websites cpanel :-(

I have written this test code:

<?php

// list of product buy pages
$files		= array("test1, "test2");
$lognames	= array("test1", "test2");










// all the boring processing stuff below...
$id = $_GET['id'];
if ($id >= 0 && $id < sizeof($files) && is_numeric($id)) {
	$log_filename = $lognames[$id] . ".log.txt";
	$redirect_url = $files[$id];
	$handle = @fopen($log_filename, "r");
	echo '<html><body>Got handle<br></body></html>';
	if ($handle) {
		echo '<html><body>Read File<br></body></html>';
		$curr_count = fgets($handle);
		fclose($handle);
	}
	else
		echo '<html><body>No file to read<br></body></html>';
	if (!is_numeric($curr_count))
		$curr_count = 1;
	else
		$curr_count++;
	$handle = @fopen($log_filename, "w");
	if ($handle) {
		echo '<html><body>wrote file<br></body></html>';
		fwrite($handle, $curr_count);
		fclose($handle);
	}
	else
		echo '<html><body>did not writefile<br></body></html>';

} else {
	echo '<html><body>Error: Invalid file specified</body></html>';
	exit();
}

?>


And I get this output:

Got handle
No file to read
did not writefile

So it is not writing it, sob. Why not, gah!

Sorted the php thing everyone, don't worry. Had to make a folder and give it full on permissions.

Cool, btw those unix commands are available in PHP.

I now have download tracking and buy button tracking :-) Plus a really nice reminder screen on the wrapped version.