本文介绍了用php在另一个目录中创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的文件夹结构就像-
root
admin
create_page.php
pages
my_page1.php
my_page2.php
我有用于在页面"文件夹中创建新的php文件的代码.代码就像-
I have code for creating a new php file in "pages" folder. the code is like -
$dir_path = "../pages/";
$ourFileName = '../'.$page_name.".txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
$ourFileContent = '<?php echo "something..." ?>';
if (fwrite($ourFileHandle, $ourFileContent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
代码正常执行..没问题.但是该页面没有被创建.请告诉我我在做什么错.路径有问题吗? fclose($ ourFileHandle);
The code executes normally..no problem. but the page is not being created. please tell me what i am doing wrong. is there problem with the path? fclose($ourFileHandle);
推荐答案
以下是使用更简单的 file_put_contents() fopen,fwrite,fclose
Here's an example using the more simpler file_put_contents() wrapper for fopen,fwrite,fclose
<?php
error_reporting(E_ALL);
$pagename = 'my_page1';
$newFileName = './pages/'.$pagename.".php";
$newFileContent = '<?php echo "something..."; ?>';
if (file_put_contents($newFileName, $newFileContent) !== false) {
echo "File created (" . basename($newFileName) . ")";
} else {
echo "Cannot create file (" . basename($newFileName) . ")";
}
?>
这篇关于用php在另一个目录中创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!