I'm inserting a simple, well-rated script from hotscripts to randomly display quotes which are stored in a .txt file. The .txt file is not being recognized by the php script. I think maybe there is more to the .txt file than just entering quotes onto the blank page, seperated by hitting [enter], but I'm not sure. Thanks for any help.
This is the extent of the php script:
Code:
// The name of your quote file
$quote_file = "quotes.txt";
// Open the quote file
$fp = fopen($quote_file, "r"); error message says quotes.txt not found for this line, the .txt file is in Cwd of .php script
// Read the contents and tokenize the file to individual quotes
$quotes = fread($fp, filesize($quote_file));
$array = explode("\n",$quotes); wondering I have to specify the # of quotes for 'n'?
fclose($fp);
// Find a random quote
srand((double)microtime()*1000000);
$array_index = (rand(1, sizeof($array)) - 1);
// Show the random quote
echo $array[$array_index];
?>
Instructions:
In order to make it work you first have to edit quotes.txt and put all the quotes you want displayed in it. Is there more to the .txt file than appears?
Nope, nothing more. You're just assuming you have data to work with and the file is read properly.
PHP Code:
if ($fp = @fopen($quote_file, 'r')) { $sQuotes = ""; while (!feof($fp)) { $sQuotes .= fread($fp, filesize($quote_file)); } if (!empty($sQuotes)) { $aQuotes = explode(PHP_EOL, $quotes); // you need to specify nothing here. } fclose($fp); // you haven't needed to seed random generation for like, 15 years. if (count($array) > 0) { print $array[array_rand($array, 1)]; } else { print 'There are no quotes to show.'; } } else { $e = error_get_last(); printf("Cannot open file for reading: %s" . PHP_EOL, $e['message']); }
Since you have error reporting enabled and not suppressed, than you can see the message itself in the fopen call. That would be your issue, you simply cannot read the file. Causes can be: no file found on the path provided, and no permissions to read the file. The error will tell you the cause. If it says its not found, than the path is incorrect. If its in the same directory as this file, that indicates that this file is included into a different file on a differing path. You can set the path using __DIR__ . '/quotes.txt'; instead.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Yeah, you can give it a shot I didn't test it. Works alright in my head though
If you aren't learning about file handling or anything like that, then this can be done much easier:
__FILE__ differs in that its the entire path including "this" script. Prior to. . . 5.2.x I think it was, __DIR__ hadn't existed, so we would use dirname(__FILE__) in its place.
It is IMO wise to always include or open files relative to "this" script. Since you can include "this" script in a different one with a different path, PHP takes its cwd as that of the executing script, not the inclusions. Using relatives from __DIR__ will always produce the correct paths.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php