将结果保存到文本文件

时间:2018-07-19 01:17:14

标签: php file

我已经在该站点附近搜索了答案,但是找不到任何帮助我的问题。 我有一个带有表单的脚本,我想在按下“提交”按钮时将输入的内容写到txt文件中(例如,在mytext.txt文件中逐行显示),或者可能仅是结果更简单。

结果/内容“ echo $ result ['text'];”应该保存到文本文件(mytext.txt)

我试图在每个“回声”之后添加类似的内容,但是它不起作用。这是示例代码。

$f = fopen("mytext.txt", "w");
fwrite($f,  $result);
fclose($f);

$f = fopen("mytext.txt", "w");
fwrite($f,  $username);
fclose($f);

$txt = "$result";
$myfile = file_put_contents('mytext.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);

但仍然没有运气。 我该怎么做以及在哪里添加呢?使用PHP还是使用JavaScript?请帮助。

编辑 : 脚本的一部分(对不起,我忘了它)

<?php

if(count($check_ex) == 2){
    $ex_name = $check_ex[0]."_".$check_ex[1];
}else{
    $ex_name = $check_ex[0];
}
            $additional_button = "<a href='javascript:void(0)' onclick='submitform_$dom_$ex_name()' ><button id='buy' class='btn btn-success btn-xs pull-right order-btn'>".__($additional_button_name, 'kate')."</button></a>";
        }elseif($integration == 'woocommerce'){
            if($show_price){
                $show_price = '- '.kate_display_price($username).__('/year','kate');
            }
            $additional_button = "<a href='?&add-to-cart=$additional_button_link&username=$username' id='buy' class='btn btn-success btn-xs pull-right order-btn' $buy_new_tab >".__($additional_button_name,'kate')." $show_price</a>";
            }elseif($integration == 'custom'){
            if(!$additional_button_name == '' AND !$additional_button_link == ''){
                $additional_button_links = str_replace( '{username}', $username, $additional_button_link );
                $additional_button = "<a id='buy' class='btn btn-success btn-xs pull-right order-btn' href='$additional_button_links' $buy_new_tab >".__($additional_button_name,'kate')."</a>";
            }else{
                $additional_button = '';
            }
        }else{
            $additional_button = '';
        }

        $custom_not_found_result_text = str_replace( '{username}', $username, $custom_not_found_result_texts );
        $whmcs = "<script type='text/javascript'>
                function submitform_$dom_$ex_name()
                {
                  document.whmcs_$dom_$ex_name.submit();
                }
                </script>
                <form method='post' name='whmcs_$dom_$ex_name' id='whmcs' action='$additional_button_link/cart.php?a=add&username=register' $buy_new_tab>
                <input type='hidden' name='usernames[]' value='$username' >
                <input type='hidden' name='usernamesregperiod[$username]' value='1'>
                </form>";
        if ($available->status == 1) {
                $result = array('status'=>1,
                                'username'=>$username, 
                                'text'=>    '<div class="callout callout-success alert-success clearfix available">
                                            <div class="col-xs-10" style="padding-left:1px;text-align:left;">
                                            <i class="glyphicon glyphicon-ok" style="margin-right:1px;"></i> '.__($custom_found_result_text,'kate').' </div>
                                            <div class="col-xs-2" style="padding-right:1px">'.__($additional_button,'kate').' '.$whmcs.'</div>
                                            </div>
                                            '); 
                echo $result['text'];



        } elseif($available->status == 0) {
                $result = array('status'=>0,
                                'username'=>$username, 
                                'text'=>    '<div class="callout callout-danger alert-danger clearfix not-available">
                                            <div class="col-xs-10" style="padding-left:1px;text-align:left;">
                                            <i class="glyphicon glyphicon-remove" style="margin-right:1px;"></i> '.__($custom_not_found_result_text, 'kate').' 
                                            </div>
                                            <div class="col-xs-2" style="padding-right:1px">'.$www_link.'</div>
                                            </div>
                                            ');
                echo $result['text'];


        }elseif ($available->status == 2) {
                $result = array('status'=>2,
                                'username'=> $username, 
                                'text'=>    '<div class="callout callout-warning alert-warning clearfix notfound">
                                            <div class="col-xs-10" style="padding-left:1px;text-align:left;">
                                            <i class="glyphicon glyphicon-exclamation-sign" style="margin-right:1px;"></i> '.__('not found','kate').' 
                                            </div>
                                            </div>
                                            ');
                echo $result['text'];


        }
    }
    }
    else
    {
        echo 'Please enter the username';
    }
} 

3 个答案:

答案 0 :(得分:0)

如果在尝试运行此代码时遇到错误,请检查是否已授予PHP文件访问权限以将信息写入硬盘。

答案 1 :(得分:0)

您必须检查要保存txt文件的目录是否可写。 示例:

$contents = 'Here goes your content';
$dirName = __DIR__; //in case you're saving in another directory, you may use 'files/directory/' instead of __DIR__
$fileName = $dirName.'/mytext.txt';

if (!is_writable($dirName)) {
    throw new \Exception("This folder is not writable: '$dirName'");
}

//Checks if the content was added
if (!file_put_contents($fileName, $contents)) {
    throw new \Exception("Could not write to file: $fileName");
}

答案 2 :(得分:0)

代码正确,可能是目录无权创建文件。