0

I have this php code :

$controlNum = '0111111';
$path = 'http://00.000.00:0000/Files/';

$version1 = '201909R14';
$version2 = '201909R16';

$path1 = $path.'/'.$controlNum.'/FIRST/MY%20DATA/'.$controlNum.'-FOR%20RECEIPT%20(with%20copy)%20FINAL%20DATA/'.$controlNum.'-'.$version1.'.txt' ; // declare exact path of the data
$path2 = $path.'/'.$controlNum.'/FIRST/MY%20DATA/'.$controlNum.'-FOR%20RECEIPT%20(with%20copy)%20FINAL%20DATA/'.$controlNum.'-'.$version2.'.txt' ; // declare exact path of the data    

if (fopen($path1, "r")) {
    echo 'FILE FOUND ';
}
    if(fopen($path2, "r")){
        echo 'FILE FOUND ';
    }else{
        echo 'FILE NOT FOUND ';
}

It gives an OUTPUT like this :

FILE FOUND FILE NOT FOUND

But what I want is to only output either FILE FOUND and FILE NOT FOUND only.

If first condition is false it must not echo, it must go for the second if condition.

How can I achieve that? sorry for this question, but i'm still learning..

Gelxxxx
  • 161
  • 1
  • 17
  • This might help you figure it out: https://www.php.net/manual/en/control-structures.elseif.php – AMC Feb 07 '20 at 01:52

1 Answers1

0

If all you want to do is check if the files exist, you should use file_exists instead. I think what you want is to check both files together e.g.

if (file_exists($path1) && file_exists($path2)) {
    echo 'FILE FOUND';
}
else {
    echo 'FILE NOT FOUND';
}

If you only need one of them to exist, just change the && to ||:

if (file_exists($path1) || file_exists($path2)) {
    echo 'FILE FOUND';
}
else {
    echo 'FILE NOT FOUND';
}
Nick
  • 138,499
  • 22
  • 57
  • 95
  • file_exists somehow doesn't work for me... but thanks this is what I want :) – Gelxxxx Feb 07 '20 at 02:28
  • @Gelxxxx that's weird. Well you can always substitute `fopen`... – Nick Feb 07 '20 at 02:29
  • Please be more active in closing duplicates. @Nick You are not new here and you KNOW that these basic questions are already well-answered elsewhere. If you NEED more unicorn points for some unknown reason, perhaps ask on Meta for rep points to be earned upon applying a correct duplicate. By answering these, the system is deeming the OP's questions to be "good" -- but they represent redundant content. – mickmackusa Feb 07 '20 at 02:56
  • @mickmackusa OPs problem was **not** determining whether the files existed or not, their problem was with implementing the correct logic to only output one message dependent on whether one or both flles existed. Note in the question: `It gives an OUTPUT like this : FILE FOUND FILE NOT FOUND But what I want is to only output either FILE FOUND and FILE NOT FOUND only` – Nick Feb 07 '20 at 02:59
  • So it also needs a duplicate for how to write a two part condition with or? Come on Nick. We don't have a dupe for that either? – mickmackusa Feb 07 '20 at 03:04