2014-03-01 20 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

相关问题