Welcome to WebmasterWorld Guest from 54.166.191.159

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Beginners held with "Or Die" Command

Help - I have not got my PHP Book,

     
12:39 pm on Nov 28, 2003 (gmt 0)

10+ Year Member



Ok, sorry if this is a real dumb question, I am newish to PHP and have not got my PHP books as I am at my parents :(

Hopefully I can explain it properly! I am querying/opening text delimited files using the following code:-

<?php

$fp = @fopen("file1.csv", "rb") or die("Couldn't open file");
$data = fread($fp, filesize($fp));

etc etc etc?>

So basically we have the file and if we are unable to open it we have an or die command.

Then I want to run a further query on another text delimited file and then another and then another on the same web page. So we have the same code again but querying a different file:-

<?php

$fp = @fopen("file2.csv", "rb") or die("Couldn't open file");
$data = fread($fp, filesize($fp));

etc etc etc?>

Ok - now my problem! If any of the or die commands are activated anything after this on the page is not displayed! - is there a way that the or die command can just apply to the script that is being run on, so instead of:-

Text Delimited Displayed
Could'nt Open File
****No More Data returned for that page*****

We get:-

Text Delimited Displayed
Couldn't Open File
Text Delimited Displayed
Text Delimited Displayed

Any help would be appreciated (I have tried search with no luck)

12:50 pm on Nov 28, 2003 (gmt 0)

10+ Year Member



Can't test it right now, but I think this should do the trick:

if($fp = @fopen("file2.csv", "rb")){
$data = fread($fp, filesize($fp));
}
else{
echo "Couldn't open file";
}

1:32 pm on Nov 28, 2003 (gmt 0)

10+ Year Member



Thanks Distel,

Unfortunately, this seems to have the same problem, if the file is not opened the web page will stop after "Couldn't open file"

2:21 pm on Nov 28, 2003 (gmt 0)

10+ Year Member



"is there a way that the or die command can just apply to the script that is being run on"

No, die will stop the execution of the script when it is called.

The code provided by Distel should do the trick if you are looping through your list of csv files. The script shouldn't die if fopen returns FALSE it should just run the else code and move on to the next iteration.

3:09 pm on Nov 28, 2003 (gmt 0)

10+ Year Member



Distel, Mogwai

Yep, me being a bit niave and just dumping Distel code into mine (mainly because I did not show you the full code - I need the else{ etc after my etc etc:))- have done a bit of modification and now it is working fine.

Thanks a lot :)

 

Featured Threads

Hot Threads This Week

Hot Threads This Month