-2

I have following Array with filenames. Array is saved under var $files.

array ( 
 [0] => DSC02425.jpg 
 [1] => DSC02426.jpg 
 [2] => DSC02432.jpg 
 [3] => DSC02437.jpg 
) 

I want to copy this files from one to another folder, but how can i achive this? I made a function but it isnt working:

function copyFiles($array) {
    for ($i = 0;$i < count($array);$i ++) {
        copy("./" . $array[$i] , "/$folderlabel/" . $array[$i]);
    }
}

copyFiles($files);
martynas
  • 11,368
  • 3
  • 50
  • 59
user2987790
  • 125
  • 8
  • 1
    what does "isn't working" mean? what's (not) happening? any error messages you get? – benomatis Apr 20 '14 at 14:45
  • Sorry, here's the Error: `Warning: copy(//KG002-DSC02425.jpg): failed to open stream: Permission denied in /volume1/web/www/pindex/index.php` etc – user2987790 Apr 20 '14 at 14:47
  • 1
    well then, learn from that error and try to correct it, start by ensuring you do have access to the given folder / file... – benomatis Apr 20 '14 at 14:48
  • 1
    ...and include this in your original post pls, make sure you add it's an edit, otherwise the already given answer may become obsolete... – benomatis Apr 20 '14 at 14:50

1 Answers1

0

Corrected function:

function copyFiles($array,$sourcePath,$savePath) {
for($i = 0;$i < count($array);$i++){
    copy($sourcePath.$array[$i],$savePath.$array[$i]);}
}
user2987790
  • 125
  • 8
  • 1
    Do *not* use `global`. Pass it as a function parameter instead: `function copyFiles($array,$folderlabel) { ... }`, and call your function like so: `copyFiles($files, $folderlabel);`. See [this answer](http://stackoverflow.com/a/5166527/1438393) for why `global`s are evil. – Amal Murali Apr 20 '14 at 14:54
  • Yes, make it a parameter. As well as the beginning ("`./`") so you can specify the working directory that function operates on. Otherwise if your script is called from a different working directory, it will crash again. – hakre Apr 20 '14 at 14:55
  • @user2987790: Also take care that [`copy`](http://php.net/copy) has a return value. Check it and deal with errors. – hakre Apr 20 '14 at 14:58