aquanuke Posted September 7, 2011 Share Posted September 7, 2011 Hi I want to read multiple .txt files and then if an action requires select one. <? // /home/person-mike.txt // /home/person-sarah.txt // /home/person-paul.txt while ($row = sqlite_fetch_array($result)) { $name = $row['name']; $filename = "/home/person-$name.txt"; $handle = fopen($filename, "r"); $sendrequest = fread($handle, filesize($filename)); fclose($handle); } ?> <? if ( $client == "sarah" ) { echo "$sendrequest[sarah]"; ?> How do I do the last bit to have a variable like $sendrequest[sarah] Quote Link to comment https://forums.phpfreaks.com/topic/246663-fopen-multiple-files-then-select-one/ Share on other sites More sharing options...
btherl Posted September 7, 2011 Share Posted September 7, 2011 Try this: <?php // /home/person-mike.txt // /home/person-sarah.txt // /home/person-paul.txt $sendrequest = array(); while ($row = sqlite_fetch_array($result)) { $name = $row['name']; $filename = "/home/person-$name.txt"; $handle = fopen($filename, "r"); $sendrequest[$name] = fread($handle, filesize($filename)); fclose($handle); } ?> <?php if ( $client == "sarah" ) { echo "$sendrequest[sarah]"; } // Or to simplify that last "if" condition: echo $sendrequest[$client]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/246663-fopen-multiple-files-then-select-one/#findComment-1266617 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 Why are you mixing flat files with a database? Is there any reason in specific you don't have ALL of the data in SQLite? If you are getting the text file from an outside source, it might be a better solution to have an automated script push that data to the DB for you. It will definitely save lines of code and processing time. If this is out of the picture, a solution that takes less memory might go something like this: <?php $client = 'sarah'; if( ($data = getUserFile($client)) === FALSE ) echo 'Unable to find '.$client.'\'s data'; else echo $data; function getUserFile( $username ) { // These could be arguments rather than hard coded $pre = '/home/person-'; $suf = '.txt'; if( ($data = @file_get_contents($pre.$username.$suf)) === FALSE ) return FALSE; return $data; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246663-fopen-multiple-files-then-select-one/#findComment-1266648 Share on other sites More sharing options...
aquanuke Posted September 8, 2011 Author Share Posted September 8, 2011 Thanks btherl worked perfect. Thanks also xyph, there actually xml files im reading I just put .txt on here to simplify things. For my needs I think btherl example works better for me. Quote Link to comment https://forums.phpfreaks.com/topic/246663-fopen-multiple-files-then-select-one/#findComment-1266653 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.