上传图片代码无法正常运行

时间:2014-06-16 15:09:57

标签: php image image-uploading

我有以下代码可以正常工作,只要我上传的图片数量少于3.如果我尝试上传3或图片,那么它上传的比我选择的少一个。即如果我选择3个图像上传仅2个上传,4个然后3上传等。但是插入将图像的文件名保存到数据库 - 如果我有4个图像,则保存所有4个文件名

$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*100; //100 kb
$path = $currentdir."/uploaded_images/"; // Upload directory
$count = 0;


echo $name.'<br>';

 if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
  // Loop $_FILES to exeicute all files
     foreach ($_FILES['files']['name'] as $f => $name) { 
 //echo $name.'<br>';

    $insert_offline_order_item_totals = "INSERT INTO offline_order_images
        VALUE ('','$invoice_number_2','','','','$name');";
        //echo $insert_offline_order_item_totals.'<br><br>';
            mysql_query($insert_offline_order_item_totals, $connection);



    if ($_FILES['files']['error'][$f] == 4) {
        continue; // Skip file if any error found
    }          
    if ($_FILES['files']['error'][$f] == 0) {              
        if ($_FILES['files']['size'][$f] > $max_file_size) {
            $message[] = "$name is too large!.";
            continue; // Skip large files
        }
        elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
            $message[] = "$name is not a valid format";
            continue; // Skip invalid file formats
        }
        else{ // No error found! Move uploaded files 
            if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
            $count++; // Number of successfully uploaded file

            echo $count;
        }
    }
}
}

我可以提供一些建议,说明为什么会发生这种情况。

0 个答案:

没有答案