【问题标题】:how to save image from url using php and replace if already exist如何使用php从url保存图像并替换如果已经存在
【发布时间】:2015-08-11 02:07:55
【问题描述】:

我正在尝试从 dailymotion 中保存视频缩略图,我可以带来图像 URL,但我遇到的实际问题是通过重命名图像将该缩略图保存在我的服务器上。其次,如果服务器上已经存在该名称,我想检查该名称,它可以替换较旧的名称,此功能基本上用于更新视频 ID。

这是我获取dailymotion视频缩略图网址的php代码,但我不知道如何保存它

<?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
?>

例如这是缩略图网址:

anothersite.com/images/demo-image.jpg

如何在我的 URL 路径中保存图像:

mysite.com/video_thumbs/

并将 demo-image.jpg 重命名为 anyothername.jpg

然后检查和替换函数的最后一部分,如果 anyothername.jpg 已经存在,然后替换为旧的,这将用于更新 mysql

【问题讨论】:

  • 您已经在使用解决方案:file_get_contents(),然后您可以使用file_put_contents() 保存获取的数据,这会将数据放在您想要的任何位置,使用您想要的任何名称。跨度>

标签: php mysql image


【解决方案1】:

你的意思是这样的:

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

【讨论】:

  • file_put_contents 对我有用,但是如果已经存在,我如何检查更新然后替换它?
  • 每张图片都必须有一个名称,以便您以后识别(视频的 ID 或其他内容)。因此,当您使用代码时,它会检查该文件名是否已存在于您的服务器上。您还可以使用sha1_file() 函数在更新前检查它们是否是相同的图像。如果我对某些事情不清楚或者误解了您想要达到的目标,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-17
  • 2011-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多