doufen3786 2015-08-10 17: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 18: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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题