Trouble with rename command

Discussion in 'PHP' started by keeganray, Mar 5, 2010.

  1. #1
    Hi,

    I'm pretty new to PHP and I am trying to rename a file using PHP code. My code looks like this:

    rename("/var/www/html/$old.html","/var/www/html/$random.html");
    PHP:
    Where $old contains the name of the file and $random contains a string of numbers.

    When I run the code it gives me this error in my web browser:

    Warning: rename(/var/www/html/Array.html,/var/www/html/556055023.html) [function.rename]: No such file or directory in /var/www/html/echo.php on line 283

    How would I fix this?

    Thanks,

    Keegan
     
    keeganray, Mar 5, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    There is no Array.html file. Maybe the $old is an array and the filenames is the elements of that array. If you cannot solve that problem, write here and I will try to help you.
     
    s_ruben, Mar 5, 2010 IP
  3. keeganray

    keeganray Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have been trying to have the code print out the actual file name which is stored into $old, but everything I've tried gives me that same result, where it prints out Array instead of the file name. The variable $Random works, but some how $old doesn't. I am getting $old with the code below:

    $old=file("/var/www/html/Rand_No.txt");  // records old html name
    
    fwrite(fopen("/var/www/html/Rand_No.txt", "w+"),$random);   // records new html name over old html name
    fclose(fopen("/var/www/html/Rand_No.txt", "w+"));
    
    rename("/var/www/html/$old.html","/var/www/html/$random.html");
    
    
    PHP:
    Thanks for your help,

    Keegan
     
    keeganray, Mar 6, 2010 IP
  4. sunlcik

    sunlcik Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You may try this code pls:
    
    //records old html name.  ->   return array
    $old	= file("/var/www/html/Rand_No.txt");
    //you'd better print_r($old);tell me what this result is?
    foreach($old as $key=>$value)
    {
    	//$random  where is $random from??
    	//add $random at this line pls.be sure the value of random are different!!
    	if(rename("/var/www/html/$value.html","/var/www/html/$random.html"))
    	{
    		//true :  be sure php script can creat a new text file for recording the true result
    		$fp	= fopen('/var/www/html/rename_true.txt','ab+');
    		fwrite($fp,$random);
    		fclose($fp);
    	}else{
    		//false :  be sure php script can creat a new text file for recording the false result
    		$fp	= fopen('/var/www/html/rename_false.txt','ab+');
    		fwrite($fp,$random);
    		fclose($fp)
    	}
    }
    
    PHP:
    -------
    Wu
     
    sunlcik, Mar 6, 2010 IP
  5. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #5
    Well are the paths correct ??
     
    Bohra, Mar 6, 2010 IP
  6. keeganray

    keeganray Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That worked!

    I modified it a little, but that fixed my array problem. Thanks sunlcik!

    -Keegan
     
    keeganray, Mar 8, 2010 IP