$url="http://www.source.com/top";
$destination=fopen("/var/www/vhosts/domain.com/httpdocs/temp/" . date('m-d-Y'),"w");
echo "dest=$destination<br>";
echo "url=$url<br>";
$source=fopen($url,"r");
$maxsize=5000000000;
$length=0;
while (($a=fread($source,1024))&&($length<$maxsize))
{
$tmpfile=$tmpfile . $a;
$length=$length+1024;
}
fwrite($destination,$tmpfile);
fclose($source);
fclose($destination);
以上 PHP 源代码在我的共享托管帐户上就像魅力一样。但是,它无法在我的专用 Linux Centos 机器上写入文件。在这台 Centos 机器中,源 $url 可以正常读取,但是这一行:
$destination=fopen("/var/www/vhosts/domain.com/httpdocs/temp/" . date('m-d-Y'),"w");
在 Linux 系统中写入文件失败。我尝试在 linux root 用户下运行上面的代码(例如 php file-name.php)并能够创建文件,但无法读取源文件:
$destination=fopen("/var/www/vhosts/domain.com/httpdocs/temp/" . date('m-d-Y'),"w");
我收到 403 错误。我对这台 Linux Centos 机器发生的事情感到非常困惑。就像我之前在 Centos 盒子中发布的那样,我遇到了 Session variables return empty 的问题。同一页面的提交之间。如有任何帮助,我们将不胜感激。
最佳答案
为什么不简单地做呢?:
<?php
error_reporting(E_ALL);/*Debug any permission problems*/
$url="http://www.source.com/top";
$temp="/var/www/vhosts/domain.com/httpdocs/temp/".date('m-d-Y').".tmp";
file_put_contents($temp,file_get_contents($url));
?>
关于PHP fopen($url,w),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9388411/