使用php上传多张图片
我有一个简单的 php 脚本,允许用户上传照片。我想知道如何转换此脚本以使用户能够同时上传多张照片。
这是 php 脚本的一部分:
if (isset($_FILES['images'], $_POST['album_id'])) {
$image = addslashes(file_get_contents($_FILES["images"]["tmp_name"]));
$image_name = addslashes($_FILES['images']['name']);
$image_size = getimagesize($_FILES["images"]["size"]);
$image_temp = addslashes($_FILES['images']['tmp_name']);
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$ex = explode('.', $image_name);
$image_ext = strtolower(end($ex));
$album_id = $_POST['album_id'];
我需要对此部分进行哪些更改才能允许多次上传? 我知道某个地方应该有一个 foreach 循环,但我不知道该把它放在哪里。
I have a simple php script that allows users to upload photos. I'd like to know how to convert this script to enable user to upload multiple photos at the same time.
This is part of the php script:
if (isset($_FILES['images'], $_POST['album_id'])) {
$image = addslashes(file_get_contents($_FILES["images"]["tmp_name"]));
$image_name = addslashes($_FILES['images']['name']);
$image_size = getimagesize($_FILES["images"]["size"]);
$image_temp = addslashes($_FILES['images']['tmp_name']);
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$ex = explode('.', $image_name);
$image_ext = strtolower(end($ex));
$album_id = $_POST['album_id'];
What changes do I need to make to this part to allow multiple uploads?
I know there should be a foreach loop in there somwhere but I don't know where to put it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mike 这里是一个粗略的解决方案,我不会在这里编写整个代码,但我会解释其背后的逻辑。
您有两种方法可以选择,一种是手动命名每个字段(这意味着您仅限于手动添加的字段数量)或使用数组。这两种方法之间的唯一区别是您将它们“指向”您的 php 脚本的方式。如果您为每个字段使用预定义的名称,那么您可以正常调用它们,但如果您使用数组方法,则需要使用如下格式:
$_FILES['FIELDNAME']['#ID']
(#ID 从 0 开始到创建的字段数 - 1,因此您可以花一些时间浏览所有字段)。因此,您的字段位于 html 页面中,而在 php 页面中,您将使用 for 循环遍历它们。因此,您可以使用当前的 php 代码进行单次上传,然后遍历每个字段并一一上传。这是一个例子:
Mike here is a rough solution, I will not write the entire code here but I will explain the logic behind it.
You have 2 ways to go , one would be to name each and every field manually (which would mean that you are limited to the number of fields you add manually) or use an array. the only difference between these 2 methods are the way you "point" them to your php script. incase you use predefined name for every field then you can call them normally but if you use the array method then you would need to use a format like :
$_FILES['FIELDNAME']['#ID']
(#ID starts from 0 to the number of fields that are created - 1, so you can do a while and go through them all).So you have your fields in the html page, and in the php page you would go through them using a for loop. so you can use your current php code for the single upload and then go through each and every field and upload them one by one. here is an example :