使用具有多种功能的PHP上传多张图片

时间:2019-04-22 11:27:40

标签: php image upload

如何使用PHP上传多张图片? 这段代码可以正常上传单个文件,但是需要添加功能才能上传多个图像。

<?php
$result = 0;
define('FORUM_PATH', 'full_patch');
require_once (FORUM_PATH . 'api_member_login.php');
$ipbMemberLoginApi = new apiMemberLogin();
$ipbMemberLoginApi->init();
$member = $ipbMemberLoginApi->getMember();
$id = ($member['member_id']);
$foldername = date("Y_m_d");
$uploads_folder = "uploads";
$upload_folder = "uploads/$id/$foldername";
if (file_exists($upload_folder)) {
} else {
    mkdir("$uploads_folder/$id", 0755);
    mkdir("$upload_folder", 0755);
}
if (is_uploaded_file($_FILES['myfile']['tmp_name']) && ($_FILES["myfile"]["size"] <= 18192 * 1 * 18192)) {
    $enabled = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);
    list($width, $height, $extension) = getimagesize($_FILES['myfile']['tmp_name']);
    if (in_array($extension, $enabled)) {
        $newname = uniqid() . "_" . $_FILES['myfile']['name'];
        $imglink = $upload_folder . '/' . $newname;
        if (@move_uploaded_file($_FILES['myfile']['tmp_name'], "$upload_folder/" . $newname)) 
            include ("class.ExifCleaning.php");
        ExifCleaning::adjustImageOrientation($imglink);
        $max_width = 1024;
        $max_height = 800;
        $im = new Imagick();
        $im->readImage($imglink);
        $im->stripImage();
        $im->resizeImage(min($im->getImageWidth(), $max_width), min($im->getImageHeight(), $max_height), imagick::FILTER_CATROM, 1, true);
        if (file_put_contents($imglink, $im)) {
            require ('watermarkImagick.php');
            $result = 1;
        }
    } else {
        $result = 0;
    }
}
?>

HTML:

    <form id="imgUplForm" action="api.php" method="post" enctype="multipart/form-data" target="upload_target" onchange="startUpload();">
    <div id="imageUpload">Upload
        <p id="myf1_upload_process"><img src="uploadstat.gif" border="0" /></p>
        <p id="myf1_upload_form" align="left">
            <input type="file" id="file_browse" name="myfile" onchange="this.form.submit()" accept="image/*" />
        </p>
    </div>
    <iframe id="upload_target" name="upload_target" style="display:none;width:0;height:0;border:0px solid #fff;"></iframe>
</form>

上传后添加了一些功能,例如:调整大小,Exif清洁... 需要所有这些功能来上传图像。

1 个答案:

答案 0 :(得分:1)

您可以通过这种方式上传多个文件

  1. 输入字段必须定义为数组,即images[]

  2. 应将其定义为multiple="multiple"

    <input name="images[]" type="file" multiple="multiple" />
    
    // Count # of uploaded files in array
    $total = count($_FILES['images']['name']);
    
    // Loop through each file
    for( $i=0 ; $i < $total ; $i++ ) {
    
      //Get the temp file path
      $tmpFilePath = $_FILES['images']['tmp_name'][$i];
    
      //Make sure we have a file path
      if ($tmpFilePath != ""){
      //Setup our new file path
      $newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i];
    
    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {
    
      //Handle other code here
    
     }
     }
    }
    

有关更多详细信息,PHP Multiple Upload