Home > Backend Development > PHP Tutorial > ios interface, how to batch download multiple images in PHP?

ios interface, how to batch download multiple images in PHP?

WBOY
Release: 2016-09-21 14:12:54
Original
1087 people have browsed it

ios interface, batch download of multiple pictures, what ios passes to me is the ID of each picture, but my download can only be executed once, please solve (the ios interface will be used later, currently debugging on the web side)?
The code is as follows :
public function index(){

<code>$download =array(
        0=>3,
        1=>5,
    );
    foreach($download as $dow_v){
        //查找数据库中的图片信息
        $dow_arr = D('Upload') -> where(array('up_id' => $dow_v)) -> find();
        //文件名截取
        $file_name = substr($dow_arr['upload_url'], strrpos($dow_arr['upload_url'],'/')+1);</code>
Copy after login
Copy after login

//File path

<code>        $file_path=$dow_arr['upload_url'];
        $xinxi = $this->download($file_name,$file_path);
    }
}</code>
Copy after login
Copy after login

//How to download files

<code> public function download($file_name='',$file_path=''){
    //获取下载文件的大小
    $file_size=filesize($file_path);
    //             打开文件,并判断是否存在
    $fp = fopen($file_path,"r") or exit("文件不存在");
    //返回的文件     http协议信息
    header("Content-type:application/octet-stream");
    //按照字节大小返回
    header("Accept-Ranges:bytes");
    //返回文件大小
    header("Accept-Length:$file_size");
    //这里客户端的弹出对话框
    header("Content-Disposition:attachment;filename=".$file_name);
    //向客户端回送数据
    $buffer=1024;
    //为了下载的安全。我们最后做一个文件字节读取计数器
    $file_count=0;
        
    //判断文件是否结束
    while(!feof($fp)&&($file_size-$file_count>0)){
        $file_data=fread($fp,$buffer);
        //统计读了多少个字节
        $file_count+=$buffer;
        //把部分数据回送给浏览器
        echo $file_data;
    }
    fclose($fp);
}</code>
Copy after login
Copy after login

Reply content:

ios interface, batch download of multiple pictures, what ios passes to me is the ID of each picture, but my download can only be executed once, please solve (the ios interface will be used later, currently debugging on the web side)?
The code is as follows :
public function index(){

<code>$download =array(
        0=>3,
        1=>5,
    );
    foreach($download as $dow_v){
        //查找数据库中的图片信息
        $dow_arr = D('Upload') -> where(array('up_id' => $dow_v)) -> find();
        //文件名截取
        $file_name = substr($dow_arr['upload_url'], strrpos($dow_arr['upload_url'],'/')+1);</code>
Copy after login
Copy after login

//File path

<code>        $file_path=$dow_arr['upload_url'];
        $xinxi = $this->download($file_name,$file_path);
    }
}</code>
Copy after login
Copy after login

//How to download files

<code> public function download($file_name='',$file_path=''){
    //获取下载文件的大小
    $file_size=filesize($file_path);
    //             打开文件,并判断是否存在
    $fp = fopen($file_path,"r") or exit("文件不存在");
    //返回的文件     http协议信息
    header("Content-type:application/octet-stream");
    //按照字节大小返回
    header("Accept-Ranges:bytes");
    //返回文件大小
    header("Accept-Length:$file_size");
    //这里客户端的弹出对话框
    header("Content-Disposition:attachment;filename=".$file_name);
    //向客户端回送数据
    $buffer=1024;
    //为了下载的安全。我们最后做一个文件字节读取计数器
    $file_count=0;
        
    //判断文件是否结束
    while(!feof($fp)&&($file_size-$file_count>0)){
        $file_data=fread($fp,$buffer);
        //统计读了多少个字节
        $file_count+=$buffer;
        //把部分数据回送给浏览器
        echo $file_data;
    }
    fclose($fp);
}</code>
Copy after login
Copy after login

php only needs to return the path of the image to be downloaded to ios. I made a mistake before, because this is downloading the image through the client, so I only need to return the path and download the image on ios! ! ! ! (Complicating the problem)

When downloading, only one picture can be returned.
How many pictures and interfaces can be adjusted for ios?

Well, if there are multiple files, you can just loop through them. I feel like the issue isn't very clear.
I don’t understand, what do you mean you can only download it once?
Maybe what you want is this github.com/JeanWolf/httpclient
This library can achieve similar concurrent multi-request processing

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template