homepage Welcome to WebmasterWorld Guest from 54.196.196.108
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Beginners held with "Or Die" Command
Help - I have not got my PHP Book,
Dayo_UK




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

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)

 

Distel




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

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";
}

Dayo_UK




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

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"

mogwai




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

"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.

Dayo_UK




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

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 :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved