2017-05-30 1 views
1

これは少し基本的に聞こえるが、私はこれを行う方法を知らない、 私はJSONファイルを書き込むことができていますが、私はそれが特定のディレクトリに格納する必要があります。書き込みJSONは

   $theColor = array('color' => 'red'); 
       $fp = fopen('color.json', 'w'); 
       fwrite($fp, json_encode($theColor)); 
       fclose($fp); 

これを書き込むことはできますが、ファイルはルートに表示されます。私はワードプレスを使用しています。あなたのことを確認し

$fp = fopen('/path/to/directory/color.json', 'w'); 

$fp = fopen('color.json', 'w'); 

:次の行を置き換え

答えて

1

>/

任意のアイデア:私は特定のフォルダにまたは私のドライブCに転送する必要がありますしかし、/path/to/directory/に正しい権利を持っています。


あなたのコメントで尋ねられたように。ファイルをダウンロードするコード。

$data = "/path/to/directory/color.json"; 
header("Content-Type: application/json"); 
header('Pragma: public'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Cache-Control: private', false); 
header('Content-Disposition: attachment; filename="color.json"'); 
header('Content-Transfer-Encoding: binary'); 
header('Content-Length: ' . filesize($data)); 
readfile($data); 
exit; 

よく知られているポップアップが表示されます。そうでない場合は、file_get_contents("/path/to/directory/color.json")

や関数などへの最初のデータ変数を変更しよう:

/** 
* This method sets generates the headers for a file download and sends the file. PHP is exited after this function 
* 
* @param string $fileName The name of the file, as displayed in the download popup 
* @param string $data  The path to the file, or the contents of the file 
* @param string $contentType The content type of the file 
* @param bool $file   Whether or not $data is a the path to a file, or the file data.<br /> 
*       True means $data contains the path to the file<br /> 
*       False when $data is a the data as a string 
* 
* @return void Exits PHP 
*/ 
function outputForDownload($fileName, $data, $contentType, $file = true) 
{ 
    header("Content-Type: {$contentType}"); 
    header('Pragma: public'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Cache-Control: private', false); 
    header('Content-Disposition: attachment; filename="' . $fileName . '"'); 
    header('Content-Transfer-Encoding: binary'); 
    if ($file === true) { 
     header('Content-Length: ' . filesize($data)); 
     readfile($data); 
    } else { 
     header('Content-Length: ' . mb_strlen($data)); 
     echo $data; 
    } 
    exit; 
} 
+0

おかげでその作業。 保存後に前記ファイルをダウンロードする方法も知っていますか?ファイルがサーバーから救われたと仮定すると、私は私の編集を参照してください私の地元 –

+0

にダウンロードしますが、私はまた、私はそれに使用する機能が含まれて – Jelmergu