我试图创建一个子目录中的文件文件,但它也没有创造它创建目录
$tempFilename = "xml/tempfile.xml";
if(file_exists($tempFilename))
{
unlink($tempFilename);
}
$file = fopen($tempFilename,"w");
$fileTemplate = ($xml);
我试图创建一个子目录中的文件文件,但它也没有创造它创建目录
$tempFilename = "xml/tempfile.xml";
if(file_exists($tempFilename))
{
unlink($tempFilename);
}
$file = fopen($tempFilename,"w");
$fileTemplate = ($xml);
使用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);
应该在打开文件后立即创建该文件,不需要在其中写入任何内容。如果你不写任何东西,你会得到一个空文件。 – Barmar
您可以使用file_put_contents
file_put_contents($tempFilename, '23');
试试这个
首先你需要创建目录
$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);
'xml'文件夹是否存在? –
你想用'$ fileTemplate =($ xml);'做什么? – kmas