Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to change teh folder name in which i am uploading the images?

Posted on 2009-08-27
3
Medium Priority
?
944 Views
Last Modified: 2012-08-13
i am not uploading my images to the specific folder. i am uploading it in the folder named $title.
now i have 2 tables.
one is album with fields id,title,desc
another is photo_gallery with fields id,image,album_name

the title of one is same as the album_name of another. it is basically the album into which i am uploading the images. now the question is if i edit the first table means album title then the folder name should also change and in another table album_name should also change.
how can i do this?
0
Comment
Question by:designersx
  • 2
3 Comments
 

Author Comment

by:designersx
ID: 25203441
i had taken the hidden field and changed the name of album_name in another table.
now only task that is left is how i should change the folder name. i explain you with example.

For example, you have given the album name that is abc and uploaded the 5 images in that. ok, now you change the name of the album and now that is xyz and those images should be in xyz folder and not in abc folder.

i need an idea for this.
0
 

Author Comment

by:designersx
ID: 25203460
i want to change the folder name because i need to display the images also.
0
 
LVL 2

Accepted Solution

by:
nbruno02 earned 2000 total points
ID: 25203629
If I understand your problem correctly, it sounds like you're going to have to:

1. Create the new folder
2. Read all of the files from the old folder and copy it into the new folder
3. Delete the old folder

Something like the following functions should help you. The function you should call is rename_folder(). Make sure the arguments you pass to that function are directories, so, for your example, you would pass $old_folder as /home/user/public_html/albums/abc/ and $new_folder as /home/user/public_html/albums/xyz

// Taken from PHP.net/rmdir comments
function delete_directory( $dir) 
{
	$files = glob( $dir . '*', GLOB_MARK );
	foreach( $files as $file )
	{
		if( substr( $file, -1 ) == '/' )
			delete_directory( $file );
		else
			unlink( $file );
	}
    rmdir( $dir);
}
 
// Taken from PHP.net/copy comments
function smartCopy($source, $dest, $options=array('folderPermission'=>0755,'filePermission'=>0755))
    {
        $result=false;
       
        if (is_file($source)) {
            if ($dest[strlen($dest)-1]=='/') {
                if (!file_exists($dest)) {
                    cmfcDirectory::makeAll($dest,$options['folderPermission'],true);
                }
                $__dest=$dest."/".basename($source);
            } else {
                $__dest=$dest;
            }
            $result=copy($source, $__dest);
            chmod($__dest,$options['filePermission']);
           
        } elseif(is_dir($source)) {
            if ($dest[strlen($dest)-1]=='/') {
                if ($source[strlen($source)-1]=='/') {
                    //Copy only contents
                } else {
                    //Change parent itself and its contents
                    $dest=$dest.basename($source);
                    @mkdir($dest);
                    chmod($dest,$options['filePermission']);
                }
            } else {
                if ($source[strlen($source)-1]=='/') {
                    //Copy parent directory with new name and all its content
                    @mkdir($dest,$options['folderPermission']);
                    chmod($dest,$options['filePermission']);
                } else {
                    //Copy parent directory with new name and all its content
                    @mkdir($dest,$options['folderPermission']);
                    chmod($dest,$options['filePermission']);
                }
            }
 
            $dirHandle=opendir($source);
            while($file=readdir($dirHandle))
            {
                if($file!="." && $file!="..")
                {
                     if(!is_dir($source."/".$file)) {
                        $__dest=$dest."/".$file;
                    } else {
                        $__dest=$dest."/".$file;
                    }
                    //echo "$source/$file ||| $__dest<br />";
                    $result=smartCopy($source."/".$file, $__dest, $options);
                }
            }
            closedir($dirHandle);
           
        } else {
            $result=false;
        }
        return $result;
    }
 
// Make sure you specify these as directories
function rename_folder( $old_folder, $new_folder)
{
	// Create the new folder if it doesn't already exist
	if( !is_dir( $new_folder))
	{
		@ mkdir( $new_folder);
	}
 
	// Copy the contents from the old folder to the new folder
	smartCopy( $old_folder, $new_folder);
 
	// Delete the old folder
	rmdir( $old_folder);
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question