Page 1 of 1

do somthing by one of the method

Posted: Tue May 22, 2012 7:34 pm
by wvoyance
I want to copy a file from either destination A, or B, or C, or D....etc
If anyone succeeded, jump out and continue to do the next task.

How should I write such program? It is not CASE, not SWITCH, not IF....I just don't know how should I do it.
Can anyone give me some suggestion? Thanks.

Re: do somthing by one of the method

Posted: Tue May 22, 2012 10:19 pm
by requinix
A few ways. I'm partial to an array.

Code: Select all

$paths = array("/path/1", "/path/2", "/path/3");
foreach ($paths as $path) {
    if (copy($from, $to)) break;
}

Re: do somthing by one of the method

Posted: Wed May 23, 2012 6:29 am
by wvoyance
Yes, it is indeed more elegant.
So, you have other ways? Can you name some.

Furthermore, I want to test whether has there anything already there before copy,
since there might have other way to put the file.

My originally program looks like this:

if (!file_exists (DIR_FS_CATALOG_IMAGES . $image_name)) {
$success = copy('http://S', DIR_FS_CATALOG_IMAGES . $image_name);
echo ("image copied from S");
}
....
.....repeat many similar lines
....
if (!file_exists (DIR_FS_CATALOG_IMAGES . $image_name)){
$success = copy('http://A' , DIR_FS_CATALOG_IMAGES . $image_name);
echo ("image copied A");
}