用数字重命名我的图像然后自动递增它

时间:2015-06-21 07:17:33

标签: php web

我有一个简单的问题。我只想用一个数字重命名我的图像然后自动递增它。 输出应该是 1.JPG 2.JPG 3.JPG 这是我的代码:

if(isset($_FILES['file_array'])){

    $name_array = $_FILES['file_array']['name'];
    $tmp_name_array = $_FILES['file_array']['tmp_name'];
    $type_array = $_FILES['file_array']['type'];
    $size_array = $_FILES['file_array']['size'];
    $error_array = $_FILES['file_array']['error'];

    $pid = mysql_insert_id();
    $foldername = "$pid";
    $dir = "../../img/inventory_images/$foldername/";
    mkdir($dir, 0777, true);

    for($i = 0; $i < count($tmp_name_array); $i++){
        if(move_uploaded_file($tmp_name_array[$i], "../../img/inventory_images/$foldername/".$name_array[$i])){

        }else{

        }
    }
}

3 个答案:

答案 0 :(得分:0)

当您想要保存文件/图像时,您应该有图像类型

".$name_array[$i].$type_array

答案 1 :(得分:0)

试试这个

"../../img/inventory_images/$foldername/{$i}.".pathinfo($name_array[$i], PATHINFO_EXTENSION)

答案 2 :(得分:0)

这里我将讨论总体mvc模型以及如何在php中上传多个图像

            public function finishing_product()
                { 
                if($this->session->userdata('user_name') == TRUE)
                {
                $data['user_name']=$this->session->userdata('user_name');      
                //$this->load->library('upload'$config);

                if(isset($_POST['sub1']))

                {
                $this->load->model('student_reg');
                $data['cat_id']=$_POST['cat_id'];
                if ($data['cat_id']=="1")

            {
                $loc='C:/xampp/htdocs/mahata/uploads/';
                $files = $_FILES;
                $cpt = count($_FILES['userfile']['name']);
                for($i=0; $i<$cpt; $i++)

            {
                $_FILES['userfile']['name']= $files['userfile']['name'][$i];
                $_FILES['userfile']['type']= $files['userfile']['type'][$i];
                $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
                $_FILES['userfile']['error']= $files['userfile']['error'][$i];
                $_FILES['userfile']['size']= $files['userfile']['size'][$i];    
                $this->load->library('upload', $config);  
                $this->upload->initialize($this->set_upload_options());
                $this->upload->do_upload();



                $name = $_FILES["userfile"]["name"][$i];
                $ext = end((explode(".", $files['userfile']['name'][$i])));
                echo $file;
                $id=$this->student_reg->finishing_cat1($data);
                $file= $id.".".$ext;
                $filepath= $loc . $file;
                echo $filepath; 

                if (move_uploaded_file($_FILES['userfile']['tmp_name'], $filepath))

                {
                echo "File is valid, and was successfully uploaded.\n";
                } 

                else {
                echo "Upload failed";
                }



                } 
                 }
                $this->load->view('finishing_product',$data);

                }

                else
                {
                redirect("index");
                }

                }

我的模特

 public function infra_cat1($data)
{

$sql="INSERT INTO `infracat1`(`category_id`) 
VALUES (".$data['cat_id'].")";

 $this->db->query($sql);
 $id=$this->db->insert_id();

return $id;
}

我的观看文件

     <div class="col-lg-5">
     <div class="form-group required"> <label class="control-label">Infra Category</label>
     <select name="cat_id" class="form-control"  >
     <option value="-1">select</option>

<option value="1" <?php if ($month_b==1) {echo "selected";}?>  >Steel </option>
<option value="2" <?php if ($month_b==2) {echo "selected";}?>  >Electrical</option> 
<option value="3" <?php if ($month_b==3) {echo "selected";}?>  >Plumbing solution</option> 
<option value="4" <?php if ($month_b==4) {echo "selected";}?>  >Water Proofing</option> 
<option value="5" <?php if ($month_b==5) {echo "selected";}?>  >Manufactured and Plastering Sand</option> 
<option value="6" <?php if ($month_b==6) {echo "selected";}?>  >Pavers</option>
<option value="7" <?php if ($month_b==7) {echo "selected";}?>  >Windows </option>



    </select>


                <div>    
         <table class="table table-stripped">
<tbody>

    <tr>
        <td>Display</td>
        <td>
            <input class="form-control" type="file" name="userfile[]" id="multiple" multiple="" />    
        </td>
    </tr>

    <tr>
        <td colspan="2">
            <button type="submit" name="sub1" class="btn btn-primary btn-outline btn-block pull-right"><span>Save</span></button>
        </td>
    </tr>
</tbody>