繁体   English   中英

需要使用PHP将远程文件保存在磁盘上

[英]need to save a remote files on disk using php

嗨,我正在尝试使用php将文件保存在磁盘上,当我在“ Location”变量中复制文件的地址时,它起作用了,但是当我使用javascript函数传递值时,它却没有作用。 我想这个问题是由于白色间距或类似问题引起的,但不知道如何解决。

function upload(location){
if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("myOutput").innerHTML="done";
                }
            }
            xmlhttp.open("GET","SaveFiles.php?a="+location,true);
            xmlhttp.send();
    }  

   if(isset($_GET["a"]))
   {
     $Location = $_GET["a"];
     FileFunc($Location);
   }

 function FileFunc($Location){

     // $Location = "http://www.xxx.com/1.jpg"; <<When I uncomment this line it works
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $Location);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $myFile = curl_exec($ch);
     curl_close($ch);
     $file = 'myFile.jpg';
     file_put_contents($file, $myFile);
 }

您必须将服务器主机添加到卷曲地址

 function FileFunc($Location){

  $Location = $_SERVER['HTTP_HOST']."/".$Location;
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $Location);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $myFile = curl_exec($ch);
 curl_close($ch);
    $file = 'myFile.jpg';
 file_put_contents($file, $myFile);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM