punky79 Posted March 7, 2008 Share Posted March 7, 2008 I have code written to generate xml data in PHP from my MySQL database. I can display this in my browser using "echo" but wbat I really want is to write this xml data to a seperate xml file so that I can have flash use it. Here is my code: <?php require_once('Connections/PhotoABC.php'); ?> <?php if (!isset($_SESSION)) { session_start(); } $sqluserid = "SELECT `user_id` FROM `user` WHERE `username` = '".$_SESSION['MM_Username']."'"; $useridqueryresult = mysql_query($sqluserid) or die (mysql_error()); if (mysql_num_rows($useridqueryresult) > 0) { $row=mysql_fetch_assoc($useridqueryresult); $userid=$row['user_id']; } $sqlimagename = "SELECT `image_name` FROM `gallery` WHERE `user_id` = '$userid'"; $imagenamequeryresult = mysql_query($sqlimagename) or die (mysql_error()); $xml_output = "<?xml version=\"1.0\"?>\n"; $xml_output .= "<images>\n"; for($x = 0 ; $x < mysql_num_rows($imagenamequeryresult) ; $x++){ $row = mysql_fetch_assoc($imagenamequeryresult); $xml_output .= "\t<image>\n"; xml_output .= "\t\t<image>" . $row['image_name'] . "</image>\n"; $xml_output .= "\t</image>\n"; } $xml_output .= "</images>"; echo $xml_output; ?> I would like to write this xml data to a new xml file called "images" and to save it to this directory "C:\htdocs\PhotoABC\upload_test\Jordan" Any help would be great Link to comment https://forums.phpfreaks.com/topic/94931-xml-php-mysql-writing-to-a-xml-file/ Share on other sites More sharing options...
Barand Posted March 7, 2008 Share Posted March 7, 2008 www.php.net/file_put_contents Link to comment https://forums.phpfreaks.com/topic/94931-xml-php-mysql-writing-to-a-xml-file/#findComment-486315 Share on other sites More sharing options...
punky79 Posted March 7, 2008 Author Share Posted March 7, 2008 thanks for reply I have been trying to use this function but I keep getting this error: Warning: file_put_contents(C:\htdocs\PhotoABC\upload_test\J ordan) [function.file-put-contents]: failed to open stream: Permission denied in C:\htdocs\PhotoABC\xml.php on line 43 I dont know if I need to open inout and output streams or just if my permissions are not set properly? Do u have any suggestions? $filename= 'C://htdocs//PhotoABC//upload_test//'.$_SESSION['MM_Username']; file_put_contents($filename ,$xml_output); Link to comment https://forums.phpfreaks.com/topic/94931-xml-php-mysql-writing-to-a-xml-file/#findComment-486331 Share on other sites More sharing options...
Barand Posted March 7, 2008 Share Posted March 7, 2008 Sounds like you don't have write permission in that directory Link to comment https://forums.phpfreaks.com/topic/94931-xml-php-mysql-writing-to-a-xml-file/#findComment-486333 Share on other sites More sharing options...
punky79 Posted March 7, 2008 Author Share Posted March 7, 2008 Than ks for your reply I have managed to get it working now. Here is the code I used: $filenamepath .= "images.xml"; $fp = fopen($filenamepath,'w'); $write = fwrite($fp,$xml_output); echo $xml_output; Thanks for your help Link to comment https://forums.phpfreaks.com/topic/94931-xml-php-mysql-writing-to-a-xml-file/#findComment-486341 Share on other sites More sharing options...
Barand Posted March 7, 2008 Share Posted March 7, 2008 Don't forget to fclose() the file. (file_put_contents() opens, writes, closes all in one statement) Link to comment https://forums.phpfreaks.com/topic/94931-xml-php-mysql-writing-to-a-xml-file/#findComment-486356 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.