PDA

View Full Version : fopen - path or permissions problem?


labrat
02-07-2005, 09:42 PM
I have a php program that is trying to create a backup file of data of a MySQL database. I didn't write this code and there are so many includes, I'm not sure where to look for the problem. When I run the code, I get "Cannot open file backups/tng_sources.bak". Now, there is a config file where I set the $rootpath and $backuppath and they are set as "/home/[hrbasedomain]/public_html/genealogy/" and "backups" respectively so I'm not sure why the error msg seems to exclude the $rootpath part.


foreach( $tablelist as $table ) {
$filename = "$rootpath$backuppath/$table.bak";
if( file_exists( $filename ) ) unlink( $filename );
$fp = @fopen( $filename, "w" );
if( !$fp ) { die ( "$admtext[cannotopen] $filename" ); }
flock( $fp, LOCK_EX );
$nextchunk = -1;
$numrows = 0;

do {
$nextone = $nextchunk + 1;
$nextchunk += $largechunk;
$query = "SELECT * FROM $table LIMIT $nextone, $largechunk";
$result = mysql_query($query) or die ("$admtext[cannotexecutequery]: $query");
if( $result ) {
$numrows = mysql_num_rows( $result );
while( $row = mysql_fetch_array( $result, MYSQL_NUM ) ) {
$line = "";
for( $i = 0; $i < sizeof( $row ); $i++ ) {
if( $line ) $line .= ",";
$element = addslashes( $row[$i] );
$line .= "\"$element\"";
}
$line .= "\n";
fwrite( $fp, "$line" );
}
mysql_free_result( $result );
}
} while ( $numrows );

flock( $fp, LOCK_UN );
fclose( $fp );
}


I have a server set up on my home machine (WinXP/Apache/PHP/MySQL) and I tried it there. It has no problem running on my local setup (but the rootpath is "C:\Web\localhost\genealogy) so I was wondering if it might be a permissions error. Of course there are other variables that are different so I'm not sure.

Please let me know if you can point me in the right direction or if I left out any important details.

TIA,
-labrat

Viper007Bond
02-08-2005, 01:40 AM
Is "Cannot open file backups/tng_sources.bak" the exact and full error message?

labrat
02-08-2005, 02:15 AM
Yes it is, when I try to back up the "tng_sources" table. Only the table name changes and I get the same message (different table name) no matter what table I try to back up. That's why I was wondering about the path name provided for the variable vs. that returned in the message. They don't seem to match.

labrat
02-08-2005, 03:09 AM
OK, I got it. This program is so messed up. The web page for administration of the config file shows the $rootpath as what I posted, but when I went into the actual file by way of the HR webftp, I found that it was set to an empty string. DOH! I set the string to what I thought it was already set to (as I posted) and that fixed everything.

Sorry to bother you all.

-labrat

Viper007Bond
02-08-2005, 07:29 AM
Glad you got it all worked out. *thumbs up*