Skip to content
Fix Code Error

Saving image from PHP URL

March 13, 2021 by Code Error
Posted By: Anonymous

I need to save an image from a PHP URL to my PC.
Let’s say I have a page, http://example.com/image.php, holding a single “flower” image, nothing else. How can I save this image from the URL with a new name (using PHP)?

Solution

If you have allow_url_fopen set to true:

$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));

Else use cURL:

$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Answered By: Anonymous

Related Articles

  • Login to remote site with PHP cURL
  • Reading JSON POST using PHP
  • How can I send cookies using PHP curl in addition to…
  • How to get info on sent PHP curl request
  • Curl and PHP - how can I pass a json through curl by…
  • Send XML data to webservice using php curl
  • CURL and HTTPS, "Cannot resolve host"
  • How to include Authorization header in cURL POST…
  • Save image from url with curl PHP
  • Download a folder from azure blob storage which is…
  • PHP CURL DELETE request
  • Where should I place my Vaadin 10+ static files?
  • Downloading a large file using curl
  • Execute a PHP script from another PHP script
  • Getting HTTP code in PHP using curl
  • PHP cURL GET request and request's body
  • set cookie throw curl request
  • OPENSSL file_get_contents(): Failed to enable crypto
  • How can I find where I will be redirected using cURL?
  • Keeping session alive with Curl and PHP
  • In PHP how to send signed xml as soap request using curl
  • CURL ERROR: Recv failure: Connection reset by peer -…
  • Fatal error: Call to undefined function curl_init()
  • php: Get html source code with cURL
  • curl_exec() always returns false
  • How to catch curl errors in PHP
  • What is the incentive for curl to release the…
  • Call a REST API in PHP
  • Send file via cURL from form POST in PHP
  • PHP Curl And Cookies
  • convert php to python base64
  • How do I make a request using HTTP basic…
  • curl -GET and -X GET
  • How do I measure request and response times at once…
  • MatLab: Open JPEG binary data as Image
  • Get JSON object from URL
  • curl_init() function not working
  • Can I call curl_setopt with CURLOPT_HTTPHEADER…
  • How do I use arrays in cURL POST requests
  • SOAP request in PHP with CURL
  • Passing $_POST values with cURL
  • PHP CURL & HTTPS
  • file_get_contents() how to fix error "Failed to open…
  • Curl error 60, SSL certificate issue: self signed…
  • how to fix Invalid request (Unsupported SSL request)…
  • Linking or Routing dynamicaly with Next.js
  • Can PHP cURL retrieve response headers AND body in a…
  • file_get_contents(): SSL operation failed with code…
  • how to call url of any other website in php
  • How to use basic authorization in PHP curl
  • Curl not recognized as an internal or external…
  • PHP cURL HTTP CODE return 0
  • Vuetify v-tabs v-tab-item overflows window width
  • Sending array having space in keys being removed…
  • how to upload file using curl with PHP
  • Appending data to json file in C: append stops after…
  • PHP file_get_contents() returns "failed to open…
  • Error "Unexpected wire type" in a protobuf response in PHP
  • cURL error 60: SSL certificate: unable to get local…
  • How to parse JSON and access results
  • How can I overwrite file contents with new content in PHP?
  • PHP cURL, extract an XML response
  • How to echo xml file in php
  • Improve INSERT-per-second performance of SQLite
  • Perform curl request in javascript?
  • What is cURL in PHP?
  • HTTP Basic Authentication - what's the expected web…
  • curl posting with header application/x-www-form-urlencoded
  • How to use cURL to get jSON data and decode the data?
  • PHP + curl, HTTP POST sample code?
  • SQL Server - find nth occurrence in a string
  • PHP cURL Received HTTP code 407 from proxy after CONNECT
  • Calling other function in the same controller?
  • Get file content from URL?
  • How can I run multiple curl requests processed sequentially?
  • Show Curl POST Request Headers? Is there a way to do this?
  • Getting title and meta tags from external website
  • How to use Svelte store with tree-like nested object?
  • RAW POST using cURL in PHP
  • POST data to a URL in PHP
  • Simplest PHP example for retrieving user_timeline…
  • How to convert this curl command to JavaScript's fetch
  • Deploy did not succeed: Deploy directory 'out' does…
  • libcurl/C : when can I free header and postdata buffers?
  • "gatsby-source-airtable" threw an error while…
  • curl.h no such file or directory
  • update file based on a key in C
  • jQuery Mobile: document ready vs. page events
  • How to initalize a array of string from a file data
  • How do I get the HTML code of a web page in PHP?
  • PHP write file from input to txt
  • Correct way to set Bearer token with CURL
  • removing string array words from a txt file
  • How do I install cURL on Windows?
  • PHP CURL Enable Linux
  • cURL POST command line on WINDOWS RESTful service
  • Using curl POST with variables defined in bash…
  • Curl: Fix CURL (51) SSL error: no alternative…
  • How to insert data into elasticsearch
  • POST request with JSON body

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Redirect stderr and stdout in Bash

Next Post:

How can I run a program from a batch file without leaving the console open after the program starts?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error