want to convert following code into function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vb_123
Forum Newbie
Posts: 19
Joined: Wed May 21, 2008 2:16 am

want to convert following code into function

Post by vb_123 »

For future reference, please include PHP code within BBcode tags, as I have done for you below.
hi all,
i want to convert the following "file rename" code into function(rewrite following code using a function).i am not too good in using a function .....

Code: Select all

 
<?php
 
$oldFileName=$_POST['oldFileName'];
$newFileName=$_POST['newFileName'];
//function rname($dirpath,$oldFileName,$newFilename)
//{
 
//  }
 
    if(isset($_POST['sub']))
    {
//      print_r($file);
    $dir='upload/';
    $dh = opendir($dir) or die ("Unable to open the directory");
    if (is_dir($dir)) 
    {
       if ($dh = opendir($dir))
           {
             while (($file = readdir($dh)) !== false)
                {
                    if($file == "." || $file == "..")
                    {
                    continue;
                    }
                    if($oldFileName==$file)
                    {
//                      $file=$newFileName;
                        rename($dir.$oldFileName,$dir.$newFileName);
                        echo "file has been renamed " .$oldFilename.' TO '.$newFileName;
                    }
                    echo "<br>";
                    unset($_POST);
                }
           }
    
    }
    }
//} 
 
?>  
<form  name="frm" method="post" action="rname.php">
    <table width="400"  align="center" cellpadding="0" cellspacing="0" border="0">
          <tr>
            <td>Enter file name to be renamed : </td>
            <td><input type="text" name="oldFileName" size="30"></td>
        </tr>
            
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
         <tr>
            <td>Enter new file name: </td>
            <td><input type="text" name="newFileName" size="30"></td>
        </tr>
 
 
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
 
        <tr>
            <td></td>
            <td><input type="submit" name="sub" value="Submit"></td>
            
         </tr>
       
    </table>
</form>
 
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: want to convert following code into function

Post by tecktalkcm0391 »

It is really easy to do with some help. I'm not going to right it for you, but I will give you some help.

Start with this:

Code: Select all

function rname($dirpath, $oldFileName, $newFilename){
// just copy your code in here replacing any variable or strings with the appropriate var from above... so 
    $dir='upload/';
// would be come
   $dir=$dirpath;
 
} 
 
To run the function just do

Code: Select all

rname('directory here or variable with info', 'old filename here', 'new filename here');
 
Post Reply