cabaldemon Posted February 11, 2008 Share Posted February 11, 2008 i have a code that reads from a directory and echose the directory out to the php web page.what it wont do is write to a file so that a music player or my other a video player can read from this code is to write to the file // directory path can be either absolute or relative $dirPath = 'music/.'; $myFile = "music.m3u"; $stringData = "$file"; // open the specified directory and check if it's opened successfully if ($handle = opendir($dirPath)) { // keep reading the directory entries 'til the end while (false !== ($file = readdir($handle))) // just skip the reference to current and parent directory if ($file != "." && $file != "..") { if (is_dir("$dirPath/$file")) { // found a directory, do something with it? $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, $stringData); fclose($fh); } } // ALWAYS remember to close what you opened closedir($handle); } ?> can anyone help thank you Quote Link to comment https://forums.phpfreaks.com/topic/90569-read-from-a-directory-write-to-a-file/ Share on other sites More sharing options...
resago Posted February 12, 2008 Share Posted February 12, 2008 permissions? you may have to 777 the directory you are trying to write to before hand. Quote Link to comment https://forums.phpfreaks.com/topic/90569-read-from-a-directory-write-to-a-file/#findComment-464454 Share on other sites More sharing options...
cabaldemon Posted February 13, 2008 Author Share Posted February 13, 2008 thank you for that answer i am on a windows machine because of the kinda servers i host but does that code look ok cause i cant get it to work Quote Link to comment https://forums.phpfreaks.com/topic/90569-read-from-a-directory-write-to-a-file/#findComment-465421 Share on other sites More sharing options...
cabaldemon Posted February 13, 2008 Author Share Posted February 13, 2008 i can get it to work on a web page but i cant get it to copy to a file Quote Link to comment https://forums.phpfreaks.com/topic/90569-read-from-a-directory-write-to-a-file/#findComment-465423 Share on other sites More sharing options...
trq Posted February 13, 2008 Share Posted February 13, 2008 Firstly, your opening the file within a loop, not good. Secondly, your opening it in write only mode so it will not append each line. And third and finally, the $stringData variable your trying to write to the file contains nothing. Try... <?php $dirPath = 'music/'; $myFile = "music.m3u"; $fh = fopen($myFile, 'a') or die("can't open file"); if ($handle = opendir($dirPath)) { while (false !== ($file = readdir($handle))) if ($file != "." && $file != "..") { if (is_dir("$dirPath/$file")) { fwrite($fh, "$dirPath/$file\n"); } } } } fclose($fh); closedir($handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/90569-read-from-a-directory-write-to-a-file/#findComment-465435 Share on other sites More sharing options...
cabaldemon Posted February 13, 2008 Author Share Posted February 13, 2008 yes soz i forgot to write the begining <?php and $stringdata shouldnt have been in there i tried your code and it still wont write to a file i cant figure it out Quote Link to comment https://forums.phpfreaks.com/topic/90569-read-from-a-directory-write-to-a-file/#findComment-465482 Share on other sites More sharing options...
cabaldemon Posted February 14, 2008 Author Share Posted February 14, 2008 bump Quote Link to comment https://forums.phpfreaks.com/topic/90569-read-from-a-directory-write-to-a-file/#findComment-466390 Share on other sites More sharing options...
cabaldemon Posted February 14, 2008 Author Share Posted February 14, 2008 can someone please help please i still cannot get this code to make it write to a file from read of directory files thank you in advance Quote Link to comment https://forums.phpfreaks.com/topic/90569-read-from-a-directory-write-to-a-file/#findComment-466480 Share on other sites More sharing options...
trq Posted February 14, 2008 Share Posted February 14, 2008 Are you getting an error about not being able to open the file? What exactly do you want to write to the file? Quote Link to comment https://forums.phpfreaks.com/topic/90569-read-from-a-directory-write-to-a-file/#findComment-466484 Share on other sites More sharing options...
cabaldemon Posted February 14, 2008 Author Share Posted February 14, 2008 ok i finally got it to write to a file by setting the absolute path where the directory is but now it writes the exact path instead of the file Quote Link to comment https://forums.phpfreaks.com/topic/90569-read-from-a-directory-write-to-a-file/#findComment-466507 Share on other sites More sharing options...
cabaldemon Posted February 16, 2008 Author Share Posted February 16, 2008 can anyone help with that because i still cannot fix this thank you Quote Link to comment https://forums.phpfreaks.com/topic/90569-read-from-a-directory-write-to-a-file/#findComment-468105 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.