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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.