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 2016-11-26 10:15
    关注

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

    $file_name = $id.'['. $key.$_FILES['files']['name'][$key] . ']';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗