2014-03-01 19 views
0

如果文件已存在於服務器上,我想爲文件名添加後綴。重命名映像文件(如果服務器上已存在)PHP

例如:如果服務器上存在的文件image.jpg的新文件重命名爲image_1.jpg,image_2.jpg等

任何幫助,將不勝感激!

$max_file_size = 1024*100000; // 100000kb 
    $valid_exts = array('jpeg', 'jpg', 'png', 'gif'); 
    // thumbnail sizes 
    $sizes = array(250 => 150); 

    if (isset($_FILES["$fileime"])) { 
     if($_FILES["$fileime"]['size'] < $max_file_size){ 
     // get file extension 
     $ext = strtolower(pathinfo($_FILES["$fileime"]['name'], PATHINFO_EXTENSION)); 
     if (in_array($ext, $valid_exts)) { 
      /* resize image */ 
      foreach ($sizes as $width => $height) { 
      /* Get original image x y*/ 
        list($w, $h) = getimagesize($_FILES["$fileime"]["tmp_name"]); 
        /* calculate new image size with ratio */ 
        $ratio = max($width/$w, $height/$h); 
        $h = ceil($height/$ratio); 
        $x = ($w - $width/$ratio)/2; 
        $w = ceil($width/$ratio); 
        /* new file name */ 
        $path = '../thumbs/'.$_FILES["$fileime"]['name']; 
        /* read binary data from image file */ 
        $imgString = file_get_contents($_FILES["$fileime"]['tmp_name']); 
        /* create image from string */ 
        $image = imagecreatefromstring($imgString); 
        $tmp = imagecreatetruecolor($width, $height); 
        imagecopyresampled($tmp, $image, 
        0, 0, 
        $x, 0, 
        $width, $height, 
        $w, $h); 
+0

您有什麼具體問題嗎?你說你需要做什麼,但是爲了清晰起見,最好問題是什麼,或者你不清楚哪些問題,以便你能得到特定的幫助。 – Warlord

+0

我只需要任何幫助如何修改代碼,以便新上傳的文件不會覆蓋具有相同文件名的現有文件。 – WEBi

回答

0

爲什麼不只是在文件名的末尾添加時間戳呢?

$max_file_size = 1024*100000; // 100000kb 
    $valid_exts = array('jpeg', 'jpg', 'png', 'gif'); 
    // thumbnail sizes 
    $sizes = array(250 => 150); 

    if (isset($_FILES["$fileime"])) { 
     if($_FILES["$fileime"]['size'] < $max_file_size){ 
     // get file extension 
     $ext = strtolower(pathinfo($_FILES["$fileime"]['name'], PATHINFO_EXTENSION)); 
     if (in_array($ext, $valid_exts)) { 
      /* resize image */ 
      foreach ($sizes as $width => $height) { 
      /* Get original image x y*/ 
        list($w, $h) = getimagesize($_FILES["$fileime"]["tmp_name"]); 
        /* calculate new image size with ratio */ 
        $ratio = max($width/$w, $height/$h); 
        $h = ceil($height/$ratio); 
        $x = ($w - $width/$ratio)/2; 
        $w = ceil($width/$ratio); 
        /* new file name */ 
        $filename_array = explode('.', $_FILES["$fileime"]['name']); 
        $extension = array_pop($filename_array); 
        $new_name = implode('.',$filename_array).'_'.time().'.'.$extension; 
        $path = '../thumbs/'.$new_name; 
        /* read binary data from image file */ 
        $imgString = file_get_contents($_FILES["$fileime"]['tmp_name']); 
        /* create image from string */ 
        $image = imagecreatefromstring($imgString); 
        $tmp = imagecreatetruecolor($width, $height); 
        imagecopyresampled($tmp, $image, 
        0, 0, 
        $x, 0, 
        $width, $height, 
        $w, $h); 
+0

對不起,我是PHP初學者。我知道我應該使用$ timestamp = time();你會建議我在上面的代碼中實現它嗎? – WEBi

相關問題