File Manipulation Code

  Home arrow File Manipulation Code arrow Move or Copy a Directory (and files an...
FILE MANIPULATION CODE

Move or Copy a Directory (and files and sub dirs)
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2005-06-16

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    The code contains two functions. You need to run the MoveDirectory function pass it the source and destination directories. If copy is set to false the origional dir is removed. If it is set to true the origional dir is not deleted.

    By : metcarob

    function MoveDirectory($source,$dest,$copy=false) {
    //remove trailing slashes
    if (substr($dest,-1) == "\\") $dest = substr($dest,0,strlen($dest)-1);
    if (substr($source,-1) == "\\") $source = substr($source,0,strlen($source)-1);
    if (!file_exists($source)) return FALSE;

    Ensure_Sub_Directory_Exists($dest);

    //now copy all the files & direcrtories to the new location and deltet them
    $d = opendir($source);
    while ($f = readdir($d)) {
    $l = $source . "\\" . $f;
    if (is_dir($l)) {
    if (!($f['value']=="." || $f=="..")) {
    MoveDirectory($l,$dest . "\\" . $f,$copy);
    };
    } else {
    copy( $l ,$dest . "\\" . $f);
    if (!$copy) {
    //we are moving the directory so we should delete the file
    unlink($l);
    };
    };
    };
    closedir($d);
    if (!$copy) {
    rmdir($source);
    };
    return TRUE;
    };
    function Ensure_Sub_Directory_Exists($dir) {
    //When passed a directory create it
    if (substr($dir,-1) == "\\") $dir = substr($dir,0,strlen($dir)-1);

    if (file_exists($dir)) return TRUE;

    $a = explode("\\",$dir);
    while ($t = each($a)) {
    $b = $t['value'];
    };
    $b = str_replace("\\" . $b,"",$dir);

    //does $b exist?
    if (!file_exists($b)) {
    Ensure_Sub_Directory_Exists($b);
    };
    //PArent exists so make this directork exist
    mkdir($dir);
    return TRUE;
    };
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More File Manipulation Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    FILE MANIPULATION CODE ARTICLES

    - Bandwidth Control with pure PHP
    - Eazy Gallery
    - file_get_contents for PHP < 4.3.0
    - PHP Class: Image Snapshot 1.3
    - Universal downloader
    - Image Gallery v2.0
    - Free/Used Disk Space
    - Directory Lister
    - Directory image view, with selective hidden
    - Move or Copy a Directory (and files and sub ...
    - Ensure_Sub_Directory_Exists
    - Wedit
    - Form Examples Text Boxes to Drop Downs
    - myFiles
    - List files in a directory, no subdirectories

    Developer Shed Affiliates

     



    © 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap