Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php include not opening .txt file

    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?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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($fpfilesize($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($array1)];
        }
        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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, This is what I changed, and it didn't work. I tried __FILE__ too.

    $fp = fopen(__DIR__ . '/quotes.txt', "r");

    Also, are you suggesting updated code for the original script?

    I'm afraid I've gotten in over my head on this, but I'd like to finish what I started.

    Thanks again.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    <?php

    $sFile 
    __DIR__ '/quotes.txt';
    if (
    false !== ($aQuotes file($sFileFILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES)))
    {
        print 
    $aQuotes[array_rand($aQuotes1)];
    }
    else
    {
        print 
    'Cannot retrieve quotes!';
    }
    __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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •