简体   繁体   English

使用一个输入字段上传多张图像

[英]Uploading multiple images with one input field

i have created a photography website with an admin page that uploads photos to different categories in a mysql table.我创建了一个带有管理页面的摄影网站,该页面将照片上传到 mysql 表中的不同类别。 This much works, but i can only upload one file at a time and i'd like to be able to select multiple images.这很有效,但我一次只能上传一个文件,我希望能够选择多个图像。

Here's the form这是表格

<form action="index.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">

    <select name="category" id="category">
        <option value="Nature">Nature</option>
        <option value="People">People</option>
        <option value="Abstract">Abstract</option>
    </select>

    <input type="file" name="fileField" id="fileField" />

    <input type="submit" name="submit" id="submit" value="Add Images" />

</form>

And here's the php for parsing the form这是用于解析表单的 php

if (isset($_POST['submit'])) {

$category = mysql_real_escape_string($_POST['category']);
// Add this product into the database now
$sql = mysql_query("INSERT INTO images (category, date_added) 
    VALUES('$category',now())") or die (mysql_error());


 $pid = mysql_insert_id();
// Place image in the folder 
$newname = "$pid.jpg";
move_uploaded_file( $_FILES['fileField']['tmp_name'], "../photos/$newname");
header("location: thumbnail_generator.php"); 
exit();
}

I looked into the html5 file input method, and as far as i can tell, i can change the input as folllows:我查看了 html5 文件输入法,据我所知,我可以按以下方式更改输入:

<input type="file" name="fileField[]" id="fileField" multiple="multiple"/>

This allows me to select multiple files on the site, but i can't figure out how to implement this into my php.这允许我在站点上选择多个文件,但我无法弄清楚如何将其实现到我的 php.ini 文件中。 Any help would be much appreciated.任何帮助将非常感激。

<form method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
Add Photos <input multiple="true" onchange="this.form.submit()" type="file" name="photos[]"/>

<input type="hidden" name="sendfiles" value="Send Files" />
</form>

    <?php
define ("MAX_SIZE","5000"); 
function getExtension($str)
{
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

$errors=0;

if(isset($_POST['sendfiles'])) 
{
  $uploaddir = "files/"; //a directory inside
    foreach ($_FILES['photos']['name'] as $name => $value)
    {
        $filename = stripslashes($_FILES['photos']['name'][$name]);
     //get the extension of the file in a lower case format
          $extension = getExtension($filename);
         $extension = strtolower($extension);
        echo "\n This is the extension: ",$extension;
         if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
         {
        //print error message
        ?>
           <h4>Unknown extension!</h4>
          <?php
             $errors=1;
         }
        else
        {
            $size=filesize($_FILES['photos']['tmp_name'][$name]);
            if ($size > MAX_SIZE*1024)
            {
            ?>
               <h4>You have exceeded the size limit!</h4>
              <?php
                $errors=1;
            }
            $image_name=$filename.'.'.$extension;
            $newname="files/".$image_name;
            $copied = copy($_FILES['photos']['tmp_name'][$name], $newname);
            if (!$copied) 
            {
                ?>
                <h4>Copy unsuccessfull!</h4>
                <?php
                $errors=1;
            }

        }

    }
    //echo "Images uploaded successfully";
}
//mysql_close($dbhandle);
?>

您可能正在寻找诸如uploadifyswfuploadplupload 之类的东西

It works as-if there were multiple file fields Docs , it's transparent to PHP.它就像有多个文件字段Docs 一样工作,它对 PHP 是透明的。 Here is some simple example code:下面是一些简单的示例代码:

<html>
  <body>
    <form action="" method="post" enctype="multipart/form-data">
      <input type="file" name="fileField[]" id="fileField" multiple="multiple">
      <input type="text" name="test" value="test">
      <input type="submit" name="submit">
    </form>
  </body>
</html>
<?php
var_dump($_FILES, $_POST);

Store it on your host and request it.将其存储在您的主机上并请求它。 You can then play around with it, it will show you the structure of the $_FILES and $_POST array.然后你可以玩弄它,它会告诉你$_FILES$_POST数组的结构。

Example output:示例输出:

array
  'fileField' => 
    array
      'name' => 
        array
          0 => string 'hakre-iterator-fun-cut-top.png' (length=30)
          1 => string 'hakre-iterator-fun-cut-middle.png' (length=33)
      'type' => 
        array
          0 => string 'image/png' (length=9)
          1 => string 'image/png' (length=9)
      'tmp_name' => 
        array
          0 => string '/tmp/php1A2.tmp' (length=15)
          1 => string '/tmp/php1A3.tmp' (length=15)
      'error' => 
        array
          0 => int 0
          1 => int 0
      'size' => 
        array
          0 => int 234001
          1 => int 213058
array
  'test' => string 'test' (length=4)
  'submit' => string 'Submit' (length=6)

See Uploading multiple files Docs .请参阅 上传多个文件Docs

Wouldn't something like creating an array work too?像创建数组这样的事情也不会起作用吗? So what he could do is所以他能做的是

$images = array();

Then in his form just add a add to image list button?然后在他的表单中添加一个添加到图像列表按钮? From there he just keep adding as many images as he wants and then just iterate over the array and add to the database?从那里开始,他只是不断添加任意数量的图像,然后遍历数组并添加到数据库中?

Simple!简单的! Just Add multiple="true" to your input tag.只需将 multiple="true" 添加到您的输入标签。

<input name "example" type="file" multiple="true"/>

Try out this.试试这个。

http://www.uploadify.com/demos/ http://www.uploadify.com/demos/

Uploadify is a powerful and highly-customizable file upload script. Uploadify 是一个功能强大且高度可定制的文件上传脚本。 Uploadify is easy to get up and running with minimal effort and little coding knowledge. Uploadify 只需很少的工作和很少的编码知识即可轻松启动和运行。

you will receive an array of images -> fileField[].您将收到一组图像 -> fileField[]。 Just iterate through this array and add the images the same way you were adding them before.只需遍历此数组并以与之前添加图像相同的方式添加图像。 Also, I'd suggest using this, very nice script - http://valums.com/ajax-upload/另外,我建议使用这个非常好的脚本 - http://valums.com/ajax-upload/

EDIT: Just don't forget to implement at least some security checks, like min and max upload size, file extensions and mime-type checking!编辑:不要忘记至少实施一些安全检查,例如最小和最大上传大小、文件扩展名和 mime 类型检查! Even if it's for the back-end, this could still lead to unpleasant events.即使是用于后端,这仍然可能导致不愉快的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM