当我使用 file_put_contents 时,我试图覆盖我在 php 中创建的文件,我收到 0 个写入的“x”字节
使用下面使用 fwrite 的函数,我得到文件不可写的错误。
权限为 777(包括目录)
创建文件没有问题,问题是当我尝试修改它们时。
谁能弄清楚问题出在哪里?
function update_body($body)
{
$URL = $_REQUEST["URL"];
$filename = $URL;
$somecontent = $body;
if (is_writable($filename))
{
if (!$handle = fopen($filename, 'w'))
{
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $somecontent) === FALSE)
{
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
}
else
{
echo "The file $filename is not writable";
}
}
我创建文件的过程
function save_body($body)
{
$datearr = getdate(); //get date array, use date for folder, time for filename
$current_date = $datearr['year'].$datearr['mon'].$datearr['mday'];
$current_time = $datearr['hours'].$datearr['minutes'].$datearr['seconds'];
// create directory according the date
if (!file_exists($_SERVER['DOCUMENT_ROOT'] .'/articles/'.$current_date))
mkdir($_SERVER['DOCUMENT_ROOT'] .'/articles/'.$current_date, 0777, true);
// write the file
$fp = fopen($_SERVER['DOCUMENT_ROOT'].'/articles/'.$current_date.'/'.$current_time.'.txt',"w");
fwrite($fp,$body);
fclose($fp);
return $current_date.'/'.$current_time;
}