簡體   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