简体   繁体   English

带有URL的file_get_contents

[英]file_get_contents with URL

I am using Parse and trying to get the image from a URL and save it as a ParseFile 我正在使用Parse并尝试从URL获取图像并将其另存为ParseFile

I was successfully able to save a locally uploaded image as a ParseFile . 我成功地将本地上传的图像另存为ParseFile How can I do the same with an image URL? 如何处理图片网址?

echo $_POST['imageURL']; // url to image
// successfully saves uploaded image
// $file = ParseFile::createFromData( file_get_contents( $_FILES['image']['tmp_name'] ), $_FILES['image']['name']  );

// trying to save image from URL
$file = ParseFile::createFromData( file_get_contents( $_POST['imageURL'] ), $_FILES['image']['name']  );

$file->save();

This is the error I got: 这是我得到的错误:

https://www.webniraj.com/wp-content/uploads/2012/05/WNI01_14-Logo-275x63.png
Fatal error: Call to a member function save() on a non-object in /var/www/html/quickupload/upload.php on line 96

Try this function 试试这个功能

function get_url_content($URL){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_URL, $URL);
          $data = curl_exec($ch);
          curl_close($ch);
          return $data;
      }

$file =  get_url_content('http://example.com/logo.jpg');

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

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