do somthing by one of the method

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
wvoyance
Forum Contributor
Posts: 135
Joined: Tue Apr 17, 2012 8:24 pm

do somthing by one of the method

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: do somthing by one of the method

Post 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;
}
User avatar
wvoyance
Forum Contributor
Posts: 135
Joined: Tue Apr 17, 2012 8:24 pm

Re: do somthing by one of the method

Post 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");
}
Post Reply