Table of Contents
Three ways to download remote files in PHP from the perspective of performance,
Articles you may be interested in:
Home Backend Development PHP Tutorial 3 ways to download remote files in PHP from the perspective of performance, _PHP tutorial

3 ways to download remote files in PHP from the perspective of performance, _PHP tutorial

Jul 12, 2016 am 09:01 AM
php download document Remotely

Three ways to download remote files in PHP from the perspective of performance,

When exporting Excel today, I always have to test the exported Excel file and download and open it frequently , it’s very troublesome, just think of writing a piece of code to do it in one go. Export Excel on the server ==> Download the Excel file to the local ==> and open it.

Here is the PHP solution for downloading remote files as a reminder. The third method takes into account performance issues when the file is too large.

3 options:

-rw-rw-r-- 1 liuyuan liuyuan 470 Feb 20 18:12 test1_fopen.php
-rw-rw-r-- 1 liuyuan liuyuan 541 Feb 20 18:06 test2_curl.php
-rw-rw-r-- 1 liuyuan liuyuan 547 Feb 20 18:12 test3_curl_better.php

Option 1, suitable for small files

Directly use fopen()/file_get_contents() to obtain the file stream and use file_put_contents() to write

<&#63;php
  //an example xls file form baidu wenku
  $url = 'http://bs.baidu.com/wenku4/%2Fe43e6732eba84a316af36c5c67a7c6d6&#63;sign=MBOT:y1jXjmMD4FchJHFHIGN4z:lfZAx1Nrf44aCyD6tJqJ2FhosLY%3D&time=1392893977&response-content-disposition=attachment;%20filename=%22php%BA%AF%CA%FD.xls%22&response-content-type=application%2foctet-stream';
  $fp_input = fopen($url, 'r');
  file_put_contents('./test.xls', $fp_input);
  exec("libreoffice ./test.xls", $out, $status);
&#63;>
Copy after login

Option 2: Get content through Curl

<&#63;php
  //an example xls file form baidu wenku
  $url = 'http://bs.baidu.com/wenku4/%2Fe43e6732eba84a316af36c5c67a7c6d6&#63;sign=MBOT:y1jXjmMD4FchJHFHIGN4z:lfZAx1Nrf44aCyD6tJqJ2FhosLY%3D&time=1392893977&response-content-disposition=attachment;%20filename=%22php%BA%AF%CA%FD.xls%22&response-content-type=application%2foctet-stream';
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  file_put_contents('./test.xls', curl_exec($ch));
  curl_close($ch);
  exec("libreoffice ./test.xls", $out, $status);
&#63;>
Copy after login

There is a problem with the first and second solutions, that is, the file will be read into the memory before being written to the local disk. When the file is large, it may exceed the memory and crash

Even if your memory is set to be large enough, this is unnecessary overhead

The solution is: directly give CURL a writable file stream to let it solve this problem by itself (via the CURLOPT_FILE option), so you must first create a file pointer for it.

<&#63;php
  //an example xls file form baidu wenku
  $url = 'http://bs.baidu.com/wenku4/%2Fe43e6732eba84a316af36c5c67a7c6d6&#63;sign=MBOT:y1jXjmMD4FchJHFHIGN4z:lfZAx1Nrf44aCyD6tJqJ2FhosLY%3D&time=1392893977&response-content-disposition=attachment;%20filename=%22php%BA%AF%CA%FD.xls%22&response-content-type=application%2foctet-stream';
  $fp_output = fopen('./test.xls', 'w');
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_FILE, $fp_output);
  curl_exec($ch);
  curl_close($ch);
  exec("libreoffice ./test.xls", $out, $status);
&#63;> 
Copy after login

The above content introduces you to 3 methods of downloading remote files in PHP from the perspective of performance. I hope you like it.

Articles you may be interested in:

  • php download remote file class (supports breakpoint resume upload)
  • php has password function and downloads remote files to save local specified directory modifications Enhanced version
  • High-performance PHP framework Symfony2 classic introductory tutorial
  • Comparative analysis of file_get_contents and curl performance in PHP
  • How to download remote files to local storage in PHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1086658.htmlTechArticleConsider 3 ways to download remote files in PHP from the perspective of performance. When exporting Excel today, you always have to Testing the exported Excel file, downloading and opening it frequently, it is very troublesome just thinking about writing a paragraph...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles