0

Is there a way to copy and rename multiple files in php but get their names from an array or a list of variables.

The nearest thing to what I need that I was able to find is this page Copy & rename a file to the same directory without deleting the original file

but the only thing the script on this page does is creating a second file and it's name is already preset in the script.

I need to be able to copy and create multiple files, like 100-200 and get their names set from an array.

If I have an initial file called "service.jpg" I would need the file to be copied multiple times with the different names from the array as such :

$imgnames = array('London', 'New-York','Seattle',); etc.

Getting a final result of 3 separate files called "service-London.jpg", "service-New-York.jpg" and so on.

I'm sure that it should be a pretty simple script, but my knowledge of PHP is really insignificant at the time.

Community
  • 1
  • 1
  • One approach: iterate over your `$imageNames` array and append this to the `$newFile` variable using PHP's `copy()` function. The second variable in the [`copy()`](http://php.net/manual/en/function.copy.php) function is the new name of the file, so it _can_ be renamed. – camelCase Dec 09 '15 at 18:11

2 Answers2

0

You could use a regular expression to build the new filenames, like this:

$fromFolder = 'Images/folder/';
$fromFile = 'service.jpg';
$toFolder = 'Images/folder/';
$imgnames = array('London', 'New-York','Seattle');

foreach ($imgnames as $imgname) {
    $newFile = preg_replace("/(\.[^\.]+)$/", "-" . $imgname . "$1", $fromFile);
    echo "Copying $fromFile to $newFile";
    copy($fromFolder . $fromFile, $toFolder . $newFile);
}

The above will output the following while copying the files:

Copying service.jpg to service-London.jpg
Copying service.jpg to service-New-York.jpg
Copying service.jpg to service-Seattle.jpg

In the above code, set the $fromFolder and $toFolder to your folders, they can be the same folder, if so needed.

trincot
  • 317,000
  • 35
  • 244
  • 286
0

One approach (untested) that you can take is creating a class to duplicate a directory. You mentioned you would need to get the name of the files in a directory and this approach will handle it for you.

It will iterate over an array of names (whatever you pass to it), and copy/rename all of the files inside a directory of your choice. You might want to add some checks in the copy() method (file_exists, etc) but this will definitely get you going and is flexible.

// Instantiate, passing the array of names and the directory you want copied
$c = new CopyDirectory(['London', 'New-York', 'Seattle'], 'location/of/your/directory/');

// Call copy() to copy the directory
$c->copy();

/**
 * CopyDirectory will iterate over all the files in a given directory
 * copy them, and rename the file by appending a given name
 */
class CopyDirectory
{
    private $imageNames; // array
    private $directory; // string

    /**
     * Constructor sets the imageNames and the directory to duplicate
     * @param array
     * @param string
     */
    public function __construct($imageNames, $directory)
    {
        $this->imageNames = $imageNames;
        $this->directory = $directory;
    }

    /**
     * Method to copy all files within a directory
     */
    public function copy()
    {   
        // Iterate over your imageNames
        foreach ($this->imageNames as $name) {
            // Locate all the files in a directory (array_slice is removing the trailing ..)
            foreach (array_slice(scandir($this->directory),2) as $file) {
                // Generates array of path information
                $pathInfo = pathinfo($this->directory . $file);

                // Copy the file, renaming with $name appended
                copy($this->directory . $file, $this->directory . $pathInfo['filename'] . '-' . $name .'.'. $pathInfo['extension']);
            }
        }       
    }
}
camelCase
  • 5,460
  • 3
  • 34
  • 37