tags:

views:

48

answers:

3

hey guys, i've no idea how to do that and need your help!

i have an array of filenames called $bundle. (file_one.jpg, file_two.pdf, file_three.etc) and i have the name of the folder stored in $folder. (my_directory)

i now would like to move all the files stored in $bundle to move to the directory $folder.

how can i do that?

    //print count($bundle); //(file_one.jpg, file_two.pdf, file_three.jpg)
    $folder = $folder = PATH . '/' . my_directory;
 foreach ($bundle as $value) {

  //rename(PATH.'/'.$value, $folder . '/' . $value);

 }

just so it's not confusing: PATH just stores the local file-path im using for my project. in my case it's just the folder i'm working in-so it's "files".

i have no idea which method i have to use for this and how i could solve that!

thank you for your help!

+1  A: 

The code given by you should work with minor changes:

$folder = PATH . '/' . 'my_directory'; // enclose my_directory in quotes.
foreach ($bundle as $value) {
        $old = PATH.'/'.$value, $folder;
        $new = $folder . '/' . $value;
        if(rename($old,$new) !== false) {
                // renamed $old to $new
        }else{
                // rename failed.
        }
}
codaddict
thank you works fine, except is there an easy way to do the same with directories?imagine the $bundle Array contains not only files but directories as well. (file_one.jpg, directory1, file_two.pdf, another-directory)i'm checking for a folder with $ext = pathinfo($value, PATHINFO_EXTENSION); if ($ext == '') { its a folder!!!i just need a function or a method which let's me move complete directories with all it's contents!?
@mathi just because a file doesnt have an extension doesnt mean it's a folder. Use `is_dir` to check if a directory is a directory.
Gordon
A: 
$folder = PATH . '/' . $folder;
foreach ($bundle as $value) {
        $old = PATH.'/'.$value;
        $new = $folder . '/' . $value;
        if(rename($old,$new) !== false) {
                // renamed $old to $new
        }else{
                // rename failed.
        }
}
Maulik Vora
A: 

Untested but should work:

function bulkMove($src, $dest) {
    foreach(new GlobIterator($src) as $fileObject) {
        if($fileObject->isFile()) {
            rename(
                $fileObject->getPathname(),
                rtrim($dest, '\\/') . DIRECTORY_SEPARATOR . $fileObject->getBasename()
            );
        }
    }
}
bulkMove('/path/to/folder/*', '/path/to/new/folder');

Could add some checks to see if the destination folder is writable. If you dont need wildcard matching, change the GlobIterator to DirectoryIterator. That would also eliminate the need for PHP5.3

Gordon