【问题标题】:php Copying images from one directory to another [duplicate]php将图像从一个目录复制到另一个[重复]
【发布时间】:2022-02-13 03:29:14
【问题描述】:

我尝试将一个目录映像复制到另一个目录,但它不适合我。

这是我的代码:

<?php

$old_sub_dir = '/media/import';

$new_sub_dir = '/media/catalog/product/image';

//get directory contents
$contents = array();

$dir = opendir($_SERVER['DOCUMENT_ROOT'] . $old_sub_dir);

while (false !== ($file = readdir($dir))) {
       $contents[] = $file;
}
closedir($dir);

//get only jpeg contents
$jpeg_contents = array();

foreach($contents as $file){
    if (eregi('.jpg{1}$', $file)){
        $jpeg_contents[] = $file;
    }
}

// copy each jpeg from directory 'a' to directory 'b'
foreach($jpeg_contents as $file){
    copy($_SERVER['DOCUMENT_ROOT'] . $old_sub_dir . '/' . $file, $_SERVER['DOCUMENT_ROOT'] . $new_sub_dir . '/' . $file);
}

?>

我得到的错误是:

Uncaught Error: Call to undefined function eregi()

任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 当你说它不工作时——发生了什么,如果有错误——请将它们包括在你的问题中。
  • @NigelRen 我尝试了上面的代码,但没有从这个目录复制任何图像
  • 在处理过程中,您使用要复制的文件构建了一些数组。您是否检查过这些数组是否包含您期望的文件列表?(您也可以一次性完成所有阶段,因此不确定您使用 3 个单独的循环来处理它们)。
  • 我不知道如何使用它,但我尝试了数组打印,但数组为空白
  • @NigelRen 错误是Uncaught Error: Call to undefined function eregi()

标签: php image


【解决方案1】:

eregi() 是 PHP 5.3 版本中弃用的函数。改用 preg_match() 函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 2012-02-15
    • 2014-07-18
    • 2013-06-01
    • 2020-06-22
    • 2014-08-12
    相关资源
    最近更新 更多