簡體   English   中英

PHP:將文件保存到特定目錄

[英]PHP: Save file to a specific directory

我有這個diretorio.php文件,該文件在Joomla中獲取用戶ID並創建具有該ID的目錄(如果尚不存在):

/* Get the current user id */
$user = JFactory::getUser();
$usr_id = $user->get('id');

/*Define the path to this user's directory */
$diretorio=$_SERVER['DOCUMENT_ROOT']."/Apps/files/".$usr_id;

/*Create the user's directory if it doesn't exist */
if (!file_exists($diretorio) and !is_dir($diretorio)) {
    mkdir($diretorio, 0755);
};

現在,我想使用一個Ajax將一個包含數據的文件保存在一個對象中,該Ajax會將另一個PHP文件觸發到與上面創建的相同目錄中:

$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);

但是,未創建該文件。 如果我將第一行替換為:

$myFile = "dados.json";

它將在存儲此PHP腳本的同一目錄中創建文件。

我建議像這樣使用Joomla編碼標准:

$user = JFactory::getUser();
$usr_id = $user->get('id');

$diretorio = JPATH_SITE . "/Apps/files/" . $usr_id;

if (!JFolder::exists($diretorio)) {
    JFolder::create($diretorio, 0775);
}

$myFile = $diretorio."/dados.json";

$stringData = $_POST['data'];
$stringData = '{ "data":'.json_encode($stringData).'}';
JFile::write($myFile, $stringData);

JPATH_SITE是您的Joomla網站的根

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM