【问题标题】:upload multiple files to server, and write to database将多个文件上传到服务器,并写入数据库
【发布时间】:2011-11-11 01:44:20
【问题描述】:

所以目前我的html中有以下代码<input type="file" required required="required" name="image" multiple="">

然后在我添加 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 <christiaan@baartse.nl>
 */
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;
    }
    }
    ?>

这允许您访问使用以下输入类型上传的文件 &lt;input type="file" name="userfile[christiaan][][][is][gaaf][]" /&gt; 喜欢

<?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);
}
?>

【问题讨论】:

    标签: php mysql file-upload upload multiple-forms


    【解决方案1】:

    【讨论】:

    • 我找到了这个,但我不确定如何用我的代码来实现它,因为我仍然希望它只被命名为相同的名称,比如末尾的 _1、_2 等名字...
    • 我认为你可以编辑 $name try it 并告诉我你发现了什么 try it by append this time().$name
    【解决方案2】:

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

    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 存在是有原因的 - 使用它!

    【讨论】:

      猜你喜欢
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多