php file_put_contents:无法打开流,没有这样的文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14783592/
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
file_put_contents: Failed to open stream, no such file or directory
提问by vaindil
I am trying to use dompdf to save a form to an easily-readable .pdf file, and my processing script is below. I am receiving the error Warning: file_put_contents(/files/grantapps/NAME0.pdf) [function.file-put-contents]: failed to open stream: No such file or directory in /home/username/public_html/subdir/apps/dompdf/filename.php on line 39. (Line 39 is the final line in my full script.) I don't know how to resolve this issue.
我正在尝试使用 dompdf 将表单保存为易于阅读的 .pdf 文件,我的处理脚本如下。我收到错误Warning: file_put_contents(/files/grantapps/NAME0.pdf) [function.file-put-contents]: failed to open stream: No such file or directory in /home/username/public_html/subdir/apps/dompdf/filename.php on line 39。(第 39 行是我完整脚本的最后一行。)我不知道如何解决这个问题。
allow_url_fopenis on, and I have tried all kinds of file paths, including the full directory (/home/username/public_html/subdir/files/grantapps/) and what's in the code below, but nothing worked.
allow_url_fopen已打开,并且我尝试了各种文件路径,包括完整目录 ( /home/username/public_html/subdir/files/grantapps/) 和下面代码中的内容,但没有任何效果。
require_once("dompdf_config.inc.php");
date_default_timezone_set('America/Detroit');
$html = 'code and whatnot is here';
$name = str_replace(" ", "", $_POST['form2name']);
$i = 0;
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
while(file_exists("files/grantapps/" . $name . $i . ".pdf")) {
$i++;
}
file_put_contents("files/grantapps/" . $name . $i . ".pdf", $dompdf->output());
回答by SomeoneYouDontKnow
There is definitly a problem with the destination folder path.
目标文件夹路径肯定有问题。
Your above error message says, it wants to put the contents to a file in the directory /files/grantapps/, which would be beyond your vhost, but somewhere in the system (see the leading absolute slash )
您上面的错误消息说,它想将内容放入目录中的文件中/files/grantapps/,这将超出您的vhost,但在系统中的某个地方(请参阅前导绝对斜杠)
You should double check:
你应该仔细检查:
- Is the directory
/home/username/public_html/files/grantapps/really present. - Contains your loop and your file_put_contents-Statement the absolute path
/home/username/public_html/files/grantapps/
- 目录是否
/home/username/public_html/files/grantapps/真的存在。 - 包含您的循环和您的 file_put_contents-Statement 绝对路径
/home/username/public_html/files/grantapps/
回答by Sachin
I was also stuck in the same kind of problem and i follow the simple step->
我也遇到了同样的问题,我按照简单的步骤操作->
just get the exact url of the file to which u want to copy Ex->
只需获取您要复制到的文件的确切网址 Ex->
http://www.test.com/test.txt (file to copy)
and pass the exact absolute folder path with filenamewhere do u want to write that file
并通过文件名传递确切的绝对文件夹路径,您要在哪里写入该文件
1->if u r on windows machine then
d:/xampp/htdocs/upload/test.txtand
1->如果你在 Windows 机器上然后
d:/xampp/htdocs/upload/test.txt和
2->if u r on linux machine then
/var/www/html/upload/test.txt
2->如果你在 linux 机器上,那么
/var/www/html/upload/test.txt
and u can get the docuement root by the PHP function->
你可以通过PHP函数获取文档根目录->
$_SERVER['DOCUMENT_ROOT']

