2017-10-06 41 views
0

我有一個名爲xyz的圖像。除了這個名爲xyz的文件有未知的擴展名,即jpg,jpeg,png,gif等。我想將這個文件從一個名爲advertisers/images的文件夾複製到我的網站cpanl中的其他文件夾發行商/圖像。如何用php做到這一點。提前致謝。如何將一個圖像從一個文件夾複製到其他文件夾中php

+3

[將文件複製到另一個文件夾PHP(可能的重複https://stackoverflow.com/questions/21526046/copy-files-到另一個文件夾-php) – DJSweetness

回答

0

可以使用copy功能:

$srcfile = 'source_path/xyz.jpg'; 
$dstfile = 'destination_path/xyz.jpg'; 
copy($srcfile, $dstfile); 
0

使用複印()函數

copy('advertisers/images/xyz.png','publishers/ 
images/xyz.png'); 

更改文件擴展名,不管它是什麼。

如果您不知道擴展名,請使用通配符。它將爲您提供與通配符匹配的所有文件的數組。

$files = glob('advertisers/images/xyz.*'); 
    foreach ($files as $file) { 
    copy($file,'publishers/images/'.$file); 
} 
+0

sir文件可能會有所不同,我想複製一個圖像文件xyz及其擴展名,無論它是jpg,jpeg,JPG,JPEG,PNG,GIF,GIF等格式的擴展名。 – John

+0

@John使用。*代​​替延長。它會複製所有名稱爲xyz –

+0

的文件,我使用了$ imagePath =「/home/public_html/advertisers/images/1adfd189e18d6d101a29cfd09b0426a4.*」;但圖像未被複制 – John

0

您應該下同編寫代碼:

<?php 
$imagePath = "../Images/somepic.jpg"; 
$newPath = "../Uploads/"; 
$ext = '.jpg'; 
$newName = $newPath."a".$ext; 

$copied = copy($imagePath , $newName); 

if ((!$copied)) 
{ 
    echo "Error : Not Copied"; 
} 
else 
{ 
    echo "Copied Successful"; 
} 
?> 
相關問題