Mr. Hunt
Mr. Hunt

Reputation: 11

Having An Issue Backing Up MY Database From Php

the issue that I'm having is with the fopen(), fwrite(), and fclose() function. This is the error I am receiving:

Warning: fopen(/db-backup-salisbury_school-20121128-021138.sql) [function.fopen]: failed to open stream: Permission denied in C:\inetpub\wwwroot\omarejaz\sbyschool\q5.php on line 177

Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\inetpub\wwwroot\omarejaz\sbyschool\q5.php on line 178

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\inetpub\wwwroot\omarejaz\sbyschool\q5.php on line 179

The code for this portion of php can be found below any help would be greatly appreciated!

 * Save SQL to file
 * @param string $sql
 */
protected function saveFile(&$sql, $outputDir = '.')
{
    if (!$sql) return false;

    try
    {
        $handle = fopen($outputDir.'/db-backup-'.$this->dbName.'-'.date("Ymd-His", time()).'.sql','w+');
        fwrite($handle, $sql);
        fclose($handle);
    }
    catch (Exception $e)
    {
        var_dump($e->getMessage());
        return false;
    }

    return true;
}

} ?>

Upvotes: 0

Views: 210

Answers (2)

rws907
rws907

Reputation: 777

I believe he is running a Windows Webserver with IIS - judging by the C:\Inetput\wwwroot directory structure... so in Windows you need to approach it this way

If your script is located here

C:\inetpub\wwwroot\omarejaz\sbyschool\q5.php

Then you need to specify the full directory using proper Windows Directory structure in your Output Directory. Keep in mind that you may have to escape the \'s since, in PHP \ is and escape character.

$OutputDir = "C:\\inetpub\\wwwroot\\omarejaz\\sbyschool\\tmp";

Then in Windows make sure that you have the proper permissions set on the "tmp" folder to allow for IIS to write to it.

Upvotes: 1

soulfreshner
soulfreshner

Reputation: 366

You need to set $outputDir to '/tmp' or something where your PHP script can write to.

If the script is run from the command line, the user running the script needs write permissions. If it is run from the web browser, apache or 'nobody' needs write permissions.

Use chmod -R a+w <outputfolder> to set write permissions for all users.

If you do not have access to the web server, you can set the folder permissions using filezilla as per this video: http://www.youtube.com/watch?v=oq0oM2w9lcQ

If you can get the contents of the file using file_get_contents() and just echo it to the standard output. If you don't print anything else, your browser will prompt you to download the file.

Upvotes: 0

Related Questions