drvfqr5609 2018-04-13 18:11
浏览 43
已采纳

即使检查了权限,限制和文件大小,Php也无法打开流

I'm trying to make php save data on the server. Those data are generated through js script. However, I am unable to make the php create the file. The error msg I get is as follows: Warning: file_put_contents(data/) [function.file-put-contents]: failed to open stream: Is a directory in

I have checked the permissions and the php.ini for restrictions with zero success. Any ideas?

If I change the "data/" bit to "X", the server creates an empty file named X. Which seems strange. The commands used in the js script come from jsPsych.

Thanks a lot!

I append the code:

function saveData(name, data){
  var xhr = new XMLHttpRequest();

  xhr.open('POST', 'write_data.php');
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.send(JSON.stringify({filename: name, filedata: data}));
};

(...)

on_finish:

function(){
  saveData('subject-'+subject_id+'.csv', jsPsych.data.get().csv() );
}

<?php

$filename = 'data/'.$_POST['filename'];
$data = $_POST['filedata'];
file_put_contents($filename, $data);

?>
  • 写回答

3条回答 默认 最新

  • douhan8581 2018-04-13 18:54
    关注

    I am not an expert with Xhr & your style of doing it. I do however know jQuery, and regular JS.

    Jquery method:

    var postName = 'something'; // this is found with $_POST['postName'] in PHP
    var postName2 = 'somethingElse'; 
    $.post("path/yourFile.php", {postName:value, postName2:value}, function(data){
                if(data == 'success'){
                    // complete success 
                } else {
                    // assessment failed to save
                    console.log(data); 
                }
            });
    

    JS Methods:

    The first thing you do, is open up your request...

    var xhr= new XMLHttpRequest(); 
    xhr.open("POST", "write_data.php", true);
    

    Try this...

    xhr.open("POST", "write_data.php", true);

    Then you can either use this way of doing it...

    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.send("fname=Wubur&lname=Com");
    

    Or the JSON way...

    xmlhttp.setRequestHeader("Content-Type", "application/json");
    xmlhttp.send(JSON.stringify({fname:"Harry", lname:"Potter"}));
    

    yourFile.php (where we are POSTing to):

    If you use the first method (NOT json)...

    <?php 
    
    if(isset($_POST['fname']) && isset($_POST['lname']){ // check for post param & permission to create template 
        // do stuff here... 
        echo 'success'; // if everything worked
    } else {
        echo 'I need fname and lname!!!'; // no post params supplied 
    }
    
    ?>
    

    If you use JSON method...

    <?php 
    
    $data = json_decode(file_get_contents('php://input'), true);
    $data2 = json_decode(file_get_contents('php://input'));
    // try both, I forget which one it is...
    echo "success";
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料