I have an array filled with paths looking like this:

library/main/single/list.php
library/article/grid/thumbs.php
library/footer/tiny.php

These files and folders exists on my http://localhost/test/

I also have path that is http://localhost/new/

What I want to do

What I need to do is move the files while keeping the current file structure (with directories intact) to the new location.

The result should be like this

Is there an easy way to do it or do I have to cut every string by its slash?

有帮助吗?

解决方案

You could try:

$from = './test/'; //replace with absolute path if better
$to = './new/';
$paths = array('library/main/single/list.php', 'library/article/grid/thumbs.php', 'library/footer/tiny.php');
$dirs = array();
foreach( $paths as $path ) {
    $pathinfo = pathinfo($to.$path);
    if (!in_array($pathinfo['dirname'], $dirs) && !file_exists($pathinfo['dirname']) && mkdir($pathinfo['dirname'], 0777, true))
        $dirs[] = $pathinfo['dirname'];
    copy($from.$path, $to.$path);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top