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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?