2015-06-21 60 views
0

我有一个简单的问题。我只是想用一个数字重命名我的图像,然后自动增加它。 输出应该是 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{ 

     } 
    } 
} 

回答

0

试试这个

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

谢谢你工作得很好..... –

+0

你WELCO我 :) –

0

你应该有图像的类型当u要保存的文件/图片

​​
0

我在这里讨论着如何p中上传多个图像总MVC模式HP

  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> 

相关问题