使用函数将多个图像上载到数据库

ycggw6v2  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(384)

我已经试着了解如何做这个多图片上传的事情已经有几天了,我想不通。我对这个很陌生,所以请不要小气。我查了好几个网站才真正请求帮助这里。
这是表格所在的页面

<?php
require_once("photoalbum-common.php");
require_once "lib/dbutils.php";
?>

<!DOCTYPE html>
<html>
<head>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <link rel='stylesheet' href='CSS/actors-styles.css'>
    <link rel='stylesheet' href='http://www.w3schools.com/lib/w3.css'>
</head>
<body>
<?php
$pdo = connect();
if ( isset( $_POST['formid']) && $_POST['formid'] == 'fileupload') {
$photoid = addPhotograph( $pdo, $_POST['name'], $_POST['description'], 'self', $_FILES['images']);

    }

?>

<div class='w3-container w3-center'>

<button onclick="document.getElementById('id01').style.display='block'" class="link">Add images</button>
</div>

<div id="id01" class="w3-modal w3-animate-opacity">
  <div class="w3-modal-dialog">
    <div class="w3-modal-content w3-card-4 w3-animate-zoom">
      <header class="w3-container w3-teal"> 
        <a href="" class="w3-closebtn">&times;</a>

        <h2>Upload photograph</h2>
      </header>
      <div class="w3-container">
        <form action='?' method='post' enctype='multipart/form-data' id='photoform' class='w3-container'>
          <div class="w3-group">
<!--            <input type='file' name='uploadfile' class='w3-input'> -->
            <label class="w3-label">Image file</label>
        <input type="file" name="images[]" multiple class='w3-input'/>

          </div>
          <div class="w3-group"> 
            <label class="w3-label">Name</label>  
            <input type='text' name='name' class='w3-input' required>

          </div>
          <div class="w3-group">  
            <label class="w3-label">Description</label> 
            <textarea name='description' class='w3-input' required></textarea>

          </div>
          <input type='hidden' name='formid' value='fileupload'>
          <input type='submit' value='Upload' id='uploadbutton' class='w3-btn'>
        </form>
      </div>
      <footer class="w3-container w3-teal">
        <p></p>
      </footer>
    </div>
  </div>
</div>

</body>
</html>

以下是photoalbum-common.php中的函数

function addPhotograph( $pdo, $name, $description, $user, $fileinfo) {
  $stmt = $pdo->prepare("INSERT INTO photographs ( `name`, `description`, `user`) VALUES (?, ?, ?)");
  $stmt->execute( array( $name, $description, $user));
  $photoid = $pdo->lastInsertId();

  $tmp_name = $fileinfo["tmp_name"];
  $name = $photoid."_".$fileinfo["name"];
  $stmt = $pdo->prepare("UPDATE photographs SET `image`=? WHERE `photoid`=?");
  $stmt->execute( array( $name, $photoid));
  $result = move_uploaded_file($tmp_name, "images/$name");
  return $photoid;  
}

function reArrayFiles(&$file_post) {

    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}

我从第二个函数得到它http://php.net/manual/en/features.file-upload.multiple.php,不幸的是,我没有设法使任何工作。我能想到的最好办法是显示两个上传的图片,而不显示实际的图片,因为它们没有上传到数据库。
如果有人可以帮助我或指导我如何上传多张图片使用以上作为当前内容只能为一张图片上传工作。先谢谢你。

lbsnaicq

lbsnaicq1#

它不起作用,因为您已经设置了文件数组并对单个文件执行代码。
请用以下代码更正:

if(!empty($fileinfo)){
    $total_files = count($fileinfo['name']);        
    for($i = 0 ; $i<$total_files ; $i++)
    {
        $tmp_name = $fileinfo["tmp_name"][$i];
        $name = rand()."_".$fileinfo["name"][$i];
        $result = move_uploaded_file($tmp_name, "images/$name");
    }
}
$tmp_name = $fileinfo["tmp_name"];

相关问题