Instead of opening the file multiple times while appending text, I want to open the file once and append all the text necessary and close the file at the end of append session.
I come up with error message saying " Warning: fwrite(): supplied argument is not a valid stream resource"
The file at action: http://raghavan20.allhyper.com/backupDatabase.php
//backupDatabase.php
Code: Select all
<?php
//database connection provided
?>
<?php
//File functions
//open a file and return the pointer
function openFile($fileName){
return fopen($fileName, 'r+') or die("can't open file");
}
//append to file
function appendToFile($str, $filePointer){
fwrite($filePointer, $str);
}
//change file permission type
function changeFilePointerType($fileName, $accessType){
return fopen($fileName, $accessType) or die("can't open file");
}
//close file
function closeFile($filePointer){
fclose($filePointer);
}
?>
<?php
//open file
$filePointer = openFile("myfile.sql");
//change file permission to append
$filePointer = changeFilePointerType("myfile.sql", "a");
//start of file
echo "$$$"."<br />";
//find all databases
$query = "show databases";
$result = mysql_query($query);
if (is_resource($result)){
if (mysql_num_rows($result) > 0){
while ($row = mysql_fetch_row($result)){
//allDatabases[i] = $row[0]; //append every database to the array
echo "##$row[0]##"."<br />"; //print database
$str = "##$row[0]##";
appendToFile($str, $filePointer); //append string to a file
findAllTables($row[0], $filePointer);
}
}else{
echo "No database found!!!";
}
}
//end of file
echo "$$$"."<br />";
//close file
closeFile($filePointer);
?>
<?php
//find all tables
function findAllTables($db, &$filePointer){
$query = "show tables from $db";
$result = mysql_query($query);
if (is_resource($result)){
if (mysql_num_rows($result)){
while ($row1 = mysql_fetch_row($result)){
//allDatabases[i] = $row1[0]; //append every database to the array
echo "#$row1[0]#"."<br />"; //print table
$str = "#$row[0]#";
appendToFile($str, $filePointer); //append string to a file
findAllFields($row1[0], $db, $filePointer);
}
}
}
}
?>
<?
//find all fields
function findAllFields($table, $db, &$filePointer){
$query = "show columns from $db.$table";
$result = mysql_query($query);
if (is_resource($result)){
if (mysql_num_rows($result)){
while ($row1 = mysql_fetch_row($result)){
echo "-"."$row1[0]"."-"."<br />";
$str = "$row[0]";
appendToFile($str, $filePointer); //append string to a file
}
}
}
}
?>