dtn36013 2017-08-14 05:40
浏览 59
已采纳

如何使用php从传递给url中的html页面的参数创建文本文件?

I need my page bargraph.html to get parameters like .../bargraph.html?di=xxxx&mn=yyyy and save the values of di and mn using a php script in a text file named cred.txt. The code I'm using for bargraph.html is

<body>
    <?php
       $mobile_num = $_GET["mn"];
       $device_id = $_GET["di"];
       $file_name = "cred.txt";
       $location = "cred/".$file_name;
       $text = $mobile_num."
".$mobile_num;
       $my_file = fopen($location, "w") or die("Unable to open file!");
       fwrite($my_file, $text);
       echo "response submitted successfully!";
       fclose($my_file);
    ?>
</body>

The file named cred.txt is not created inside the cred/ directory and neither I get any errors. What am I doing wrong?

If the same thing can be done using JavaScript I'll use that instead of php for this purpose.

  • 写回答

2条回答 默认 最新

  • douzhu3367 2017-08-14 06:16
    关注

    The question references bargraph.html - presumably the php code that you have is NOT on that page but a separate script? If that is the case then if you were to use exception handling to try to track down the issue it might help. Also, I have always found better success when using full paths as opposed to relative ones

    <?php
        if( isset( $_GET['mn'], $_GET['di'] ) ){
            try{
                $filename='cred.txt';
    
                $mobile_num=filter_input( INPUT_GET, 'mn', FILTER_SANITIZE_STRING );
                $device_id=filter_input( INPUT_GET, 'di', FILTER_SANITIZE_STRING );
                /*
                    I have always found it is best to use a full path rather than relative
                    Change `path/to/` to the appropriate path
                */
                $path=$_SERVER['DOCUMENT_ROOT'] . '/path/to/cred';
    
                /* If the path does not exist, warn user */
                if( !realpath( $path ) ){
                    throw new Exception( sprintf( 'Unable to find path: %s', $path ) );
                }
    
                /* Can the chosen directory be read? */
                if( is_readable( $path ) && is_writable( $path ) ){
    
                    $file=$path . '/' . $filename;
                    #$text=$mobile_num . PHP_EOL . $mobile_num . PHP_EOL;
    
                    /* I think this is probably what you intended? */
                    $text=$device_id . PHP_EOL . $mobile_num . PHP_EOL;
    
                    $status=file_put_contents( $file, $text, FILE_APPEND | FILE_TEXT );
                    throw new Exception( $status ? sprintf('All good! Saved %s',$file) : sprintf('Error - unable to save %s',$file) );
    
                } else {
                    /*
                        should set permissions if reading/writing of target folder failed
                        chmod($path,0777); etc
                    */
                    throw new Exception( sprintf( 'The path %s is either not readable or writable',$path ));
                }
            }catch( Exception $e ){
                exit( $e->getMessage() );
            }
        }
    ?>
    

    Using a plain HTML page you could send an ajax request to the above PHP script ( in code below called bargraph.php )

    <html>
        <head>
            <title>ajax-store credentials</title>
    
        </head>
        <body>
            <form id='bg'>
                <input type='text' name='mn' id='mn' placeholder='Mobile number: eg 0141 353 3874' />
                <input type='text' name='di' id='di' placeholder='Device ID: eg yellow banana' />
                <input type='button' id='bttn' value='Go' />
            </form>
            <script>
                document.getElementById('bttn').onclick=function(e){
    
                    var mn=document.getElementById('mn').value;
                    var di=document.getElementById('di').value;
                    if( mn != '' && di != '' ){
                        var xhr=new XMLHttpRequest();
                        xhr.onload=function(r){
                            document.getElementById('status').innerHTML=this.response;
                        };
                        xhr.onerror=function(r){
                            document.getElementById('status').innerHTML=err.message;
                        };
                        xhr.open('GET','?mn='+mn+'&di='+di,true);
                        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                        xhr.send();
                    }
                }
            </script>
            <div id='status'></div>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历