繁体   English   中英

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

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

我的数据库中有多个数组中的图像链接。 我想将它们下载到服务器中的文件夹中,将图像重命名为0.jpeg,1.jpeg,2.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));
    }
 }

您需要使用某种增加的索引,然后可以在文件名中使用

当前代码最简单的方法是使用结果数组的索引:

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

我的首选代码是:

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

不太喜欢foreach循环定义中的函数

暂无
暂无

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

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