raysefo Posted June 7, 2013 Share Posted June 7, 2013 Hi, I am new to php. I wrote a sample code as follows; try { //Get Files to Parse $homepage = file_get_contents('ftp://username:[email protected]:21/RESULTS/test.txt'); $kw = explode("\n", $homepage); $totalNumRow = 0; $totalAmount = 0; for($i=0;$i<count($kw);$i++){ if(substr($kw[$i], 0, 1) == "5" && substr($kw[$i], 1, 1) <> "/") { // echo $kw[$i]; $totalNumRow = $totalNumRow + 1; $originalstring = $kw[$i]; $delimiter = ","; if(strpos($originalstring,$delimiter) > 0){ $outarray = explode($delimiter,$originalstring); // $variable1 = $outarray[0]; $amount = $outarray[1]; // echo $amount; $totalAmount = $totalAmount + $amount; } // echo "<br>"; } } } catch (Exception $e) { $msg = $e->getMessage(); sendEmail($msg); } Lets say if the file is not in server ($homepage = file_get_contents('ftp://username:[email protected]:21/RESULTS/test.txt') , is there a way to catch and send this error? Best Regards. Link to comment https://forums.phpfreaks.com/topic/278880-try-catch-and-how-to-email-error-message/ Share on other sites More sharing options...
Eddy_J1 Posted June 7, 2013 Share Posted June 7, 2013 Have a look at this http://php.net/manual/en/function.mail.php Also you should really comment your code more and remove all those test echos Link to comment https://forums.phpfreaks.com/topic/278880-try-catch-and-how-to-email-error-message/#findComment-1434601 Share on other sites More sharing options...
cpd Posted June 7, 2013 Share Posted June 7, 2013 You'll need to do some testing but I'd have thought file_get_contents() would return FALSE and not trigger an E_WARNING because you're retrieving a file using the File Transfer Protocol. I'm not 100% sure as to what the result is if it fails though so test it. Once you've found out what the trigger is, you can do something like: try { if($file === false) { throw new Exception("Message"); } } catch(Exception $e) { // Handle } Link to comment https://forums.phpfreaks.com/topic/278880-try-catch-and-how-to-email-error-message/#findComment-1434618 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.