problem with fopen on the server (works perfectly in local)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
orangeapple
Forum Commoner
Posts: 70
Joined: Tue Jan 06, 2004 1:24 pm
Location: Geneva / Switzerland

problem with fopen on the server (works perfectly in local)

Post by orangeapple »

Hi there,

Have a problem with this backup file creation which works perfectly in local but doesn't once installed on the server :

Code: Select all

else {

  $fdb = fopen ("$path","wb");



//find increment number table alarm
$query_find_increment = "SELECT * FROM alarm ORDER BY id";
            $result_increment = mysql_query ($query_find_increment) or die ("Could not understand the query ");

                while ($row = mysql_fetch_array($result_increment))
                   {
                            $num_increment=$row['id'];
                            
}
$num_increment=$num_increment+1;


  
   $string1 ="CREATE TABLE `alarm` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `msg_from` varchar(60) default NULL,
  `msg_date` datetime default NULL,
  `msg_gmt` varchar(5) default NULL,
  `alarm_mac_id` varchar(10) default NULL,
  `alarm_date` datetime default NULL,
  `alarm_id` varchar(10) default NULL,
  `alarm_value` varchar(10) default NULL,
  `alarm_ack` char(1) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=$num_increment ;";
 fwrite($fdb,$string1);


$query = "SELECT * FROM alarm ORDER BY id";
            $result = mysql_query ($query) or die ("Could not understand the query ");

                while ($row = mysql_fetch_array($result))
                   {
                            $id=$row['id'];
                            $msg_from=$row['msg_from'];
                            $msg_date=$row['msg_date'];
				    $msg_gmt=$row['msg_gmt'];
                            $alarm_mac_id=$row['alarm_mac_id'];
                            $alarm_date=$row['alarm_date'];
                            $alarm_id=$row['alarm_id'];
					$alarm_value=$row['alarm_value'];
					$alarm_ack=$row['alarm_ack'];







                        $string2="INSERT INTO alarm VALUES ('$id', '$msg_from', '$msg_date', '$msg_gmt', '$alarm_mac_id', '$alarm_date', '$alarm_id', '$alarm_value', '$alarm_ack');";
                        fwrite($fdb,$string2);

                }




   
   $string1 ="CREATE TABLE `alarm_description` (
  `id` varchar(10) NOT NULL default '',
  `level` varchar(7) default NULL,
  `name` varchar(20) default NULL,
  `value` varchar(20) default NULL,
  `description` text,
  `send` char(1) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;";

 fwrite($fdb,$string1);


$query = "SELECT * FROM alarm_description ORDER BY id";
            $result = mysql_query ($query) or die ("Could not understand the query ");

                while ($row = mysql_fetch_array($result))
                   {
                            $id=$row['id'];
                            $level=$row['level'];
                            $name=$row['name'];
				    $value=$row['value'];
                            $description=$row['description'];
                            $send=$row['send'];
                           





                        $string2="INSERT INTO alarm_description VALUES ('$id', '$level', '$name', '$value', '$description', '$send');";
                        fwrite($fdb,$string2);

                }



//find increment number table equipment
$query_find_increment = "SELECT * FROM equipment ORDER BY equipid";
            $result_increment = mysql_query ($query_find_increment) or die ("Could not understand the query ");

                while ($row = mysql_fetch_array($result_increment))
                   {
                            $num_increment=$row['equipid'];
                            
}
$num_increment=$num_increment+1;


  
   $string1 ="CREATE TABLE `equipment` (
  `equipid` int(5) unsigned NOT NULL auto_increment,
  `equipowner` varchar(8) default NULL,
  `equipident` varchar(10) default NULL,
  `equiptype` varchar(20) default NULL,
  `equiplocation` varchar(50) default NULL,
  `tzone` varchar(5) default NULL,
  KEY `equipid` (`equipid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=$num_increment ;";
 fwrite($fdb,$string1);


$query = "SELECT * FROM equipment ORDER BY equipid";
            $result = mysql_query ($query) or die ("Could not understand the query ");

                while ($row = mysql_fetch_array($result))
                   {
                            $equipid=$row['equipid'];
                            $equipowner=$row['equipowner'];
                            $equipident=$row['equipident'];
				    $equiptype=$row['equiptype'];
                            $equiplocation=$row['equiplocation'];
                            $tzone=$row['tzone'];
                          






                        $string2="INSERT INTO equipment VALUES ('$equipid', '$equipowner', '$equipident', '$equiptype', '$equiplocation', '$tzone');";
                        fwrite($fdb,$string2);

                }







   $string1 ="CREATE TABLE `level_description` (
  `level` varchar(7) NOT NULL default '',
  `name` varchar(20) default NULL,
  `description` text,
  `send` char(1) default NULL,
  PRIMARY KEY  (`level`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;";

 fwrite($fdb,$string1);


$query = "SELECT * FROM level_description ORDER BY level";
            $result = mysql_query ($query) or die ("Could not understand the query ");

                while ($row = mysql_fetch_array($result))
                   {
                            $level=$row['level'];
                            $name=$row['name'];
                            $description=$row['description'];
				    $send=$row['send'];
                                                       





                        $string2="INSERT INTO level_description VALUES ('$level', '$name', '$description', '$send');";
                        fwrite($fdb,$string2);

                }







//find increment number table login
$query_find_increment = "SELECT * FROM login ORDER BY numlogin";
            $result_increment = mysql_query ($query_find_increment) or die ("Could not understand the query ");

                while ($row = mysql_fetch_array($result_increment))
                   {
                            $num_increment=$row['numlogin'];
                            
}

$num_increment=$num_increment+1;

    
   $string1 ="CREATE TABLE `login` (
  `numlogin` int(5) unsigned NOT NULL auto_increment,
  `login` varchar(10) NOT NULL default '',
  `randompass` varchar(35) default NULL,
  `logindate` varchar(30) default NULL,
  `logoutdate` varchar(30) default NULL,
  `unixtime` varchar(20) default NULL,
  KEY `numlogin` (`numlogin`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=$num_increment ;";
 fwrite($fdb,$string1);


$query = "SELECT * FROM login ORDER BY numlogin";
            $result = mysql_query ($query) or die ("Could not understand the query ");

                while ($row = mysql_fetch_array($result))
                   {
                            $numlogin=$row['numlogin'];
                            $login=$row['login'];
                            $randompass=$row['randompass'];
                            $logindate=$row['logindate'];
                            $logoutdate=$row['logoutdate'];
                            $unixtime=$row['unixtime'];




                            $login = addslashes ($login);



                        $string2="INSERT INTO login VALUES ('$numlogin', '$login', '$randompass', '$logindate', '$logoutdate', '$unixtime');";
                        fwrite($fdb,$string2);
                }


//find increment number table passe
$query_find_increment = "SELECT * FROM passe ORDER BY id";
            $result_increment = mysql_query ($query_find_increment) or die ("Could not understand the query ");

                while ($row = mysql_fetch_array($result_increment))
                   {
                            $num_increment=$row['id'];
                            
}

$num_increment=$num_increment+1;

    
   $string1 ="CREATE TABLE `passe` (
  `id` int(5) unsigned NOT NULL auto_increment,
  `login` varchar(8) NOT NULL default '',
  `passe` varchar(20) NOT NULL default '',
  `privilege` char(1) NOT NULL default '',
  `email` varchar(250) NOT NULL default '',
  `name` varchar(20) default NULL,
  `firstname` varchar(20) default NULL,
  `master` varchar(10) default NULL,
  `sex` varchar(6) default NULL,
  `company` varchar(20) default NULL,
  `autologout` varchar(5) NOT NULL default '7200',
  `usertzone` varchar(5) NOT NULL default '0',
  `emailsend` varchar(5) NOT NULL default '1',
  UNIQUE KEY `login_2` (`login`),
  UNIQUE KEY `login_3` (`login`),
  KEY `id` (`id`),
  FULLTEXT KEY `login` (`login`),
  FULLTEXT KEY `passe` (`passe`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=$num_increment ;";
 fwrite($fdb,$string1);


$query = "SELECT * FROM passe ORDER BY id";
            $result = mysql_query ($query) or die ("Could not understand the query ");

                while ($row = mysql_fetch_array($result))
                   {
                            $id=$row['id'];
                            $login=$row['login'];
                            $passe=$row['passe'];
                            $privilege=$row['privilege'];
                            $email=$row['email'];
                            $name=$row['name'];
                            $firstname=$row['firstname'];
                            $master=$row['master'];
                            $sex=$row['sex'];
                            $company=$row['company'];
                            $autologout=$row['autologout'];
                            $usertzone=$row['usertzone'];
                            $emailsend=$row['emailsend'];




                            $login = addslashes ($login);



                        $string2="INSERT INTO passe VALUES ('$id', '$login', '$passe', '$privilege', '$email', '$name', '$firstname', '$master', '$sex', '$company', '$autologout', '$usertzone', '$emailsend');";
                        fwrite($fdb,$string2);

                }






            $fclose = fclose($fdb);

 }
do you have an idea why ???
Thanks for your answer
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it'd really help to know what error it's throwing, if any... I'm not reading all that code, so I'll simply guess your script doesn't have permission to write to the folder or file in supplied by $path
User avatar
orangeapple
Forum Commoner
Posts: 70
Joined: Tue Jan 06, 2004 1:24 pm
Location: Geneva / Switzerland

Post by orangeapple »

$path = C:/backup.sql

in local use the file is created whereas on the server the file is simply not created (without displaying any error warning).
Does it have to do with the fact that the php file is executed on the server and it can't save on the client C:/ disk ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if the server is a unix server, it won't save it ever (as far as I know), using that path. If the path doesn't exist, or your script doesn't have write access privledges to that path it'll also fail.

Check your error logs, there likely is an error there...
User avatar
orangeapple
Forum Commoner
Posts: 70
Joined: Tue Jan 06, 2004 1:24 pm
Location: Geneva / Switzerland

Post by orangeapple »

yes, it is a unix server.

Is there a way to save that file to a client pc ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

not directly (often) ... you could use FTP if written to the local path for instance, or create the "file" by simply dumping it to screen or passing the necessary headers to initiate a "download" of it.. the latter two wouldn't actually create a physical file, instead you'd just echo data out.
User avatar
orangeapple
Forum Commoner
Posts: 70
Joined: Tue Jan 06, 2004 1:24 pm
Location: Geneva / Switzerland

Post by orangeapple »

would be interested in the initiate download option.
could you tell me a bit more about this ?
tks
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Include this before outputting anything to the client

Code: Select all

header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=backup.sql");
This will initiate a dowload of the text file
User avatar
orangeapple
Forum Commoner
Posts: 70
Joined: Tue Jan 06, 2004 1:24 pm
Location: Geneva / Switzerland

Post by orangeapple »

mmmmmhhh !

...works well, except that it seems not possible to put a variable instead of the file name. This :

Code: Select all

$path=$_POST['path'];
$randompass=$_POST['randompass'];

header("Content-Type: text/plain"); 
header("Content-Disposition: attachment; filename='$path'");
saves the file under the php file name which generates the download instead of taking the value of $path... :?

anyone an idea ?
Tks a lot !
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that's the fun of working with download scripts.. you must send the correct headers. IE is the biggest pain since they changed the restrictions of naming for downloads..
User avatar
orangeapple
Forum Commoner
Posts: 70
Joined: Tue Jan 06, 2004 1:24 pm
Location: Geneva / Switzerland

Post by orangeapple »

Ok thanks a lot guys !
Post Reply