php - Writing user input to a file -
the below code doesn't create info.txt file , dies: how can show error codes in such case - appending die command "."?
$morf .= $name ." ". $family; $morf .="with username " . $user; $morf .=" , password " . $pass; $morf .=" lives in " . $city; $filelines = ""; if (file_exists("info.txt")) { $filelines = file_get_contents("info.txt"); $filenewlines = $filelines . $morf . "\n"; file_put_contents("info.txt", $filenewlines); } else { die("something went wrong !"); }
you can use try...catch
have debugging information upon encountering error. also, not sure if intentional, logic there has script fail when file not exist. i've included allowance condition here, not if intent.
try { // not continue if file not exist if (!file_exists("info.txt")) die('something went wrong: file not exist'); // append data file $filelines = file_get_contents("info.txt"); $filenewlines = $filelines . $morf . "\n"; file_put_contents("info.txt", $morf); } catch (exception $e) { // handle error die("something went wrong: ".$e->getmessage()); }
Comments
Post a Comment