Getting file

Discussion in 'PHP' started by Jen-, Jul 20, 2007.

  1. #1
    Hello, I'm trying to get a file per second or minute on a web site. For testing I am trying per second. No .txt file in the array is being included on a second. Can I number the items listed in the array so it will grab one of them depending on what second it is? If so, how is that done with this? Thanks very much!


    <?php
    $getdate = date("s");
    $this=array("file01.txt","file02.txt");
    $getfiles=count($this);
    $analyze = explode(".", $getfiles[$getdate]);
    $result = array_pop($analyze);
    if ($result == "txt")
    {include($getfiles[$getdate]);
    }?>
    PHP:

     
    Jen-, Jul 20, 2007 IP
  2. mistamagoo

    mistamagoo Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The problem is here: $getfiles=count($this);

    $getfiles is being set to an integer and not an array as you seem to be expecting on the next line.

    Just remove the "count" and you should have no trouble: $getfiles=($this);

    edit:

    You have what seems to be unnecessary copying also (with $this and $getfiles). I'm not sure what your future plans are for the use of the code but at the very least I'd change it to this:

    <?php
    $getdate = date("s");
    $getfiles=array("file01.txt","file02.txt");
    $filecount=count($getfiles);
    $analyze = explode(".", $getfiles[$getdate]);
    $result = array_pop($analyze);
    if ($result == "txt")
    {include($getfiles[$getdate]);
    }
    ?>

    #------------------------
    The exploding and popping also seems unnecessary. You might just want to test $getfiles[$getdate] with a regular expression to see if it ends with ".txt", rather then exploding and popping, since you'd be able to have it all in the "if":

    if(preg_match("/\.txt$/i", $getfiles[$getdate]) == 1) #If $getfiles[$getdate] ends with .txt
    {
    include($getfiles[$getdate]);
    }


    #------------------------
    <?php
    $getdate = date("s");
    $getfiles=array("file01.txt","file02.txt");
    $filecount=count($getfiles);
    if(preg_match("/\.txt$/i", $getfiles[$getdate]) == 1) #If $getfiles[$getdate] ends with .txt
    {
    include($getfiles[$getdate]);
    }
    ?>
     
    mistamagoo, Jul 20, 2007 IP
  3. loongar

    loongar Banned

    Messages:
    160
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hope you must have resolved
     
    loongar, Jul 20, 2007 IP
  4. Greg Carnegie

    Greg Carnegie Peon

    Messages:
    385
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I am not sure what you mean but if you have 60 different files isn't it best do do it this way:

    
    $getdate = date("s");
    $myFiles=array("file01.txt","file02.txt", ..., "file60.txt");
    include($myFiles[$getdate]);
    
    PHP:
    In your script you are using $getfiles as an array while it is integer.
     
    Greg Carnegie, Jul 20, 2007 IP
  5. mistamagoo

    mistamagoo Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    EDIT: Sorry double post. Browser made me think my post got deleted.
     
    mistamagoo, Jul 21, 2007 IP
  6. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks a lot magoo and everybody, that helped it on these:

    <?php
    ini_set('error_reporting', 8191);
    ini_set('display_startup_errors', 1);
    ini_set('display_errors', 1);
    $getdate = date("s");
    $getfiles=array("file01.txt","file02.txt");
    $filecount=count($getfiles);
    if(preg_match("/\.txt$/i", $getfiles[$getdate]) == 1) #If $getfiles[$getdate] ends with .txt
    {
    include($getfiles[$getdate]);
    }
    ?>
    PHP:
    And this:

    <?php
    ini_set('error_reporting', 8191);
    ini_set('display_startup_errors', 1);
    ini_set('display_errors', 1);
    $getdate = date("s");
    $getfiles=array("file01.txt","file02.txt");
    $filecount=count($getfiles);
    $analyze = explode(".", $getfiles[$getdate]);
    $result = array_pop($analyze);
    if ($result == "txt")
    {include($getfiles[$getdate]);
    }
    ?>
    PHP:
    All of them above have the exact same error left including gregs. Error says undefined index plus a number between 0 and 60. And the number changes to a new number on each refresh per second. So is it looking for a number to identify each item in the array? If so how would I accomodate that? The reason why I was doing the exploding and popping is because I will be including another file besides just a .txt file soon but if these other ways work, I'll use it. If I add these to any of them above
    
    $getfiles[1] = "file02.txt"; 
    $getfiles[2] = "file01.txt"; 
    PHP:
    instead of the other type of array, it isn't helping yet, how to do the right kind of array with my or some of these codes? Thank you.
     
    Jen-, Jul 21, 2007 IP
  7. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $filecount was on the wrong line, this is the updated one. Now it just says failed opening the include line, but doesn't say why. I tested the files on the 1-2 hour am.

    <?php
    ini_set('error_reporting', 8191);
    ini_set('display_startup_errors', 1);
    ini_set('display_errors', 1);
    $getdate = date("H");
    $getfiles=array("file01.txt","file02.txt");
    $filecount=count($getfiles);
    $analyze = explode(".", $filecount[$getdate]);
    $result = array_pop($analyze);
    if ($result == "txt")
    {include($filecount[$getdate]);}
    ?>
    PHP:
     
    Jen-, Jul 21, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    If this is a static array, and the file you are going to include is going to be one of these files in the array, and you have full control over the array... then why are you checking for the txt extension? Let's assume the files from 0 to 59 exist, then you can do simply:

    
    include 'file' . date('s') . '.txt';
    
    PHP:
    Or am I missing something?
     
    nico_swd, Jul 21, 2007 IP
  9. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yes I said above I am trying to add more than one type of file with it. So this is where I'm at to get different type files by minute, unless it can be done without the pops and explodes, just don't know how.

    This says it doesn't like this first Telse:
    elseif ($ext == "html") include($data[$currentFile]);
    PHP:
    <?php
    ini_set('error_reporting', 8191);
    ini_set('display_startup_errors', 1);
    ini_set('display_errors', 1); 
    $currentFile = date("i");
    $file1 = file_get_contents('file01.txt');
    $file2 = file_get_contents('file02.txt');
    $file3 = file_get_contents('file03.txt');
    $file4 = file_get_contents('file04.txt');
    $file5 = file_get_contents('file05.txt');
    $file6 = file_get_contents('file06.txt');
    $file7 = file_get_contents('file07.txt');
    $file8 = file_get_contents('file08.txt');
    $file9 = file_get_contents('file01.html');
    $file10 = file_get_contents('file10.txt');
    $allfiles = array($file1,$file2,$file3,$file4,$file5,$file6,$file7,$file8,$file9,$file10);
    $file = explode(",", $allfiles);
    #echo $file[0]; // $file1
    #echo $file[2]; // $file2
    #echo $file[3]; // $file3
    #echo $file[4]; // $file4
    #echo $file[5]; // $file5
    #echo $file[6]; // $file6
    #echo $file[7]; // $file7
    #echo $file[8]; // $file8
    #echo $file[9]; // $file9
    #echo $file[10]; // $file10
    foreach($file as $data):
    $ext = substr($data, -3);
    #$ext = array_pop($file);
    if ($ext == "txt") 
    include($data[$currentFile]);
    break;
    elseif ($ext == "html")
    include($data[$currentFile]);
    break;
    endforeach;
    $parts = explode(".", $files[$currentFile]);
    $ext = array_pop($parts);
    if ($ext == "txt")
    {include($files[$currentFile]);}
    elseif ($ext == "html")
    {include($files[$currentFile]);}
    ?>
    PHP:
     
    Jen-, Jul 21, 2007 IP