Jump to content

want to convert following code into function


tested_123

Recommended Posts

hi all,

i want to rewrite following "file rename" code using a function.i am not good in using a function....please help me in converting this code into function

 

<?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".$oldFileName." has been renamed  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> </td>
            <td> </td>
        </tr>
         <tr>
            <td>Enter new file name: </td>
            <td><input type="text" name="newFileName" size="30"></td>
        </tr>


        <tr>
            <td> </td>
            <td> </td>
        </tr>

        <tr>
            <td></td>
            <td><input type="submit" name="sub" value="Submit"></td>
            
         </tr>
       
    </table>
</form>	

 

(edited by kenrbnsn to add


tags)

people dont like to do code for you.

 

function rname($dirpath,$oldFileName,$newFilename)

that is a step in the right direction, its just a case of renaming variables to work with the functions input. if you have a try but have errors or cannot seem to get it working correctly then post again and people will take a look

thanx i have converted the code into function ............here it goes..............

 

<?php

 

$oldFileName=$_POST['oldFileName'];

$newFileName=$_POST['newFileName'];

function rname($dir,$oldFileName,$newFileName)

{

$r= rename($dir.$oldFileName,$dir.$newFileName);

return $r;

}

 

if(isset($_POST['sub']))

{

$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)

{

$found="true";

$var=rname('upload/',$oldFileName,$newFileName);

echo "file  ".$oldFileName." has been renamed  to  " .$newFileName."<br.";

}

else

{

$found="false";

}

 

}

 

  }

 

}

 

  if($found=="false")

{

echo "entered file does not exist in specified folder";

}

}

 

?>

<form  name="frm" method="post" action="rename_1.php">

    <table width="350"  align="center" cellpadding="0" cellspacing="0" border="0">

          <tr>

<td>Enter file name to be renamed : </td>

            <td><input type="text" name="oldFileName" size="20"></td>

</tr>

 

<tr>

<td> </td>

<td> </td>

</tr>

<tr>

<td>Enter new file name: </td>

            <td><input type="text" name="newFileName" size="20"></td>

</tr>

 

 

<tr>

<td> </td>

<td> </td>

</tr>

 

<tr>

<td></td>

<td><input type="submit" name="sub" value="Submit"></td>

 

        </tr>

     

</table>

</form>

 

hello all,

            i have a one more problem regarding file renaming.Now wat i have to do is that there are subfolders(may be 1 or 2 or 3...) inside a folder and that subfolders contain several files.Now wat i want is that if i enter old filename in a textbox(that file can be present in any of the subfolder but we don't know in which subfolder it is present) and in another text box if i give new filename the "file rename" program must searches for that file placed in any of the subfolders and then rename it.........itz a typical program i think......can anybody help me doing this............

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.