即使检查了权限,限制和文件大小,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);

?>

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";

?>

My guess is $_POST['filename']; is empty. The error:

Warning: file_put_contents(data/) [function.file-put-contents]: failed to open stream: Is a directory

Is self explanatory and even shows data/. Notice the lack of file name after the "/"? Check your incoming post.

More Details

The Super Global $_POST does not have a key filename or that key is empty. This line of code $filename = 'data/'.$_POST['filename']; produces this: data/. So either $_POST['filename'] is empty or it does not exist. Use var_dump or print_r to examine $_POST and see what's in it.

The problem seems to be based around your content type. In PHP, only a Content-Type of application/x-www-form-urlencodedor multipart/form-data populates the $_POST super global array. Since you are telling the server that you are sending the data via application/json, you need to fetch the data from the request body via:

php://input

Try this:

$params = json_decode("php://input", TRUE);
$filename = 'data/'.$params['filename'];
$data = $params['filedata'];
file_put_contents($filename, $data);