倍数上传重命名(功能)

时间:2015-09-25 03:13:14

标签: php

好的,下面的php上传脚本已经工作了,部分没有工作只是重命名文件(如果存在)。

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){

foreach ($_FILES['files']['name'] as $f => $img_name) {     
    if ($_FILES['files']['error'][$f] == 4) {
        continue;
    }
    if ($_FILES['files']['error'][$f] == 0) {              
        if ($_FILES['files']['size'][$f] > $max_file_size) {
            $message[] = "$img_name est trop lourde !";
            continue;
        }
        elseif( ! in_array(pathinfo($img_name, PATHINFO_EXTENSION), $valid_formats) ){
            $message[] = "$img_name est pas valide !";
            continue;
        }
        else{
            if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$img_name)) {
                while(file_exists($path . $img_name)){
                    $increment++;
                    $img_name = $name.$increment.'.'.$extension;    
                    $count++;
                }
            }
        }
    }
  }
}

我已经在php doc上搜索了很多东西,尝试了很少的方法但是......当我尝试使用之前已经上传过的名字的文件时,它并没有改变实际的上传文件。 / p>

1 个答案:

答案 0 :(得分:0)

您在上传文件后确定文件名。之前确定它。 改变如:

        while(file_exists($path . $img_name)){
            $increment++;
            $img_name = $name.$increment.'.'.$extension;    
            $count++;
        }
        if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$img_name)) {
            // File Uploaded !!!
        }

修改
我已经改变了你的代码。代码中的评论。

$valid_formats = array("jpg", "JPG", "png", "PNG" , "bmp", "BMP");
$max_file_size = 1024*6000; //60 000 kb - 6 mb
$path = "../../../img/final/img_recipes/"; //directory
$count = 0;
$uploaded_image_names = array(); //create a new array

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    foreach ($_FILES['files']['name'] as $f => $img_name) {     
        if ($_FILES['files']['error'][$f] == 4) {
            continue;
        }
        if ($_FILES['files']['error'][$f] == 0) {

            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$img_name est trop lourde !";
                continue;
            }
            elseif( ! in_array(pathinfo($img_name, PATHINFO_EXTENSION),     $valid_formats) ){
                $message[] = "$img_name est pas valide !";
                continue;
            }
            else{
                // Moved name and extension initialization to here.
                // Here is where you want to determine the actual filename
                $name = pathinfo($img_name, PATHINFO_FILENAME);
                $extension = pathinfo($img_name, PATHINFO_EXTENSION);
                $increment = 0;
                while(file_exists($path . $img_name)){
                    $img_name = $name.$increment.'.'.$extension;
                    $increment++;
                }
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$img_name)) { 
                        $count++;
                        //Store the uploaded filenames to array here
                        $uploaded_image_names[] = $path.$img_name;
                }
            }
        }
    }
}
foreach ($uploaded_image_names as $uploaded_image_name){
    //store the $uploaded_image_name to db
}

注意:我没有对此进行测试,因为我现在没有PHP可用。