SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jun 2001
    Location
    au
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how do i rename a file automatically?

    i am wanting to make the file "current_content.txt" rename itself "previous_day.txt" before it deletes everything inside it and picks a new set of links. if anyone could help me out that would be much appreciated.

    PHP Code:

    if($day_number $file[0] || $test_mode) {

    $file fopen("current_content.txt""w+");
    flock($file2);
    fputs ($file"");
    flock($file3);
    fclose($file);

    // AND PICK A DIFFERENT SET OF LINKS
    include("pick_content.php");

    if you need to see more of the code just ask

  2. #2
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried rename() ?
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  3. #3
    SitePoint Member
    Join Date
    Jun 2001
    Location
    au
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by anode
    Have you tried rename() ?
    im a newb at php

    would this be right?

    PHP Code:
    if($day_number $file[0] || $test_mode) {

    if (
    rename("current_content.txt""previous_content.txt"))
    {
    $file fopen("current_content.txt""w+");
    flock($file2);
    fputs ($file"");
    flock($file3);
    fclose($file);

    // AND PICK A DIFFERENT SET OF LINKS
    include("pick_content.php");
    }


  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, that looks mostly right. i wouldn't put the rename() in an if () though i don't think:

    PHP Code:
    if ($day_number $file[0] || $test_mode) {

        
    rename('./current_content.txt''./previous_content.txt');

        
    $file fopen('./current_content.txt''w+');
        
    flock($fileLOCK_EX);
        
    fputs ($file'');
        
    flock($fileLOCK_UN);
        
    fclose($file);

        
    // AND PICK A DIFFERENT SET OF LINKS
        
    include("pick_content.php");

    i'd use the LOCK_EX etc. constants instead of their numeric version unless you need this to work with versions of PHP before 4.0.1.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  5. #5
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    surely if you've renamed current to previous and then try fopen on current you'll get a file not found

    now - if you used copy(current, previous); two copies of the file would exist and the script might work. using an if constraint there would be useful imo.

    if (copy("current_content.txt", "previous_content.txt" ))
    {
    // two files now exist, so safe to amend current as we have previous as a backup.....
    teckis - that's news to me.

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pootergeist
    surely if you've renamed current to previous and then try fopen on current you'll get a file not found
    the 'w+' option to fopen() will create the file if it doesn't exist, remember.

  7. #7
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    d'oh - umm yeah I have to blame < 9am for that one
    teckis - that's news to me.


Bookmarks

Posting Permissions

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