多张照片上传PHP [英] Multiple Photo Upload PHP

查看:95
本文介绍了多张照片上传PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,努力找出如何上传多个图像(.jpg / .png / etc)。
对于我的任务,我希望将5张图片上载到数据库记录。到目前为止,我一直在努力将一起上传5个记录。我已经使用了Ws3网站的PHP,并且它可以成功运行,但是这个代码仅用于一个图像。

I've looked around and have struggled to find out how to upload multiple images (.jpg/.png/etc). For my task I was looking to upload 5 pictures to the database record. So far I'm struggling to even upload 5 together in one record. I've used PHP from the Ws3 website and it works successfully but this code is for one image alone.

PHP代码 -

 $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
  if ($_FILES["file"]["error"] > 0)
  {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
  }
  else
  {

    if (file_exists("upload/" . $_FILES["file"]["name"]))
    {

    }
    else
    {
        move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . uniqid() . "_" . $_FILES["file"]["name"]);
    }
  }
}
else
{
    $error =  "Invalid file";
}

我的HTML如下,

My HTML is as follows,

<label for="file">Filename:</label>
        <input type="file" name="file" id="file">

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

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

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

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

任何建议都非常感谢家伙! Cheers

Any advice is greatly appreciated guys! Cheers

推荐答案

首先,添加 enctype =multipart / form-data到你的表格。然后将文件字段的名称更改为:

First, add enctype="multipart/form-data" to your form. Then change the names of the file fields to:

<input type="file" name="file[]" id="file" >
<input type="file" name="file[]" id="file" >
<input type="file" name="file[]" id="file" >

这将创建一个提交文件的数组。现在,您可以在文件上执行 foreach 。快速示例:

This will create an array of the files submitted. Now you'll be able to do a foreach on the files. Quick example:

foreach ($_FILES['file']['name'] as $f => $name) {

}

围绕您现有的代码,然后添加 [$ f] 给每个 $ _ FILES [file] 变量。所以 $ _ FILES [file] [size] 必须改为 $ _ FILES [file] [size] [ $ f] 等等。在 foreach 引用 $ name $ _ FILES [file] [name ] [$ f] ,所以你可以用 $ name 来代替。

Around your existing code, then add [$f] to every $_FILES["file"] variable. So $_FILES["file"]["size"] has to be changed to $_FILES["file"]["size"][$f] and so on. In the foreach I referring $name to $_FILES["file"]["name"][$f], so you can use $name instead.

完整的php代码基于您的脚本:

Full php code based on your script:

foreach ($_FILES['file']['name'] as $f => $name) {
 $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $name);
    $extension = end($temp);

if ((($_FILES["file"]["type"][$f] == "image/gif")
|| ($_FILES["file"]["type"][$f] == "image/jpeg")
|| ($_FILES["file"]["type"][$f] == "image/jpg")
|| ($_FILES["file"]["type"][$f] == "image/png"))
&& ($_FILES["file"]["size"][$f] < 2000000)
&& in_array($extension, $allowedExts))
{
  if ($_FILES["file"]["error"][$f] > 0)
  {
    echo "Return Code: " . $_FILES["file"]["error"][$f] . "<br>";
  }
  else
  {

    if (file_exists("upload/" . $name))
    {

    }
    else
    {
        move_uploaded_file($_FILES["file"]["tmp_name"][$f], "upload/" . uniqid() . "_" . $name);
    }
  }
}
else
{
    $error =  "Invalid file";
}
}

最后,我建议你去学习更多关于PHP在不同的网站上。 W3schools很容易,但也不是你最好的选择。例如,你正在检查这个:

At the end, I would suggest you to go learn more about PHP on different sites. W3schools is easy, but also not your best option. As an example, you are checking this:

    if ((($_FILES["file"]["type"][$f] == "image/gif")
|| ($_FILES["file"]["type"][$f] == "image/jpeg")
|| ($_FILES["file"]["type"][$f] == "image/jpg")
|| ($_FILES["file"]["type"][$f] == "image/png"))
&& ($_FILES["file"]["size"][$f] < 2000000)
&& in_array($extension, $allowedExts))

但是只有数组就足够了,像这样:

But only the array will be enough, like this:

    if ($_FILES["file"]["size"][$f] < 2000000 && in_array($extension, $allowedExts))

$ _ FILES [file] [type] 并不总是像预期的那样返回类型。我们在音频和视频文件中多次看到它。关于指向W3school脚本的MIME类型的另一篇文章: PHP:$ _FILES ["文件" " type"]是无用的

$_FILES["file"]["type"] not always returns the type as expected.. We see it many times in audio and video files. Another post on SO about MIME types referring to the W3schools script: PHP: $_FILES["file"]["type"] is useless

在对StackOverflow提出新问题之前,我刚刚为你写了一个长长的答案,但同样的事情已经为这类问题做过很多次了。 https://stackoverflow.com/search?q=multiple+upload+php

Before asking new questions on StackOverflow, please do a search. I just wrote a long answer just for you, but the same thing has been done MANY times for this kind of question already. https://stackoverflow.com/search?q=multiple+upload+php

希望它能回答你的问题,祝你学习PHP。

Hope it answers your question, and good luck learning PHP.

这篇关于多张照片上传PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆