Reputation: 2094
my code works successfully . but i dont know how to save the .csv file to a seperate directory D:\xampp\htdocs\works_try\export_files. and my file resides in D:\xampp\htdocs\works_try\master\file.php my code given below .
<?php
$server='localhost';
$login='root';
$password='';
$db='tc';
$filename='export1.csv';
$conn=mysqli_connect($server, $login, $password,$db);
$fp = fopen('export_files/../'.$filename, "w");
$sql="SELECT * FROM student";
$res = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($res);
$line = "";
$comma = "";
foreach($row as $name => $value) {
$line .= $comma . '"' . str_replace('"', '""', $name) . '"';
$comma = ",";
}
$line .= "\n";
fputs($fp, $line);
mysqli_data_seek($res, 0);
while($row = mysqli_fetch_assoc($res)) {
$line = "";
$comma = "";
foreach($row as $value) {
$line .= $comma . '"' . str_replace('"', '""', $value) . '"';
$comma = ",";
}
$line .= "\n";
fputs($fp, $line);
}
fclose($fp);
echo " success :) !!!!!!!!!!!!!!!!!!!!!! Please check the file ";
?>
i should save the .csv file in folder_name export_files and my php code runs in the folder_name master.my problem is csv file getting saved in folder_name master itself.where file should save in export_files.
Upvotes: 1
Views: 1353
Reputation: 2094
if you use fopen($filename, "w") then file will be save inside the folder. if you want to save the file present outside folder ie(export_files is separate folder and file present in seperate folder ) then try this
fopen('../export_files/'.$filename, "w");
the file will be saved inside export_files folder
Upvotes: 2
Reputation: 1663
you can also use the following code then the path will be in relation to the directory of the running script. e.g. as example going one directory up and in export_files.
$filename = dirname(__FILE__) .'/../export_files/export1.csv';
you can also use other constants like __DIR__
see http://php.net/manual/de/language.constants.predefined.php
Upvotes: 0
Reputation: 360
If you want it in the root folder of the site use this:
$filename='export1.csv';
$conn=mysqli_connect($server, $login, $password,$db);
$fp = fopen('./'.$filename, "w");
Upvotes: 0