php copy files

Discussion in 'PHP' started by stats, May 29, 2007.

  1. #1
    how can i copy folder1/*.* to folder2/ ?

    supposing i don't know the exact file names in folder1 ..

    i just want my script to copy over all the files that it can find in folder1 to folder2

    Thanks
     
    stats, May 29, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $source = './path/to/folder1/*.*';
    
    function copy_files($file, $destination = './path/to/folder2/')
    {
        copy($file, $destination . basename($file));
    }
    
    array_map('copy_files', glob($source));
    
    PHP:
    Untested but should work.
     
    nico_swd, May 29, 2007 IP
    coderlinks likes this.
  3. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #3
    nice solution Nico
     
    gibex, May 29, 2007 IP
  4. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Now thats what I call great coding. In fact, I didnt even know a function called glob existed . :)
     
    coderlinks, May 29, 2007 IP
  5. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Good find!
     
    lemaitre, May 29, 2007 IP
  6. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Thank you :)
     
    stats, May 29, 2007 IP
  7. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #7
    I'm having a bit trouble with syntax here .. could you please show the correct syntax to use ?

    here's my final code

    if (!is_dir("$login")) { mkdir ("$login"); }
    $source = "templates/$template_ID/images/*.*";
    function copy_files($file, $destination = '$login/images/') {
        copy($file, $destination . basename($file));
    }
    array_map('copy_files', glob($source));
    PHP:
    as you see, i use $login variable (which is already defined earlier) in the name of destination folder. Also i use $template_ID in source but it works fine.

    and here is what i get as a result:
    so, it doesn't change the "$login" to it's value , but instead uses "$login" as a string itself
     
    stats, May 29, 2007 IP
  8. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Do it like this, variables are not interpolated inside single quotes:

    if (!is_dir("$login")) { mkdir ("$login"); }
    $source = "templates/$template_ID/images/*.*";
    function copy_files($file, $destination = "$login/images/") {
        copy($file, $destination . basename($file));
    }
    array_map('copy_files', glob($source));
    PHP:
    PHP offers several types of quoting. Double quotes offer the most features, including variable interpolation and escape sequences like "\n" for a newline. With single quotes you get exactly what you put within the quotes, no special features.
     
    lemaitre, May 29, 2007 IP
  9. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #9
    I have tried that as well as some other combinations ..

    in your exact case, here's what i get

     
    stats, May 29, 2007 IP
  10. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #10
    I think i got the reason why this happens ..

    the $login variable is defined AFTER the function is defined by the compiler, so at the time when the function being created - the variable $login does not exist yet ..

    ok, then i guess i need another function instead of this, which will accept $login as an argument, and when the time comes to call the function, the $login will already be defined

    so, can anyone please help me to create such a function ?

    Thanks
     
    stats, May 29, 2007 IP
  11. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Try this:

    if (!is_dir($login)) { mkdir ($login); }
    $source = "templates/$template_ID/images/*.*";
    $destination = "$login/images/";
    function copy_files($file) {
        global $destination;
        copy($file, $destination . basename($file));
    }
    array_map('copy_files', glob($source));
    PHP:
     
    lemaitre, May 29, 2007 IP
  12. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #12
    Thanks :)

    i actually have figured out to go another way like this

    $cmd = "cp -rf /path1/images path2";
    system($cmd);

    it did all the job :)))
     
    stats, May 29, 2007 IP
  13. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #13
    That's good but it won't work on Windows. Using glob is cross-platform, hence the reason I thought it was a good find.
     
    lemaitre, May 30, 2007 IP