在php中使用file_put_contents将文件/内容放入目录
在使用php时,我面临一个非常简单的问题在php中使用file_put_contents将文件/内容放入目录,php,Php,在使用php时,我面临一个非常简单的问题 file_put_contents("somefile.txt",$content) 它可以工作,但当我试图把文件放在某个目录中,比如- file_put_contents("somedirectory/sonefile.txt",$content); 它不起作用。知道我错过了什么吗 准确的代码是- file_put_contents($_SERVER['DOCUMENT_ROOT']."/temp_code/code.txt",$code);
file_put_contents("somefile.txt",$content)
它可以工作,但当我试图把文件放在某个目录中,比如-
file_put_contents("somedirectory/sonefile.txt",$content);
它不起作用。知道我错过了什么吗
准确的代码是-
file_put_contents($_SERVER['DOCUMENT_ROOT']."/temp_code/code.txt",$code);
尝试使用绝对路径:
file_put_contents(realpath("somedirectory/sonefile.txt"),$content);
尝试使用绝对路径:
file_put_contents(realpath("somedirectory/sonefile.txt"),$content);
试试chmod($dir,0777)//使其可写'
$dir="somedirectory";
if (!is_dir($dir))
{
mkdir($dir); //create the directory
chmod($dir, 0777); //make it writable
}
file_put_contents($dir."/somefile.txt",$content);
试试chmod($dir,0777)//使其可写'
$dir="somedirectory";
if (!is_dir($dir))
{
mkdir($dir); //create the directory
chmod($dir, 0777); //make it writable
}
file_put_contents($dir."/somefile.txt",$content);
使用realpath()
作为@Amal的答案,或使用$\u服务器['DOCUMENT\u ROOT']
如下所示
file_put_contents($_SERVER['DOCUMENT_ROOT']."path/to/your/folder/somefile.txt",$content);
在编程时也要注意拼写
sonefile.txt
->somefile.txt
最后检查文件夹和文件的权限是否具有写入权限。使用realpath()
作为@Amal的答案,或使用$\u服务器['DOCUMENT\u ROOT']
如下
file_put_contents($_SERVER['DOCUMENT_ROOT']."path/to/your/folder/somefile.txt",$content);
在编程时也要注意拼写
sonefile.txt
->somefile.txt
最后检查文件夹和文件的权限是否具有写入权限。解释“不工作”:错误?这显然是一个错误的问题。请记住,php从执行脚本的当前位置“搜索”。有关更多信息,请参阅realpath、pathinfo和chdir手册。是否存在“somedirectory”?是否存在somedirectory?解释“不工作”:错误?这显然是一个错误的问题。请记住,php从执行脚本的当前位置“搜索”。有关更多信息,请参阅realpath、pathinfo和chdir手册。是否存在“somedirectory”?是否存在somedirectory?您应该输入文件夹的正确路径。在那之前它不会起作用。只需打印
$\u服务器['DOCUMENT\u ROOT']
并将缺少的部分连接到文件夹路径。您应该输入文件夹的正确路径。在那之前它不会起作用。只需打印$\u服务器['DOCUMENT\u ROOT']
,并将缺少的部分连接到文件夹路径。