2012-08-14 90 views
0

我刚刚开始从互联网学习,我很新。我看到了一些文件处理的例子。但是,当我按照文件的相同步骤时,它不起作用。获取用户输入到php文件

阅读功能是工作文件。但写入文件不起作用。我也试过用file_put_content函数来写。 :(

<?php 
    if(isset($_POST['submit1'])){ 
    $fileName = "Text.txt"; 
    $fh = fopen($fileName,"a") or die("can not open the file to write"); 

    //Writing to a file 
    $newData = "Hello This is new Data"; 
    fwrite($fh,$newData); 
    fclose($fh); 

    //Reading a file -- Working 
    $text = fopen("Text.txt","r") or die ("can not open the file to read");  
    while(!feof($text)) 
    { 
     $myLine = fgets($text); 
     print $myLine; 
    } 
    fclose($text); 
    } 

?> 

请指导我.. 感谢

+0

它不工作,因为它显示“无法打开文件写入“? – 2012-08-14 09:37:48

+0

你应该检查你的文件夹/文件的权限 - 帐户是否有权访问该文件来写?发布您收到的错误消息以帮助确定问题。 – Fluffeh 2012-08-14 09:38:19

+0

是的,此消息出现在文件写入部分。但在文件阅读的情况下,它工作正常。 – 2012-08-14 09:39:19

回答

1

这工作得很好,什么是你的错误

<?php 
    $file = 'text.txt'; 
    $writer = fopen($file, 'a'); 
    $addData = 'This is a new string to be added at the end of the file'; 
    fwrite($writer, $addData); 
    fclose($writer); 
    ?> 

EDIT1: 从邮寄进境的输入要求,您可以做这样的事情:

<?php 
     if ($_SERVER['REQUEST_METHOD'] == 'POST'){ 
     $addData = $_POST['input-name']; 
     $file = 'text.txt'; 
     $writer = fopen($file, 'a'); 

     fwrite($writer, $addData); 
     fclose($writer); 
     } 
    ?>