php - 为什么无法将网页保存到我的/var/www目录中?

标签 php linux file file-get-contents document-root

文档根目录是/var/www,我想把网页下载到/var/www/php.html

<?php
$url = "http://php.net";
$html_content = file_get_contents($url);
$fp = fopen("php.html","a");
fwrite($fp,$html_content);
fclose($fp);
echo $html_content;
?>

我可以在 Firefox 中获取网页,但是我的 php.html 文件在哪里?

最佳答案

您的页面位于 PHP 脚本的同一文件夹中。如果您想访问该页面,您应该使用 http://path_to_your_script/pythons.html

要保存在/var/www 中,您应该使用:

<?php
$url = "http://localhost/postgres";
$html_content = file_get_contents($url);
$fp = fopen("/var/www/php.html","a"); // ADD /var/www to the path
fwrite($fp,$html_content);
fclose($fp);
echo $html_content;
?>

现在您可以通过 http://localhost/php.html 访问

如果无法保存到/var/www/是因为没有权限,如果有root权限可以在控制台执行:sudo chmod 777/var/www/

关于php - 为什么无法将网页保存到我的/var/www目录中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29058151/

相关文章:

php - 不工作重复 Paypal 沙箱

php - SQL 搜索字符串中的 # 字符问题

mysql - 在 linux 文件系统上索引文件的 Ruby 进程

windows - Mod_rewrite 在 Windows 和 Ubuntu 上有不同的结果

python - readlines() 在行的末尾没有 EOL

ruby - 使用 Faraday Ruby gem 下载图像并写入磁盘

php - 在 CakePHP 3 中使用 IFNULL ... GROUP_CONCAT()

php - 如何使用 PHP 获取 MySQL 的 'ft_min_word_len' 配置变量的值?

linux - 在 Ubuntu 终端中禁用自动换行

Bash 脚本 : How can I patch files?(在文件的给定位置写入给定的字符串)