php 用php在另一个目录中创建文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9957485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create file in another directory with php
提问by Imrul.H
My folder structure is like -
我的文件夹结构就像 -
root
admin
create_page.php
pages
my_page1.php
my_page2.php
I have code for creating a new php file in "pages" folder. the code is like -
我有在“pages”文件夹中创建新php文件的代码。代码就像 -
$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;
}
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);
代码正常执行..没问题。但页面没有被创建。请告诉我我做错了什么。路径有问题吗?fclose($ourFileHandle);
回答by Lawrence Cherone
Here's an example using the more simpler file_put_contents()wrapper for fopen,fwrite,fclose
这是一个使用更简单的file_put_contents()包装器 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) . ")";
}
?>
回答by ghazi
$ourFileHandle = fopen("../pages/" .$ourFileName, 'w') or die("can't open file");
Make the above change to the third line and it will probably work; I tried it and it worked.
对第三行进行上述更改,它可能会起作用;我试过了,它奏效了。
回答by Kishor Kundan
have you deliberately missed out on concatinating $dir_path and $ourFileName;
你是否故意错过了 $dir_path 和 $ourFileName 的连接?
now
现在
- is your directory/file writable?
- check for your current working directory
- is your error_reporting on ?
回答by Kuldeep
Had the same Problem, File will not be created in a folder having different permission, make sure the permissions of the folders are same like other files.
有同样的问题,文件不会被创建在具有不同权限的文件夹中,确保文件夹的权限与其他文件相同。
回答by Jibin Mathew
Try this
尝试这个
$pagename = 'your_filename';
$newFileName = '../pages/'.$pagename.".php";