不能用PHP覆盖文件

时间:2013-07-20 12:55:15

标签: php

我试图覆盖我在php中创建的文件 当我使用file_put_contents时,我收到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;
}

1 个答案:

答案 0 :(得分:0)

欢迎使用Stack Overflow。

正如@Sebas所说,尝试使用相对路径(../../../articles)或文件夹而不是

$_SERVER['DOCUMENT_ROOT']

度过美好的一天!