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

如何使用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条)

报告相同问题?

悬赏问题

  • ¥15 浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题