PHP:在上传之前重命名多个图像

时间:2016-11-26 09:02:28

标签: php

是他们修改此代码的任何方式

<?php
$desired_dir= ($_SERVER['DOCUMENT_ROOT']).'/cert/uploads/';
    if(isset($_FILES['files'])){
        $cat_name='image/*';
        if($cat_name==""){
            echo "Category Required";
            /*header('Refresh: 1;url=addfile.php');*/
        }
        else{
            $count=0;

            foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
                $file_name = $key.$_FILES['files']['name'][$key];
                $size =$_FILES['files']['size'][$key];
                $file_f = $size / 1024;
                $file_size =round($file_f);
                $file_tmp =$_FILES['files']['tmp_name'][$key];
                $file_type=$_FILES['files']['type'][$key];
                $path="uploads/";


                if($size==0){
                    echo "<h6 style='color:red'>$file_name Exeeds upload limit</h6>";
                }
                else{


                    if (file_exists("$desired_dir" . $file_name))
                    {
                        echo "<h6 style='color:red'>".$file_name . " already exists.</h6>";
                    }
                    else
                    {
                        $query="INSERT into tblphotos VALUES('','$file_name')";
                        if(mysqli_query($con, $query)){                  
                            move_uploaded_file($file_tmp,"$desired_dir".$file_name);
                            //echo "<p style='color:blue'>$file_name Uploaded</p";
                            $count=$count+1;
                        }
                        else{
                            echo "Error in adding Files";
                        }
                    }
                }
            }
            echo "<h6 style='color:blue'>"."$count Files Uploaded<h6>";
            /*header('Refresh: 2;url=addfile.php');*/
        }
    }
    ?>

更改我上传的每张图片的名称。例如,我的s​​cipt中有一个$id = 1 我想将id和数组的组合作为其名称,就像下面的例子一样。

1_[0].jpg, 1_[1].jpg, 1_[2].jpg

谢谢

1 个答案:

答案 0 :(得分:1)

如果你已经有$ id = 1 然后试试这个:

$file_name = $id.'['. $key.$_FILES['files']['name'][$key] . ']';