简体   繁体   中英

Move a array of pictures path/name in a folder with php

I have a folder called images (on server side), and there will be added pictures, constantly, I want a function that will move all the pictures from that folder (images) on another folder (called with the number of actual month) that is inside the images folder. So I will basically have a folder images with other folders each one having the month number, so lets say for today the folder 03 (March).

This is the function so far:

public function createCurrentMonthFolder() {
        $month = date("m");
        $directory = "./images/".$month;
        if(!is_dir($directory)){
            mkdir($directory, 775, true);
            }
        $source= "./images";
        $images = glob('./images/*.{jpg,gif,png}', GLOB_BRACE);
        $dest = "./images/".$month;
        var_dump($images);
        copy($source, $dest);
        unlink($source);
    }

So the first part will check if the folder of the current month is there already( if not create it). In the $images variable I will get the array of the image names like this :

array(2) { [0]=> string(19) "./images/14.jpg" [1]=> string(17) "./images/ji.jpg" }

And I want to move all that images in the $dest, that will actually be the current month folder name.

Problem is that copy() accepts only a file not a array that I send and also unlink() will want to get a file not a dir.

So how can I move the array of pictures into my desired folder and delete them after from the images folder? Thank you!

you can make a loop to access array,

foreach($images as $image){
 copy($image, $dest);
 unlink($image);
}

Probably this may help you:

$all_files = scandir('/home/user/Desktop/images');

    $source = '/home/user/Desktop/images/' ;
    $destination = '/home/user/Desktop/images/03/';

    foreach($all_files as $files){
        if(is_file($source.$files)){
            rename($source.$files,$destination.$files);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM