2012-10-12 68 views
0

我想上传多个图像。我使用Codeigniter,我知道有一个内置的文件上传类,但我只是试验我自定义的图像上传库。我已经提供了下面的代码。试图上传多个图像,但它只上传一个

我面对的问题是下面的代码是只上传一个(最后选择的形式)图像。

请问您能否告诉我我在做什么错?

我的控制器:

function img_upload(){ 

    $this->load->library('image_upload');   

    $image1= $this->image_upload->upload_image('imagefile1'); 
    $image2= $this->image_upload->upload_image('imagefile2'); 
    $image3= $this->image_upload->upload_image('imagefile3'); 

    echo $image1 ."<br>"; echo $image2; 
} 

应用程序/库/ image_upload.php(订作库)

function upload_image($image_info){ 

    $image=$_FILES[$image_info]['name']; 
    $filename = stripslashes($image); 
    $extension = $this->getExtension($filename); 
    $extension = strtolower($extension); 

    $image_name=time().'.'.$extension; 

    $newname="support/images/products/".$image_name; 
    $uploaded = move_uploaded_file($_FILES[$image_info]['tmp_name'], $newname); 

    if($uploaded) {return $image_name; } else {return FALSE;} 
} 

我的形式

<form id="myForm" enctype="multipart/form-data" 
action="<?php echo base_url();?>add/img_upload" method="post" name="myForm"> 

    <input type="file" name="imagefile1" size="20" /><br> 
    <input type="file" name="imagefile2" size="20" /><br> 
    <input type="file" name="imagefile3" size="20" /><br> 
    <br /><br /> 
    <input type="submit" value="upload" />  
</form> 
+0

您是否尝试打印生成的文件名,以查看它是否每次都覆盖图像? –

+1

是的,我试过了,它显示两次相同的文件名。谢谢 –

+0

小文件呢?我曾经有过类似的问题,因为我上传的文件在哪里大到最大限度。 – bottleboot

回答

0

您可以创建某种回滚功能并使用CI本地库。它对于服务器来说只是一些额外的工作,但它不太容易/干净/容易调试代码,并且工作起来。

function do_upload() 
{ 

1 - 构

$config['upload_path'] = './uploads/'; 
    // other configurations 

    $this->load->library('upload', $config); 
    $error = array('stat'=>0 , 'reason'=>'' ; 

2-上传

 if (! $this->upload->do_upload('file_1')) 
     { 
      $error['stat'] = 1 ; 
      $error['reason'] = $this->upload->display_errors() ; 

     } 
     else 
     { $uploaded_array[] = $uploaded_file_name ; } 

      // you may need to clean and re initialize library before new upload 
     if (! $this->upload->do_upload('file_2')) 
     { 
      $error['stat'] = 1 ; 
      $error['reason'] = $this->upload->display_errors() ; 

     } 
     else 
     { $uploaded_array[] = $uploaded_file_name ; } 

3 - 在最后检查错误和回滚

if($error['stat'] == 1) 
{ 
    $upload_path = './upload/'; 
    if(!empty($uploaded_array)) 
    { 
     foreach($uploaded_array as $uploaded) 
     { 
      $file = $upload_path.$uploaded; 
      if(is_file($file)) 
      unlink($file); 
     } 
    } 
    echo 'there was a problem : '.$error['reason']; 
} 
else 
{ 
    echo 'success'; 
} 




} 
+0

感谢您的回复。我没有使用Codeigniter的天真库,因为在上传任何图像之前,我无法检查上传的所有图像是否有效(我的意思是大小和格式)。我的意思是,如果我的任何图像存在验证问题,那么我当然不想上传任何图像。但在codeigniter中,如果任何图像有任何验证错误,没有该特定图像,则会上传其他图像,这是我不想要的。 –

+0

而另一个问题是,我的表单中的图片上传功能是可选的,所以如果我选择两个图片而不是三个codeigniters显示和错误,说我没有选择要上传的文件,但在后台它已经上传了这两个文件。 –

+0

从我看到您检查图库中的图像,并检查并分别上传每个图像。这意味着即使某个图像出现问题,仍会上传其他图片。 – max

0

我已经找到了解决我的问题,并认为它可以帮助我的人我分享。

的问题是在image_upload.php文件(定制库),特别是在这一行:

$image_name=time().'.'.$extension; // 

时间()也许在覆盖这些文件。所以我用以下替换我以前的代码:

function upload_image($image_info){ 

    $image=$_FILES[$image_info]['name']; 
    $filename = stripslashes($image); 
    $extension = $this->getExtension($filename); 
    $extension = strtolower($extension); 

    $image_name=$this->get_random_number().'.'.$extension; 

    $newname="support/images/products/".$image_name; 
    $uploaded = move_uploaded_file($_FILES[$image_info]['tmp_name'], $newname); 

    if($uploaded) {return $image_name; } else {return FALSE;} 
} 


    function get_random_number(){ 

    $today = date('YmdHi'); 
    $startDate = date('YmdHi', strtotime('-10 days')); 
    $range = $today - $startDate; 
    $rand1 = rand(0, $range); 
    $rand2 = rand(0, 600000); 
    return $value=($rand1+$rand2); 
} 
+1

你知道PHP已经有本地的uniqid()函数来生成使用时间和日期的唯一字符串。 – max

+0

:)我完全忘了它。谢谢:) –

+0

到目前为止,我已经开发了另一个功能来检查上传图像之前的验证错误。如果任何上传的图像出现错误,则不会上传其余的图像。它现在工作完美:)谢谢你的时间。 –