簡體   English   中英

多文件上傳,相冊,mysql

[英]multiple file upload, photo album, mysql

我有一個表格,其中包含各個字段,用戶需要選擇附加圖像數量的選項:

 <?php
    $num = 0;
    while($num < $num_uploads)
    {
        echo '<div><input name="userfile[]" type="file" /></div>';
        $num++;
    }
 ?>

提交后,它將在數據庫中創建一個名為“相冊”的表,該表如下所示:

function create_album($params)
{
   db_connect();

     $query = sprintf("INSERT INTO albums set
                                     albums.title = '%s',
                                                                 albums.email = '%s',
                                                                 albums.discuss_url = '%s',
                                                                 albums.theme_id = '%s',
                                                                 albums.fullname = '%s',
                                                                 albums.description = '%s',
                                                                 created_at = NOW()",
                                                                 mysql_real_escape_string($params['title']),
                                                                 mysql_real_escape_string($params['email']),
                                                                 mysql_real_escape_string($params['discuss_url']),
                                                                 mysql_real_escape_string($params['theme_id']),
                                                                 mysql_real_escape_string($params['fullname']),
                                                                 mysql_real_escape_string($params['description'])
                                                                 );

     $result = mysql_query($query);
     if(!$result)
     {
          return false;
     }

     $album_id = mysql_insert_id();

     return $album_id;
}   

我想要這些文件,但是要轉到“圖像”表並鏈接到正確的相冊。

$create_album = create_album($_POST['album']);

                   mysql_query( "INSERT INTO images(`name`,`album_id`) VALUES('$newName', '$create_album')" );

我有一個問題,就是將這些多個圖像(用戶可以選擇提交一個或2個或3個文件)與一張表單一起提交到一張相冊。 現在,如果用戶提交3個文件,則每次提交表單都會創建3個相冊。

最后,這是我的數據庫結構:

CREATE TABLE `albums` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(50) NOT NULL,
  `fullname` varchar(40) NOT NULL,
  `discuss_url` varchar(150) NOT NULL,
  `email` varchar(100) NOT NULL,
  `created_at` datetime NOT NULL,
  `theme_id` int(11) NOT NULL,
  `description` int(11) NOT NULL,
  `vote_cache` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;



CREATE TABLE `images` (
  `id` int(11) NOT NULL auto_increment,
  `album_id` int(11) NOT NULL,
  `name` varchar(30) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

我只是想對此問題進行快速更新。 也許我沒有提供足夠的信息,這對於解決問題很重要。 本文對此進行了很好的解釋。 基本上,相冊是在每次提交文件時創建的,但不是表單。

所以這是我的表格(工作中):

<?php if (!isset($_POST['btnSubmit'])) { ?>
<form action="index.php?view=create" method="post" enctype="multipart/form-data">
    <fieldset>
     <div>
             <label><b>Title:</b></label>

             <input name="album[title]" size="40" type="text" value="" class="textfield" />
             </div>

            <div>
           <label><b>Fullname</b></label>
           <input name="album[fullname]" size="40" type="text" value="" class="textfield" />
            </div>

            <div>

           <label><b>Attach Photo</b> </label>
                <input type="hidden" name="" value="" />
 <?php
    $num = 0;
    while($num < $num_uploads)
    {
        echo '<div><input name="userfile[]" type="file" /></div>';
        $num++;
    }
 ?>

這是php(我刪掉了其中的一部分,這樣比較容易):

    //If form was submitted
    if (isset($_POST['btnSubmit'])) {

    create_album($_POST['album']);
    $find_album_id = mysql_insert_id();

        /*** check if a file has been submitted ***/
        if(isset($_FILES['userfile']['tmp_name']))
        {
            /** loop through the array of files ***/
            for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++)
            {

              $ext = strrchr($imgName, ".");

               // then create a new random name
              $newName = md5(rand() * time()) . $ext;

                      if (is_image_types($_FILES['userfile']['type'][$i]) 
                      and is_valid_file_size($_FILES['userfile']['size'][$i])
                      and is_uploaded_file($_FILES['userfile']['tmp_name'][$i])
                      and is_valid_width_height($_FILES['userfile']['tmp_name'][$i])
                   )
                  {    

                       mysql_query("INSERT INTO images(name, album_id) VALUES('$newName', '$find_album_id')") ; 
                       copy($_FILES['userfile']['tmp_name'][$i], './photos/'.$original_dir.'/' .$newName.'.jpg');

            }

            else
            {
         $warning = "Click back error uploading file. 
                             Please make sure your file is a JPEG and less than 1MB";
            }
        }
    }

    }   

    break;
}

我希望它對正在尋找的人有所幫助...

暫無
暫無

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

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