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 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口