Php 多图像上传限制

Php 多图像上传限制,php,file-upload,Php,File Upload,我从我的朋友那里得到了这段代码代码工作正常,但我需要设置最多10张图像的限制: <?php if(isset($_POST['submit'])){ // Include the database configuration file include_once 'dbConfig.php'; // File upload configuration $targetDir = "uploads/"; $allowTypes = array('jpg','p

我从我的朋友那里得到了这段代码代码工作正常,但我需要设置最多10张图像的限制:

<?php 
if(isset($_POST['submit'])){ 
// Include the database configuration file 
include_once 'dbConfig.php'; 
 
// File upload configuration 
$targetDir = "uploads/"; 
$allowTypes = array('jpg','png','jpeg','gif'); 
 
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = ''; 
$fileNames = array_filter($_FILES['files']['name']); 
if(!empty($fileNames)){ 
    foreach($_FILES['files']['name'] as $key=>$val){ 
        // File upload path 
        $fileName = basename($_FILES['files']['name'][$key]); 
        $targetFilePath = $targetDir . $fileName; 
         
        // Check whether file type is valid 
        $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION); 
        if(in_array($fileType, $allowTypes)){ 
            // Upload file to server 
            if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){ 
                // Image db insert sql 
                $insertValuesSQL .= "('".$fileName."', NOW()),"; 
            }else{ 
                $errorUpload .= $_FILES['files']['name'][$key].' | '; 
            } 
        }else{ 
            $errorUploadType .= $_FILES['files']['name'][$key].' | '; 
        } 
    } 
     
    if(!empty($insertValuesSQL)){ 
        $insertValuesSQL = trim($insertValuesSQL, ','); 
        // Insert image file name into database 
        $insert = $db->query("INSERT INTO images (file_name, uploaded_on) VALUES $insertValuesSQL"); 
        if($insert){ 
            $errorUpload = !empty($errorUpload)?'Upload Error: '.trim($errorUpload, ' | '):''; 
            $errorUploadType = !empty($errorUploadType)?'File Type Error: '.trim($errorUploadType, ' | '):''; 
            $errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType; 
            $statusMsg = "Files are uploaded successfully.".$errorMsg; 
        }else{ 
            $statusMsg = "Sorry, there was an error uploading your file."; 
        } 
    } 
}else{ 
    $statusMsg = 'Please select a file to upload.'; 
} 
 
// Display status message 
echo $statusMsg; 
} 
?>


如何设置最多10张图像的上载限制?

只需添加带条件的break语句即可。这将循环处理前9个文件:

<?php 
//...
    foreach($_FILES['files']['name'] as $key=>$val){ 
        if($key == 10) {
           break;
        } 
    // ...
   }
?>

警告:您完全可以使用参数化的预处理语句,而不是手动生成查询。它们由或提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行。