欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP Download Image Or File From URL

程序员文章站 2022-05-17 15:19:17
...
I’ll show you 3 functions that download a particular file (ex: image,video,zip,pdf,doc,xls,etc) from a remote resource (via a valid URL) then save to your server.

Depending on your current php.ini settings, some functions may not work; therefore, let try which function is best for you.

Note: please ensure the folder you want to store the downloaded file is existed and has write permission for everyone or the web process.

For all examples below, I assume that we’re going to download a remote image from: http://4rapiddev.com/wp-includes/images/logo.jpg and save it to a download sub folder with name: file.jpg.

1. PHP Download Remote File With file_get_contents and file_put_contents

Example:

2. PHP Download Remote File With CURL

Example:

3. PHP Download Remote File With fopen

Example:

Again, “download” folder must be exists and writable.

From: http://4rapiddev.com/php/download-image-or-file-from-url/