在用户标识目录中创建文件

时间:2013-10-08 17:25:51

标签: php jquery joomla

我要做的是创建一个文件“dados.json”到与Joomla中的user_id匹配的目录。

因此,例如,用户15将拥有一个文件“... / files / 15 / dados.json”。

dados.json的内容是对象中包含的数据。我正在使用Ajax将数据发送到PHP脚本escreve.php:

/*Get the user Id and create a directory with that id if it doesn't exist yet */
    $user = JFactory::getUser();
    $usr_id = $user->get('id'); 
    $diretorio = JPATH_SITE."/Apps/files/".$usr_id;
    if (!JFolder::exists($diretorio)) {
        JFolder::create($diretorio, 0775);
    };

    /*Get the data and save it to the user directory in file dados.json */
    $myFile = $diretorio."/dados.json";
    $fh = fopen($myFile, 'w') or die("não é possível abrir o ficheiro");
    $stringData = $_POST['data'];
    $stringData='{  "data":'.json_encode($stringData).'}';
    fwrite($fh, $stringData);
    fclose($fh);    

但这不起作用。不创建目录,也不创建文件。我没有得到PHP的错误。

ajax如下:

$parent.find('button[name=save]').click(function () {
  $.ajax({
    url: "../Apps/php/escreve.php",
    data:{'data': handsontable.getData()}, //returns all cells' data
    dataType: 'json',
    type: 'POST',
    success: function (res) {
      if (res.result === 'ok') {
        $console.text('Data saved!');
      }
      else {
        $console.text('Save error1');
      }
    },
    error: function () {
      $console.text('Save error2');
    }
  });
});

这是将“保存错误2”记录到控制台。

请帮忙!?

0 个答案:

没有答案