Page 1 of 1

Help with rename if file exists

Posted: Tue Feb 05, 2008 6:39 pm
by nofuse
Hi

I have a code im using but I need to change it to rename the file is it already exists instead of overwriting it, don't care what it is renamed to but must have the same extension i.e filename.mst or make up a random name ending in the extension .mst.

I have copied the existing full script below:

***** PLEASE USE THE

Code: Select all

TAG WHEN POSTING *****[/color]

Code: Select all

<?php
 
// gettimers.php
 
 
$key = 'xxxxxxxxx' ;
$filename = 'timer.mst' ;
$destination = 'ProgramFiles\Settings\MyStuff\CTs' ;
 
if ( ! file_exists($filename) ) {
 
 
    $url = sprintf( 'http://www.toppy.org.uk/timers/Sfetch.php/%s/%s', $key, $filename ) ;
 
    $timerinfo = @file_get_contents($url);
 
    
    if ( $timerinfo == '' ) {
        print "No timers waiting, or download error\n" ;
    } else if ( preg_match( '/^ERROR:/', $timerinfo )) {
        print $timerinfo ;
    } else {
    
    
    
        $timerfile = fopen( $filename, 'w' ) or die("Can't save timers to disk\n") ;
 
        fwrite($timerfile, $timerinfo) ;
        fclose($timerfile) ;
    }
 
}
 
 
if ( file_exists($filename)) {
    $tfcommand = sprintf("tfcopy %s \"#:\%s\%s\"", $filename, $destination, $filename) ;
    exec($tfcommand, $result) ;
    print "Command is $tfcommand\n";
 
    $res = implode(' ', $result) ; 
    
   if ( preg_match('/Copy done/', $res ))  {
        unlink($filename) ;
   }
}

Re: Help with rename if file exists

Posted: Tue Feb 05, 2008 8:20 pm
by Ambush Commander
The logic would be to check if the file exists, if it does, rename the existing one to a new file, and then perform the rename. Can we see at least an attempt?