简体   繁体   English

file_put_contents不会保存文件

[英]file_put_contents won't save file

Hi I'm trying to use PHP file_put_contents to save a raster file to my local drive. 嗨,我正在尝试使用PHP file_put_contents将栅格文件保存到本地驱动器。 The raster file is acquired by curl request, the url link works perfectly fine if open with browser(file is downloadable), but the file_put_contents won't save anything to the folder. 栅格文件是通过curl请求获取的,如果使用浏览器打开(可下载文件),则url链接可以很好地工作,但是file_put_contents不会将任何内容保存到该文件夹​​中。 Below is my php pseudo code: 以下是我的php伪代码:

$ch = curl_init(working_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$save = curl_exec($ch); 
curl_close($ch);
file_put_contents($path_of_file, $save);

I have chmod to 777 and have the full write and read to the folder and the save path is correct. 我的chmod为777,并且具有完整的写入和读取到文件夹的权限,并且保存路径正确。

So I have figured out the problem, it was a path issue; 因此,我已经解决了这个问题,这是一个路径问题。 I'm using Javascript in my index.html to ajax information to my php file; 我在index.html中使用Javascript将信息吸收到我的php文件中; executed curl in php file, download data and return the location path back to index.html in JSON data stream. 在php文件中执行curl,下载数据并在JSON数据流中将位置路径返回给index.html。 Because my data structure, the path returned back to my index.html is not the right path(I used "../../filesave". Thanks everyone for helping! 因为我是数据结构,所以返回到index.html的路径不是正确的路径(我使用了“ ../../filesave”。感谢大家的帮助!

In the future, if anyone had similar issue, be sure to check the save path first and also check to see if have the write and read permission to the workspace. 将来,如果有人遇到类似问题,请确保首先检查保存路径,并检查对工作区是否具有写入和读取权限。

Try this: 尝试这个:

$save = file_get_contents($working_url);
file_put_contents($path_of_file, $save);

And please check: 并且请检查:

  • you can write the destination folder 你可以写目标文件夹
  • you have the right content in $save variable 您在$ save变量中具有正确的内容

Maybe provide us the URL to check it. 也许提供给我们URL进行检查。

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

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