2011-02-17 106 views
0

以下代码不会创建info.txt文件并死亡: 如何在这种情况下显示错误代码 - 用“。”附加到死命令?将用户输入写入文件

$MorF .= $name ." ". $family; 
$MorF .="with username " . $user; 
$MorF .=" and 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 !"); 
} 
+1

@Skaffman我意识到阿拉伯可能看起来像perl的,但它仍然是PHP :) – Tim 2011-02-17 20:01:27

回答

0

您可以使用try...catch,以便您在遇到错误时获得一些调试信息。另外,不确定这是否是有意的,但当文件尚不存在时,您的逻辑会使脚本失败。我已经在这里列出了这个条件的补贴,但我不确定这是否是您的意图。

try { 
    // do not continue if file does not exist 
    if (!file_exists("info.txt")) 
     die('Something went wrong: file does not exist'); 
    // append the data to the file 
    $fileLines = file_get_contents("info.txt"); 
    $fileNewLines = $fileLines . $MorF . "\n"; 
    file_put_contents("info.txt", $MorF);  
    } catch (Exception $e) { 
    // handle an error 
    die("Something went wrong: ".$e->getMessage()); 
    }