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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源