多个图像上传的脚本无法正常工作

时间:2015-03-13 12:10:39

标签: php html mysql

我尝试制作脚本,将多个图像上传到服务器上的文件夹中,并将名称和其他信息保存在数据库中。脚本工作正常,直到我尝试添加参数来保存类型,大小和类别。我删除它下方的3行正在工作。

  $fileSize = $_FILES['user_files']['size'];
  $fileType = $_FILES['user_files']['type'];
  $album = $_POST['image_album'];

这是脚本,我希望有人帮我这个。

if (isset($_POST["sub2"])) {

// include resized library
require_once('php-image-magician/php_image_magician.php');
$msg = "";
$valid_image_check = array("image/gif", "image/jpeg", "image/jpg", "image/png", "image/bmp");
if (count($_FILES["user_files"]) > 0) {

$folderName = "uploads/";

$sql = "INSERT INTO images (image_name, image_size, image_type, image_album) VALUES (:img, :size, :type, :album)";
$stmt = $pdo->prepare($sql);

for ($i = 0; $i < count($_FILES["user_files"]["name"]); $i++) {

  if ($_FILES["user_files"]["name"][$i] <> "") {

    $image_mime = strtolower(image_type_to_mime_type(exif_imagetype($_FILES["user_files"]["tmp_name"][$i])));
    // if valid image type then upload
    if (in_array($image_mime, $valid_image_check)) {

      $ext = explode("/", strtolower($image_mime));
      $ext = strtolower(end($ext));
      $filename = rand(10000, 990000) . '_' . time() . '.' . $ext;
      $filepath = $folderName . $filename;

  $fileSize = $_FILES['user_files']['size']; <---- // THIS
  $fileType = $_FILES['user_files']['type']; // THIS
  $album = $_POST['image_album']; // AND THIS

      if (!move_uploaded_file($_FILES["user_files"]["tmp_name"][$i], $filepath)) {
        $emsg .= "Error while uploading - <strong>" . $_FILES["user_files"]["name"][$i] . "</strong><br>";
        $counter++;
      } else {
        $smsg .= "Image <strong>" . $_FILES["user_files"]["name"][$i] . "</strong> is added. <br>";

        $magicianObj = new imageLib($filepath);
        $magicianObj->resizeImage(100, 100);
        $magicianObj->saveImage($folderName . 'thumb/' . $filename, 100);

        /*             * ****** insert into database starts ******** */
        try {
          $stmt->bindValue(":img", $filename);
          $stmt->bindValue(":size", $fileSize);
          $stmt->bindValue(":type", $fileType);  
          $stmt->bindValue(":album", $album);

          $stmt->execute();
          $result = $stmt->rowCount();
          if ($result > 0) {
            // file uplaoded successfully.
          } else {
            // failed to insert into database.
          }
        } catch (Exception $ex) {
          $emsg .= "<strong>" . $ex->getMessage() . "</strong>. <br>";
        }
        /*             * ****** insert into database ends ******** */
      }
    } else {
      $emsg .= "This file <strong>" . $_FILES["user_files"]["name"][$i] . "</strong> isn't image. <br>";
    }
  }
}

这是表格

<form name="f2" action="" method="post" enctype="multipart/form-data">
            <fieldset>
        Album
         <select name="image_album">
            <option value="1">Album 1</option>
            <option value="2">Album 2</option>
            <option value="3" >Album 3</option>
            <option value="4" >Album 4</option>
            <option value="5">Album 5</option>
        </select>
              <input class="files" name="user_files[]" type="file" ><span><a href="javascript:void(0);" class="add" >Add more</a></span>

              <div><input type="submit" class="submit" name="sub2" value="Качи" /> </div>
            </fieldset>
          </form>

0 个答案:

没有答案