Home >Backend Development >PHP Tutorial >How Can I Save Images from PHP URLs to My Local Computer?

How Can I Save Images from PHP URLs to My Local Computer?

DDD
DDDOriginal
2024-12-17 22:31:21120browse

How Can I Save Images from PHP URLs to My Local Computer?

Saving Images from PHP URLs

Saving images from PHP URLs to your local computer can be a useful task for various applications. Here's how to achieve this with PHP:

If your PHP configuration allows for fetching data from URLs, you can use the following code:

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

However, if file_get_contents is disabled due to security concerns, you can use cURL instead:

$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);

By using either of these methods, you can conveniently save images from remote PHP URLs to your local system.

The above is the detailed content of How Can I Save Images from PHP URLs to My Local Computer?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn