doufen3786 2015-08-10 09:48
浏览 23
已采纳

如何使用php从url保存图像,如果已经存在则替换

i am trying to save a video thumbnail from dailymotion i can bring the image URL but the actual problem i am getting is to save that thumbnail on my server by renaming image. Secondly i want to check that name if the name is already exist on the server it can replace the older one this function is basically used on updating video id.

Here is my php code to grab dailymotion video thumbnail url but i don't know to to save it

<?php
$related_dm_code = "x2rxn6i"; //Video Code
$thumbd_item='https://api.dailymotion.com/video/'.$related_dm_code.'?fields=thumbnail_240_url';
$json_thumbnail2_item = file_get_contents($thumbd_item);
$get_thumbnail2_item = json_decode($json_thumbnail2_item, TRUE);
$thumb2_item=$get_thumbnail2_item['thumbnail_240_url'];
echo $thumb2_item; //display thumbnail url
?>

e.g. here is the thumbnail url:

anothersite.com/images/demo-image.jpg

How can i save image in this path of my URL:

mysite.com/video_thumbs/

and renaming demo-image.jpg to anyothername.jpg

and then a last part of the function to check and replace if anyothername.jpg is already exist then replace with the older one this would be used to updating mysql

  • 写回答

1条回答 默认 最新

  • dongliaoqiang8524 2015-08-10 10:07
    关注

    You mean something like:

    if (!file_exists('http://www.example.com/video_thumbs/' . $file)) {
       $img = file_get_contents("http://www.anothersite.com/images/demo-image.jpg");
    
       file_put_contents('http://www.example.com/video_thumbs/' . $file, $img);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部