Jens Törnell
Jens Törnell

Reputation: 24788

PHP - Make dirs and copy files from a URL

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?

Upvotes: 0

Views: 230

Answers (1)

AndersTornkvist
AndersTornkvist

Reputation: 2629

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);
}

Upvotes: 1

Related Questions