Jump to content

rename directory file


richard_PHP

Recommended Posts

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.";
		}
	?>

Link to comment
https://forums.phpfreaks.com/topic/155702-rename-directory-file/
Share on other sites

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.";
						}
					}
				}
			}
		}
	?>

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>.";
						}
					}
				}
			}
		}
	?>

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.