在目录中创建文件

时间:2013-12-15 10:22:53

标签: php

我正在尝试在子目录中创建一个文件,但它没有创建它

$tempFilename = "xml/tempfile.xml";
    if(file_exists($tempFilename))
            {    
                unlink($tempFilename);
            }
        $file = fopen($tempFilename,"w");
        $fileTemplate = ($xml);

3 个答案:

答案 0 :(得分:0)

使用fwrite

$tempFilename = "xml/tempfile.xml";
// Test if directory exists.
$dirname = dirname($tempFilename);
if (!is_dir($dirname))
{
    mkdir($dirname);
}
if(file_exists($tempFilename)){    
     unlink($tempFilename);
}
$file = fopen($tempFilename,"w");

// Write what you need
fwrite($file, 'content of my file');
fclose($file);

答案 1 :(得分:0)

您可以使用file_put_contents

file_put_contents($tempFilename, '23');

答案 2 :(得分:0)

试试这个

首先需要创建目录

$path = '/xml';
mkdir($path, 0777, true);
$tempFilename = "/xml/tempfile.xml";
if(file_exists($tempFilename))
        {    
            unlink($tempFilename);
        }
    $file = fopen($tempFilename,"w");
   fwrite($file, '23');
fclose($fp);