簡體   English   中英

多次上傳到MySql數據庫,foreach不提交照片

[英]Multiple Upload to MySql Database, foreach not submitting photos

我有一個腳本可以將一個文件上傳到MySQL數據庫,而我試圖將其更改為上傳多個文件,但是我似乎無法獲得foreach語句來檢查並插入每個文件。

HTML表格

<form id="add_photo_form" class="form-inline" action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">

    <input type="hidden" name="album_id" value="<?php echo $_GET['album_id']; ?>" />

    <div class="form-group">
        <label for="photo_file">Select Photo <span class="required">*</span></label>
        <input class="form-control" type="file" name="photo_file[]" multiple="multiple" />
    </div>

    <div class="form-group text-right">
        <input class="btn btn-primary" type="submit" name="add_photo_submit" value="Submit" />
    </div>

</form>

PHP腳本

if (isset($_POST['add_photo_submit']))
{
    foreach($_FILES['photo_file'] as $new_photo)
    {
        $file_ext      = pathinfo('./albums/img/photos/'.basename($new_photo['name']),PATHINFO_EXTENSION);
        $file_name     = 'image_'.date('mdyHis').uniqid().'.'.$file_ext;
        $file_path     = './albums/img/photos/'.$file_name;
        $album_id      = $_POST['album_id'];
        $photo_file    = $file_name;

        if (!empty($new_photo['name']))
        {
            $uploadOk = 1;
            $check = getimagesize($new_photo['tmp_name']);
            if ($check == false)
            {
                $uploadError = 'This is not a valid image.';
                $uploadOk = 0;
            }
            if (file_exists($file_path))
            {
                $uploadError = 'This file already exists.';
                $uploadOk = 0;
            }
            if ($new_photo['size'] > 1000000000000000000) /* bytes */
            {
                $uploadError = 'The file is too large.';
                $uploadOk = 0;
            }
            if ($file_ext != 'jpg' && $file_ext != 'jpeg' && $file_ext != 'png' && $file_ext != 'gif')
            {
                $uploadError = 'Only JPG, JPEG, PNG, or GIF images are allowed.';
                $uploadOk = 0;
            }
            if ($insert = $db -> prepare("INSERT INTO photos (album_id, photo_file) VALUES (?, ?)"))
            {
                $insert -> bind_param('ss', $album_id, $photo_file);
                if ($uploadOk == 1)
                {
                    move_uploaded_file($new_photo['tmp_name'], $file_path);
                    if ($insert -> execute() == true)
                    {
                        echo '<div class="alert alert-success" role="alert"><span class="icon icon-arrows-check"></span> A new album was created.</div>';
                    }
                    else
                    {
                        echo '<div class="alert alert-danger" role="alert"><span class="icon icon-arrows-deny"></span> '.$insert -> error.'</div>';
                    }
                }
                else
                {
                    echo '<div class="alert alert-danger" role="alert"><span class="icon icon-arrows-deny"></span> '.$uploadError.'.</div>';
                }
                $insert -> close();
            }
        }

    }
}

如果我刪除[]photo_file[]在我的HTML表單,並刪除在PHP腳本foreach語句,我可以上傳一個單一的形象,沒有任何問題。

嘗試上載時,沒有數據庫錯誤,語句錯誤,文件錯誤和錯誤日志。

如何修復我的foreach語句以檢查並上傳每個圖像?

編輯根據下面接受的答案,我的新PHP腳本如下所示。 希望這可以幫助某人。

if (isset($_POST['add_photo_submit']))
{
    $total = count($_FILES['photo_file']['name']);
    for($i=0; $i<$total; $i++)
    {
        $file_ext      = pathinfo('./albums/img/photos/'.basename($_FILES['photo_file']['name'][$i]),PATHINFO_EXTENSION);
        $file_name     = 'image_'.date('mdyHis').uniqid().'.'.$file_ext;
        $file_path     = './albums/img/photos/'.$file_name;
        $album_id      = $_POST['album_id'];
        $photo_file    = $file_name;

        if (!empty($_FILES['photo_file']['name'][$i]))
        {
            $uploadOk = 1;
            $check = getimagesize($_FILES['photo_file']['tmp_name'][$i]);
            if ($check == false)
            {
                $uploadError = 'This is not a valid image.';
                $uploadOk = 0;
            }
            if (file_exists($file_path))
            {
                $uploadError = 'This file already exists.';
                $uploadOk = 0;
            }
            if ($_FILES['photo_file']['size'][$i] > 1000000000000000000) /* bytes */
            {
                $uploadError = 'The file is too large.';
                $uploadOk = 0;
            }
            if ($file_ext != 'jpg' && $file_ext != 'jpeg' && $file_ext != 'png' && $file_ext != 'gif')
            {
                $uploadError = 'Only JPG, JPEG, PNG, or GIF images are allowed.';
                $uploadOk = 0;
            }
            if ($insert = $db -> prepare("INSERT INTO photos (album_id, photo_file) VALUES (?, ?)"))
            {
                $insert -> bind_param('ss', $album_id, $photo_file);
                if ($uploadOk == 1)
                {
                    move_uploaded_file($new_photo['tmp_name'], $file_path);
                    if ($insert -> execute() == true)
                    {
                        echo '<div class="alert alert-success" role="alert"><span class="icon icon-arrows-check"></span> A new album was created.</div>';
                    }
                    else
                    {
                        echo '<div class="alert alert-danger" role="alert"><span class="icon icon-arrows-deny"></span> '.$insert -> error.'</div>';
                    }
                }
                else
                {
                    echo '<div class="alert alert-danger" role="alert"><span class="icon icon-arrows-deny"></span> '.$uploadError.'.</div>';
                }
                $insert -> close();
            }
        }
    }
}
$total = count($_FILES['photo_file']['name']);

// Loop through each file
for($i=0; $i<$total; $i++) {
  //Get the temp file path
  $tmpFilePath = $_FILES['photo_file']['tmp_name'][$i];

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = "./uploadFiles/" . $_FILES['photo_file']['name'][$i];

    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here

    }
  }
}

我認為這可能對您有幫助。 來源: 用php上傳多個文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM