Hi I am trying to save a file on disk using php, when I copy the address of the file in the "Location" variable it works but when I pass the value using javascript function it does not. I suppose the problem is because of whitespacing or similar issues but do not know how to solve it.
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);
}
you must add server host to your curl address
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.