简体   繁体   中英

Saving result to text file

I've searched around this site for an answer but couldn't find any help in my problem. I have a script with a form and I'd like to get the contents of the input written into a txt file (line by line in mytext.txt file for example) when the submit button is pressed - or maybe simpler only the result.

Result / content of "echo $result['text'];" should be saved to text file (mytext.txt)

I tried to add something like that after each "echo" but it doesn't work. here is the sample code.

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

or

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

or

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

but still no luck. How can I do that and where to add it? With PHP or maybe with JavaScript? Help please.

edit : PART of the script (I'm sorry I forgot about it)

<?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';
    }
} 

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

You must check if the directory you're trying to save your txt file is writable. Example:

$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");
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM