richard_PHP Posted April 26, 2009 Share Posted April 26, 2009 hello all back again with another problem with my dissertation. im wanting to rename a file, but its in the directory and not a database. here it is live: http://scape-pictures.110mb.com/loggedphotos.htm so when the filename in the textbox is altered, so far it says file was renamed. but it hasnt been. code: loggedphotos.php <form action="rename.php" method="post" name="list"> <?php // define directory path $dir = "uploads"; // iterate through files // look for JPEGs if (is_dir($dir)) { if ($dh = @opendir($dir) or die ("Directory failed to open.")) { while (($file = readdir($dh)) !== false) { if($file!="." && $file!=".." && $file!="thumbs.db"){ if (preg_match("/.jpg/", $file)) { // get thumbnail // link to full image echo "<p><img src='".$dir."/".$file."' class='thumb' border='2' /> <input name='files' type='text' width='300' value=".$file." /></p>"; } } } closedir($dh); } } echo "<p><input name='submit' type='submit' value='Rename File' /></p>"; ?> </form> rename.php <?php $new_name = $_POST['files']; $path = "uploads/"; if (file_exists($path . $new_name)){ move_uploaded_file($path . $new_name); echo "File was successfully renamed."; } else { echo "Sorry, the file did not delete properly. Please try again."; } ?> Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted April 26, 2009 Share Posted April 26, 2009 If you only want to rename a file, why not use php's rename function ? http://nl2.php.net/manual/en/function.rename.php Quote Link to comment Share on other sites More sharing options...
richard_PHP Posted April 26, 2009 Author Share Posted April 26, 2009 didnt think there was one. lol oops!. added it and tweaked a bit but it comes up with a blank page (with the styling but no messages displayed). <?php $new_name = $_POST['files']; $path = "uploads/"; if (is_dir($dir)) { if ($dh = @opendir($dir) or die ("Directory failed to open.")) { while (($file = readdir($dh)) !== false) { if($file!="." && $file!=".." && $file!="thumbs.db"){ if (file_exists($path . $new_name)){ rename($file, $new_name); echo "File was successfully renamed."; } else { echo "Sorry, the file did not delete properly. Please try again."; } } } } } ?> Quote Link to comment Share on other sites More sharing options...
richard_PHP Posted April 26, 2009 Author Share Posted April 26, 2009 realised that a variable wasnt right. heres the ammended, still broke, code: <?php $path = "uploads/"; $new_name = $path, $_POST['files']; if (is_dir($path)) { if ($dh = @opendir($path) or die ("Directory failed to open.")) { while (($file = readdir($dh)) !== false) { if($file!="." && $file!=".." && $file!="thumbs.db"){ if (file_exists($path . $new_name)){ rename($file, $new_name); echo "File was successfully renamed."; } else { echo "Sorry, the file was not renamed due to an error. Please <a href='loggedphotos.php'>try again</a>."; } } } } } ?> Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted April 26, 2009 Share Posted April 26, 2009 I think this line is wrong: $new_name = $path, $_POST['files']; because later on you do: if (file_exists($path . $new_name)){ wouldn't that be: if (file_exists("uploads/uploads/file")){ Quote Link to comment Share on other sites More sharing options...
richard_PHP Posted April 30, 2009 Author Share Posted April 30, 2009 change of brief now means that im using a database system, however thanks for the help. its here when i next need it. much appreciated! Quote Link to comment 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.