上传多个文件到服务器,并写入数据库

发布于 2024-12-15 01:27:10 字数 2423 浏览 0 评论 0原文

所以目前我的 html 中有以下代码

,然后在添加之前mutliple="" 标签,这一直对我有用,

if(isset($_POST['Submit']))
{
$current_image=$_FILES['image']['name'];
$extension = substr(strrchr($current_image, '.'), 1);
if (($extension!= "png") && ($extension != "jpg")) 
{
die('Unknown extension');
}
$time = date("fYhis");
$new_image = $time . "." . $extension;
$destination="./../img/treatments/".$new_image;
$action = copy($_FILES['image']['tmp_name'], $destination);

但现在我正在尝试上传多个文件,我认为我需要添加一个数组来命名它们,但我无法弄清楚,我不知道如果可以的话,我不想太多改变我的代码。

另外,目前 img 只是我的数据库中的一个字段,这有多大问题?

编辑

我找到了这段代码,但似乎无法弄清楚如何实现它并使其工作......

在尝试了数十种应该修复 $_FILES 数组的不稳定问题的方法之后,我没有找到任何可以与输入名称一起使用的方法,例如: userfile[christiaan][][][is][gaaf][ ]

所以我想出了这个课程

<?php
/**
 * A class that takes the pain out of the $_FILES array
 * @author Christiaan Baartse <[email protected]>
 */
class UploadedFiles extends ArrayObject
{
    public function current() {
        return $this->_normalize(parent::current());
    }

    public function offsetGet($offset) {
        return $this->_normalize(parent::offsetGet($offset));
    }

    protected function _normalize($entry) {
        if(isset($entry['name']) && is_array($entry['name'])) {
            $files = array();
            foreach($entry['name'] as $k => $name) {
                $files[$k] = array(
                    'name' => $name,
                    'tmp_name' => $entry['tmp_name'][$k],
                    'size' => $entry['size'][$k],
                    'type' => $entry['type'][$k],
                    'error' => $entry['error'][$k]
                );
            }
            return new self($files);
        }
        return $entry;
    }
    }
    ?>

这允许您访问使用以下输入类型上传的文件 喜欢

<?php
$files = new UploadedFiles($_FILES);
var_dump($files['userfile']['christiaan'][0][0]['is']['gaaf'][0]);
// or
foreach($files['userfile']['christiaan'][0][0]['is']['gaaf'] as $file) {
    var_dump($file);
}
?>

So at the moment I have the following code in my html <input type="file" required required="required" name="image" multiple="">

and then before I added the mutliple="" tag, this always worked for me,

if(isset($_POST['Submit']))
{
$current_image=$_FILES['image']['name'];
$extension = substr(strrchr($current_image, '.'), 1);
if (($extension!= "png") && ($extension != "jpg")) 
{
die('Unknown extension');
}
$time = date("fYhis");
$new_image = $time . "." . $extension;
$destination="./../img/treatments/".$new_image;
$action = copy($_FILES['image']['tmp_name'], $destination);

but now that i am trying to upload multiple files I think I need to add an array to name them but I cannot figure it out, I don't want to change my code very much if I can.

Also at the moment the img is only one field on my database, how much of an issue is this?

EDIT

I found this code but cant seem to figure out how to implement it and make it work...

After trying dozens of ways that are supposed to fix the wonkyness of the $_FILES array I didn't find any that could work with a input name like: userfile[christiaan][][][is][gaaf][]

So I came up with this class

<?php
/**
 * A class that takes the pain out of the $_FILES array
 * @author Christiaan Baartse <[email protected]>
 */
class UploadedFiles extends ArrayObject
{
    public function current() {
        return $this->_normalize(parent::current());
    }

    public function offsetGet($offset) {
        return $this->_normalize(parent::offsetGet($offset));
    }

    protected function _normalize($entry) {
        if(isset($entry['name']) && is_array($entry['name'])) {
            $files = array();
            foreach($entry['name'] as $k => $name) {
                $files[$k] = array(
                    'name' => $name,
                    'tmp_name' => $entry['tmp_name'][$k],
                    'size' => $entry['size'][$k],
                    'type' => $entry['type'][$k],
                    'error' => $entry['error'][$k]
                );
            }
            return new self($files);
        }
        return $entry;
    }
    }
    ?>

This allows you to access a file uploaded using the following inputtype
<input type="file" name="userfile[christiaan][][][is][gaaf][]" />
like

<?php
$files = new UploadedFiles($_FILES);
var_dump($files['userfile']['christiaan'][0][0]['is']['gaaf'][0]);
// or
foreach($files['userfile']['christiaan'][0][0]['is']['gaaf'] as $file) {
    var_dump($file);
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彩虹直至黑白 2024-12-22 01:27:10

试试这个(你的问题实际上似乎是完全不了解这一切在 php 中是如何工作的,而不是一些特定的问题/bug/...):whatever.php

<form enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>">
First file: <input type="file" name="uploads[]" /><br>
Second File: <input type="file" name="uploads[]" /><br>
Multiple files at once (firefox, chrome, ... not IE < 10): <input type="file" name="uploads_multiple[]" multiple="multiple" /><br>
<input type="submit" value="Go" />
</form>
<?php
print_r($_FILES);

尝试使用不同的输入(文件大小,丢失的文件, ...) 看看你能从 php.ini 中得到什么。我认为与发布一些完成的代码相比,您会从这种方式中受益更多。

编辑:另外,不要使用副本!大多数时候它是行不通的,有时它可能会产生意想不到的结果,有时你只会打开一罐蠕虫。 move_uploaded_file 函数存在是有原因的 - 使用它!

Try this (your problem seems in fact to be the totally missing understanding of how this all works in php, instead of some specific problem/bug/...):

whatever.php:

<form enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>">
First file: <input type="file" name="uploads[]" /><br>
Second File: <input type="file" name="uploads[]" /><br>
Multiple files at once (firefox, chrome, ... not IE < 10): <input type="file" name="uploads_multiple[]" multiple="multiple" /><br>
<input type="submit" value="Go" />
</form>
<?php
print_r($_FILES);

Try this with different inputs (filesizes, missing files, ...) to see what you get from that in php. I think you'll benefit from this way more than if posted some finished code.

Edit: Also, DON'T use copy for this! Most of the time it just won't work, sometimes it might give unexpected results, and sometimes you'll just be opening a can of worms. There is a reason the function move_uploaded_file exists - USE IT!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文