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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料