codemania
codemania

Reputation: 1094

copy all file source to destination then remove all files from source in php

just i'm trying to copy all file source to destination and then remove all file from source in php.

here is my code:

function rcopy($src, $dst) {
    if (is_dir($src)) {
        $files = scandir($src);

        foreach ($files as $file) {
            if ($file != "." && $file != "..")
                rcopy("$src/$file", "$dst/$file");
        }

        array_map('unlink', glob($src."/*"));
    } else if (file_exists($src)) {
        copy($src, $dst);
    }
}

copy working well but file not deleted from source. please help

Upvotes: 1

Views: 732

Answers (2)

Nanhe Kumar
Nanhe Kumar

Reputation: 16307

$files = scandir($src);
    foreach ($files as $file) {
        if ($file != "." && $file != "..") {
            rcopy("$src/$file", "$dst/$file");
            unlink("$src/$file");
        }
    }

Upvotes: 1

Stafox
Stafox

Reputation: 1005

To delete directory use rmdir, unlink using to delete files. Note: The directory must be empty, and the relevant permissions must permit this.

Upvotes: 1

Related Questions