【问题标题】:PHP image copying files to another directory and changing their extensionPHP图像将文件复制到另一个目录并更改其扩展名
【发布时间】:2013-10-14 16:55:52
【问题描述】:

我有以下 PHP 代码将文件从一个文件夹复制到另一个文件夹,即从源到目标。

//Copy files to new destination
$img_input = "temp_images/".$foldername."/input";
$img_org = "temp_images/".$foldername."/output/".$foldername."/originals";
$img_scan = glob("$img_input/*.*");

foreach($img_scan as $img_scans){
    $img_scans_to_go = str_replace($img_input,$img_org,$img_scans);
    copy($img_scans, $img_scans_to_go);
}

$img_input 包含混合格式的图像。现在我希望目标文件$img_org 具有.jpg 图像格式。我该怎么做?

我尝试将$img_scan = glob("$img_input/*.*"); 更改为$img_scan = glob("$img_input/*.jpg");,但只会复制 jpg 文件。其他文件不会复制。

【问题讨论】:

    标签: php file-copying


    【解决方案1】:

    如果您想使用 PHP 将图像从其他文件类型转换为 JPG,请查看其中的一些: PHP's GD lib - will let you open various file types and save as JPEG

    现有的堆栈帖子都在附近,比如这里:How to convert all images to JPG format in PHP?

    【讨论】:

    • 谢谢。在这种情况下,我更喜欢只更改扩展名。
    • 真的只是改变文件的扩展名?您的特权...查找 PHP 的 rename() 函数并将其放在您的 copy() 函数之后:php.net/manual/en/function.rename.php(PHP 手册是您最好的伙伴)
    • 我试过rename ($img_org .'/*.*',$img_org .'/*.jpg');,但没用。有什么帮助吗?
    • 我认为rename 接受的文件不超过一个像glob 这样的文件...在您的copy() 行之后,输入如下内容:rename($img_scans_to_go, $img_scans_to_go . '.jpg'); - 这应该很快以及将.jpg 放在已复制 文件的末尾 的令人讨厌的工作
    • 他们的名字真的很讨厌,比如picture01.jpeg.jpg、picture02.gif.jpg、picture05.jpg.jpg
    猜你喜欢
    • 2015-06-12
    • 2018-01-08
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    相关资源
    最近更新 更多