tested_123 Posted June 21, 2008 Share Posted June 21, 2008 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) Quote Link to comment https://forums.phpfreaks.com/topic/111237-want-to-convert-following-code-into-function/ Share on other sites More sharing options...
hitman6003 Posted June 21, 2008 Share Posted June 21, 2008 why are you wanting to put it into a function? Quote Link to comment https://forums.phpfreaks.com/topic/111237-want-to-convert-following-code-into-function/#findComment-570950 Share on other sites More sharing options...
Mattyspatty Posted June 21, 2008 Share Posted June 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/111237-want-to-convert-following-code-into-function/#findComment-570954 Share on other sites More sharing options...
tested_123 Posted June 23, 2008 Author Share Posted June 23, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/111237-want-to-convert-following-code-into-function/#findComment-572051 Share on other sites More sharing options...
tested_123 Posted June 23, 2008 Author Share Posted June 23, 2008 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............ Quote Link to comment https://forums.phpfreaks.com/topic/111237-want-to-convert-following-code-into-function/#findComment-572077 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.