want to convert following code into function
Posted: Sat Jun 21, 2008 7:20 am
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 .....
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> </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>