Php 从URL复制图像,更改名称并保存到文件夹

Php 从URL复制图像,更改名称并保存到文件夹,php,Php,我即将开始一个PHP脚本来导入csv数据库 csv有一个列,其中包含指向产品图像的URL 我需要能够做的是获取图像,检查它是什么类型的文件(jpg、png等),更改名称,将文件保存到服务器上的文件夹中,然后将文件名插入数据库 我能做的就是在数据库中插入一位,这就是我所困惑的文件的重命名 是否可以像上载文件时那样获取信息,例如: 使用html表单中的文件输入上载文件 $_FILES['file']['name']; or $_FILES['file']['type']; 如果下载一个文件,这可能

我即将开始一个PHP脚本来导入csv数据库

csv有一个列,其中包含指向产品图像的URL

我需要能够做的是获取图像,检查它是什么类型的文件(jpg、png等),更改名称,将文件保存到服务器上的文件夹中,然后将文件名插入数据库

我能做的就是在数据库中插入一位,这就是我所困惑的文件的重命名

是否可以像上载文件时那样获取信息,例如:

使用html表单中的文件输入上载文件

$_FILES['file']['name'];
or
$_FILES['file']['type'];
如果下载一个文件,这可能吗

$downloaded_image['name'];
or
$downloaded_image['type'];
或者这完全不符合标准

我以前从来没有这样做过,关于stackoverflow的大多数答案都不能完全回答我的问题,所以我希望有人能为我指出正确的方向

编辑/更新:

是否需要此工作来获取文件属性

$image_id = '123456';
$the_image = file_get_contents($downloaded_image);
$image_name = $the_image['name'];
$image_type = $the_image['type'];

$new_name = $image_id . '.' . $image_type;

$img_path = '/images/';
$save_image = file_put_contents($img_path, $new_name);
if($save_image) {
echo 'image saved';
} else {
echo 'Not Saved';
}
希望我有点道理

更新:这是原样的脚本(仍然需要整理)


通过使用
file\u get\u contents()
检索文件,您将不会获得任何关于其格式的特别有用的信息。它不携带类似于
$\u文件
中用于上传的元数据

如果图像URL预期为带扩展名的完整文件名,并且您相信扩展名是正确的,则可以将其用作您的类型。但是,使用
FILEINFO\u MIME\u TYPE
选项将探测文件以返回其MIME类型,如
image/jpeg
image/png
中所示

因此,您的工作流程将是:

  • 使用
    file\u get\u contents()检索图像
  • 使用新名称将其保存到本地文件系统
  • 调用
    finfo_file()
    检索其mime类型
  • 使用所需的详细信息更新数据库
  • 例如:

    // Assume this URL for $download_image from your CSV
    $download_image = 'http://example.com/images/img1.jpg';
    $image_id = 12345;
    
    // Store the original filename
    $original_name = basename($download_image); // "img1.jpg"
    // Original extension by string manipulation
    $original_extension = substr($original_name, strrpos($original_name, '.')); // ".jpg"
    
    // An array to match mime types from finfo_file() with extensions
    // Use of finfo_file() is recommended if you can't trust the input
    // filename's extension
    $types = array(
      'image/jpeg' => '.jpg',
      'image/png' => '.png',
      'image/gif' => '.gif'
      // Other types as needed...
    );
    
    // Get the file and save it
    $img = file_get_contents($download_image);
    $stored_name = 'images/' . $image_id . $original_extension;
    if ($img) {
      file_put_contents($stored_name, $img);
    
      // Get the filesize if needed
      $size = filesize($stored_name);
    
      // If you don't care about validating the mime type, skip all of this...
      // Check the file information
      $finfo = finfo_open(FILEINFO_MIME_TYPE);
      $mimetype = finfo_file($finfo, $stored_name);
    
      // Lookup the type in your array to get the extension
      if (isset($types[$mimetype])) {
        // if the reported type doesn't match the original extension, rename the file
        if ($types[$mimetype] != $original_extension) {
          rename($stored_name, 'images/' . $image_id . $types[$mimetype]);
        }
      }
      else {
        // unknown type, handle accordingly...
      }
      finfo_close($finfo);
    
      // Now save all the extra info you retrieved into your database however you normally would
      // $mimetype, $original_name, $original_extension, $filesize
    }
    else {
      // Error, couldn't get file
    }
    
    如果要从已有的扩展名中获取mimetype字符串,并且不使用finfo验证该类型,可以翻转
    $types
    以使用值交换键

    if (in_array($original_extension), $types) {
      $mimetype = array_flip($types)[$original_extension];
    }
    

    通过使用
    file\u get\u contents()
    检索文件,您将不会获得任何关于其格式的特别有用的信息。它不携带类似于
    $\u文件
    中用于上传的元数据

    如果图像URL预期为带扩展名的完整文件名,并且您相信扩展名是正确的,则可以将其用作您的类型。但是,使用
    FILEINFO\u MIME\u TYPE
    选项将探测文件以返回其MIME类型,如
    image/jpeg
    image/png
    中所示

    因此,您的工作流程将是:

  • 使用
    file\u get\u contents()检索图像
  • 使用新名称将其保存到本地文件系统
  • 调用
    finfo_file()
    检索其mime类型
  • 使用所需的详细信息更新数据库
  • 例如:

    // Assume this URL for $download_image from your CSV
    $download_image = 'http://example.com/images/img1.jpg';
    $image_id = 12345;
    
    // Store the original filename
    $original_name = basename($download_image); // "img1.jpg"
    // Original extension by string manipulation
    $original_extension = substr($original_name, strrpos($original_name, '.')); // ".jpg"
    
    // An array to match mime types from finfo_file() with extensions
    // Use of finfo_file() is recommended if you can't trust the input
    // filename's extension
    $types = array(
      'image/jpeg' => '.jpg',
      'image/png' => '.png',
      'image/gif' => '.gif'
      // Other types as needed...
    );
    
    // Get the file and save it
    $img = file_get_contents($download_image);
    $stored_name = 'images/' . $image_id . $original_extension;
    if ($img) {
      file_put_contents($stored_name, $img);
    
      // Get the filesize if needed
      $size = filesize($stored_name);
    
      // If you don't care about validating the mime type, skip all of this...
      // Check the file information
      $finfo = finfo_open(FILEINFO_MIME_TYPE);
      $mimetype = finfo_file($finfo, $stored_name);
    
      // Lookup the type in your array to get the extension
      if (isset($types[$mimetype])) {
        // if the reported type doesn't match the original extension, rename the file
        if ($types[$mimetype] != $original_extension) {
          rename($stored_name, 'images/' . $image_id . $types[$mimetype]);
        }
      }
      else {
        // unknown type, handle accordingly...
      }
      finfo_close($finfo);
    
      // Now save all the extra info you retrieved into your database however you normally would
      // $mimetype, $original_name, $original_extension, $filesize
    }
    else {
      // Error, couldn't get file
    }
    
    如果要从已有的扩展名中获取mimetype字符串,并且不使用finfo验证该类型,可以翻转
    $types
    以使用值交换键

    if (in_array($original_extension), $types) {
      $mimetype = array_flip($types)[$original_extension];
    }
    

    这取决于开放URL的php配置。如果允许,那么您可以使用$fileContents=file\u get\u contents(此处的图像\u url\u);可以从文件扩展名获取的类型。否则,您应该使用curl获取外部contentNo。file\u get\u contents获取文件内容。文件名位于您打开的url中。您可以从文件扩展名或使用fileinfo功能获得的文件类型。在您建议的代码中,
    $download\u image
    来自哪里?这是您提供给用户下载的文件系统上的映像,还是您从其他位置检索(下载)并保存的映像?我想我之前误解了你的意图。“csv有一个包含产品图片URL的专栏”我现在明白了。您正在检索图像、收集信息并保存它们。完整文件名-(或png)这取决于打开URL的php配置。如果允许,那么您可以使用$fileContents=file\u get\u contents(此处的图像\u url\u);可以从文件扩展名获取的类型。否则,您应该使用curl获取外部contentNo。file\u get\u contents获取文件内容。文件名位于您打开的url中。您可以从文件扩展名或使用fileinfo功能获得的文件类型。在您建议的代码中,
    $download\u image
    来自哪里?这是您提供给用户下载的文件系统上的映像,还是您从其他位置检索(下载)并保存的映像?我想我之前误解了你的意图。“csv有一个包含产品图片URL的专栏”我现在明白了。你正在检索图像、收集信息并保存它们。完整文件名-(或png)是的,凌晨3点刚过,我已经准备好睡觉了,在孩子们早上把我从床上拖起来之前,我要睡4个小时。英雄联盟迈克尔,你已经超越了一切。我只是在寻找指针,但相反,我得到了开始脚本这一部分所需的几乎所有东西。非常感谢你的帮助。非常感谢!嘿@MichaelBerkowski,我尝试了上面的方法,对我的测试脚本做了一些修改。。我得到了错误:警告:file\u get\u contents():无法打开流:HTTP请求失败!在第34行的E:\xampp\htdocs\aff\admin\import\u products.php中找不到HTTP/1.0 404-第34行是
    $img=file\u get\u contents($download\u image)在原始问题中添加了上面的测试脚本代码。@EoinH这是一个404,这意味着代码可以工作,但找不到图像。现在,我可以通过
    file\u get\u contents('http://www.partydomain.co.uk/images/P/21478.jpg)
    这就引起了对服务器的怀疑——你是否从它们身上抓取了太多图像,以至于它们会阻止你的IP并开始返回404?这是我第一次运行脚本,并且没有从中丢弃任何东西