简体   繁体   English

从URL,PHP下载到服务器中的文件夹

[英]Download to folder in server from url, PHP

I have in my database several image links in an array. 我的数据库中有多个数组中的图像链接。 I want to download these to a folder in my server renaming the images to 0.jpeg, 1.jpeg, 2.jpeg and so on. 我想将它们下载到服务器中的文件夹中,将图像重命名为0.jpeg,1.jpeg,2.jpeg等。 I only managed to insert 1 with the name of 0.jpeg. 我只设法插入名称为0.jpeg的1。

$result2 = $d->query("SELECT photos AS myfoto From table where id =  15");  

while ($row2 = $result2->fetch()){
    $arrays = $row2["myfoto"];
    foreach(json_decode($arrays) AS $url){
        //prints correctly all files 
        echo $url;
        file_put_contents('/home/my/public_html/place/fotos/[0].jpeg', 
        file_get_contents($url));
    }
 }

You need to use some kind of index that increases, and then can be used in the filename 您需要使用某种增加的索引,然后可以在文件名中使用

Easiest way with your current code is to use the index of the array of the results: 当前代码最简单的方法是使用结果数组的索引:

$result2 = $d->query("SELECT photos AS myfoto From table where id =  15");  

while ($row2 = $result2->fetch()){
    $arrays = $row2["myfoto"];
    foreach(json_decode($arrays) AS $key => $url){
        //prints correctly all files 
        echo $url;
        file_put_contents('/home/my/public_html/place/fotos/'. $key . '.jpeg', file_get_contents($url));
    }
}

My preferred code would be: 我的首选代码是:

$result2 = $d->query("SELECT photos AS myfoto From table where id =  15");  

while ($row2 = $result2->fetch()){
    $arrays = json_decode($row2["myfoto"]);
    foreach($arrays AS $key => $url){
        //prints correctly all files 
        echo $url;
        file_put_contents('/home/my/public_html/place/fotos/'. $key . '.jpeg', file_get_contents($url));
    }
}

not too fond of functions in a foreach-loop definition 不太喜欢foreach循环定义中的函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM