Forums | MacLife
You are not logged in.
#1 2008-04-02 2:07 pm
problems creating a new file - php/mysql
the goal here is to produce a .csv file that can be opened in Excel. i've hit a wall. keep getting the following error:
Code:
Warning: fwrite(): supplied argument is not a valid stream resource in /filename.php on line ### Warning: fclose(): supplied argument is not a valid stream resource in /fliename.php on line ###
the script looks like this:
Code:
$excel_to_sql_query = mysql_query($excel_to_sql) or die(mysql_error());
$content_string = "First Name, Spouse Name, Last Name, Email, Phone\r\n";
while($row = mysql_fetch_array($excel_to_sql_query, MYSQL_ASSOC)){
$content_string .= $row['FirstName'].",".$row['SpouseName'].",".$row['LastName'].",".$row['email'].",".$row['Phone']."\r\n";
}
$todays_date = date('m_d_Y');
$filename = 'csv_export/'.$_SESSION['md_empID'].'_sami_export_'.$todays_date.'.csv';
chmod($filename, 777);
fopen($filename, "w+");
if(is_writable($filename)){
echo '<p>'.$filename.' is writable</p>';
fwrite($filename, $content_string);
}
fclose($filename);getting this error on my Mac OS 10.4 localhost with PHP 5/ MySQL 5 installed. changed the 'csv_export' folder to have 777 permissions. not sure what else to do.
pre-emptive thanks to all who respond
b
Last edited by b_dubb (2008-04-02 2:07 pm)
"The Fates lead he who will; he who won't, they drag." - Seneca
Offline
#2 2008-04-02 2:22 pm
Re: problems creating a new file - php/mysql
Does the file itself have correct permissions? God knows that's screwed me up a few times.
Offline
#3 2008-04-02 2:33 pm
Re: problems creating a new file - php/mysql
You probably want something more like this:
Code:
$handle = fopen($filename, "w+");
if (is_writable($handle)) { // may actually be $filename, don't remember
fwrite($handle, $contents);
}
fclose($handle);Also, make sure the file is opening correctly.
Basseq is me, John Whittet.
(Finishing the remainder of the thought expressed in the post has been left as an exercise for the reader.)
Offline
#4 2008-04-02 2:58 pm
Re: problems creating a new file - php/mysql
in my example ... $filename is the 'sub_directory' ('csv_export') with '/filename.csv' appended
since an empty file is being created in that directory ... i'm assuming that the problem is with the fwrite() function. yeah ... this is a headscratcher.
"The Fates lead he who will; he who won't, they drag." - Seneca
Offline
#5 2008-04-02 4:15 pm
Re: problems creating a new file - php/mysql
corrected code:
Code:
$excel_to_sql_query = mysql_query($excel_to_sql) or die(mysql_error());
$content_string = "First Name, Spouse Name, Last Name, Email, Phone\r\n";
while($row = mysql_fetch_array($excel_to_sql_query, MYSQL_ASSOC)){
$content_string .= $row['FirstName'].",".$row['SpouseName'].",".$row['LastName'].",".$row['email'].",".$row['Phone']."\r\n";
}
$todays_date = date('m_d_Y');
$filename = "csv_export/".$_SESSION['md_empID']."_sami_export_".$todays_date.".csv";
if(file_exists($filename)){
unlink($filename);
}
$document_link = fopen($filename, "w+");
if(is_writable($filename)){
fwrite($document_link, utf8_encode($content_string));
}
fclose($document_link);i had to change the fwrite() and declare the fopen() as a variable. reminds me of how mysql_connect() works. except connecting to a document. anyway ... thanks to all who responded. hope this helps the next guy out.
b
"The Fates lead he who will; he who won't, they drag." - Seneca
Offline
#6 2008-04-02 6:23 pm
Re: problems creating a new file - php/mysql
Your unlink code is a little redundant, as w+ truncates anyway (e.g. as soon as you delete the file, it's created again).
Basseq is me, John Whittet.
(Finishing the remainder of the thought expressed in the post has been left as an exercise for the reader.)
Offline
