dtc88867
2016-11-26 09:02 阅读 69
已采纳

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

is their any way to modify this code

<?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');*/
        }
    }
    ?>

to change the name of every image i upload. for example i have a $id = 1 in my scipt i want the combination of the id and the arrays to be its name just like example below.

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

Thank You

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    duanne9313 duanne9313 2016-11-26 10:15

    if you already have $id = 1 Then try this :

    $file_name = $id.'['. $key.$_FILES['files']['name'][$key] . ']';
    
    点赞 评论 复制链接分享

相关推荐