rename 


php128 apg

RENAMES a particular file or directory if exists.





This function returns TRUE on success or FALSE on failure.



<?php

bool rename 
str $oldname str $newname res $context = ? )

where,

$oldname The original name

$newname 
The new name

$context 
A valid context resource criated by 
                 stream_context_create
                 
?>
 

 $oldname 


The original name.



 $newname 


The new name.

Attempts to rename $oldname to $newname moving it between directories if necessary.

If $newname is a file and already exists, it will be overwritten.

If renaming a directory and $newname exists, this function will emit a Warning.



  $context   


A valid context resource criated by stream_context_create.



  1 EXERCISE   

<?php

define
("PATH2TPN"__DIR__ '/temp/');

define('PATH2TPW''file://' __DIR__ '/temp/');

$opts01 = [ 
  
'https'=>[ 
    
'method'=>"GET"
    
'header'=>"Accept-language: en<br>\r\n" 
              
"Cookie: foo=bar<br>\r\n" 
] ]; 

$context01 stream_context_create($opts01); 

$srcpath01 PATH2TPN;

$dstpath01 PATH2TPW;

$srcfile01 'love.txt';

$dstfile01 'love.copy.txt';

if (
file_exists($srcpath01 $srcfile01))
{
if(
rename($srcpath01 $srcfile01$dstpath01 
                                    
$dstfile01$context01))
{
echo 
'The "' $srcfile01 '" file WAS copied and renamed as "' 
                   
$dstfile01 '"<br>';
}
else
{
echo 
'The "' $srcfile01 
                   
'" file WAS NOT copied or renamed as "' 
                    
$dstfile01 '"<br>';
}
}
else
{
echo 
'The "' $srcfile01 '" file WAS NOT found.';
}

?>  

 RESULT   

Applied to the same directory, the original file love.txt is deleted, however, a new copy of it is copied and saved as love.copy.txt.


  2 EXERCISE   

<?php


define
("PATH2TPN"__DIR__ '/temp/');

define('PATH2TPW''file://' __DIR__ '/temp/'); 

$opts02 = [ 
  
'https'=>[ 
    
'method'=>"GET"
    
'header'=>"Accept-language: en<br>\r\n" 
              
"Cookie: foo=bar<br>\r\n" 
] ]; 

$context02 stream_context_create($opts02); 

$srcpath02 PATH2TPN;

$dstpath02 PATH2TPW 't2/';

$srcfile02 'sw7.copy.jpg';

$dstfile02 'sw7.jpg';

if (
file_exists($srcpath02 $srcfile02))
{
if(
rename($srcpath02 $srcfile02$dstpath02 
                                    
$dstfile02$context02))
{
echo 
'The "' $srcfile02 '" file WAS copied and renamed as "' 
                   
$dstfile02 '"<br>';
}
else
{
echo 
'The "' $srcfile02 
                   
'" file WAS NOT copied or renamed as "' 
                    
$dstfile02 '"<br>';
}
}
else
{
echo 
'The "' $srcfile02 '" file WAS NOT found.';
}

?>  

 RESULT   

The original file sw7.copy.jpg is deleted in the source directory and copied into the destination directory as sw7.jpg.


  3 EXERCISE   

<?php

define
("PATH2TPN"__DIR__ '/TEST/');

define('PATH2TPW''file://' __DIR__ '/TEST/');

$opts03 = [ 
  
'https'=>[ 
    
'method'=>"GET"
    
'header'=>"Accept-language: en<br>\r\n" 
              
"Cookie: foo=bar<br>\r\n" 
] ]; 

$context03 stream_context_create($opts03); 

$srcpath03 PATH2TPN;

$dstpath03 PATH2TPW;

$srcfile03 'temp';

$dstfile03 'tempCopy';

if (
file_exists($srcpath03 $srcfile03))
{
if(
rename($srcpath03 $srcfile03$dstpath03 
                                    
$dstfile03$context03))
{
echo 
'The "' $srcfile03 '" file WAS copied and renamed as "' 
                   
$dstfile03 '"<br>';
}
else
{
echo 
'The "' $srcfile03 
                   
'" file WAS NOT copied or renamed as "' 
                    
$dstfile03 '"<br>';
}
}
else
{
echo 
'The "' $srcfile03 '" file WAS NOT found.';
}

?>