奇怪的PHP多文件上传问题

时间:2013-12-31 13:35:35

标签: php file upload

在您提问之前,我确实查看了所有相似的主题,并没有找到解决问题的方法。当我尝试上传多个文件时,例如4,上传3个文件。我确定循环是正确的,但我可能是错的。自学成才的PHP新手。

代码如下:

if(isset($_POST['submit']) && $_POST['submit']=="Upload"){
       if(count($_FILES['image_filename']['name']) == 0)
       {
            $form->seterror("fileupload"," * At least one file required.");
       }
       if($form->num_errors == 0)
       {

          $directory="../../images/properties";
          $ref='';

          //Loop through each file
          for($i=0; $i<count($_FILES['image_filename']['name']); $i++) 
          {

                 if($_FILES['image_filename']['name'][$i] != "")
                 {      
                    //Get the temp file path
                    $filename = $_FILES['image_filename']['name'][$i];    // filename stores the value
                    $filename = str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
                    $filename = stripslashes($filename); // strip file_name of slashes
                    $filename = str_replace("'","",$filename); //remove quotes
                    $filesize = $_FILES['image_filename']['size'][$i];
                    $filetype = $_FILES['image_filename']['type'][$i];
                    $filetemp = $_FILES['image_filename']['tmp_name'][$i];

                    //echo $filename.'<br>';
                    $filework->file_upload($_FILES['image_filename']['name'][$i],$directory,$_FILES['image_filename']['tmp_name'][$i]); // this is my File upload class


                    $ref=$_FILES['image_filename']['name'][$i]; 
                    if($countImg == 0) {
                       if($i==0) {
                            $main='1'; 
                       } else {
                          $main='0';
                       }
                    } else {      
                        $main='0';
                    }

                     $q="INSERT INTO property_images(property_id,image_filename,image_reference,main_image)".
                       " VALUES (".$_GET['pid'].",'".$_FILES['image_filename']['name'][$i]."','".$ref."','".$main."')";
                    //echo $q.'<br>';
                    $database->query($q) or die(mysql_error());

                    $x++;   
                 }
            }         
        }

} [/代码]

1 个答案:

答案 0 :(得分:0)

循环不是跳过最后一个文件吗?

尝试更改此内容:

for($i=0; $i<count($_FILES['image_filename']['name']); $i++)

为:

for($i=0; $i<=count($_FILES['image_filename']['name']); $i++)