本文介绍了上传多个文件到服务器,并写入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以目前我在我的html < input type =file中有以下代码required =requiredname =imagemultiple => $ b

然后在添加 mutliple =标签之前,这对我来说总是有效的, / b>

  if(isset($ _ POST ['Submit']))
{
$ current_image = $ _ FILES [ '图像'] [ '名称'];
$ extension = substr(strrchr($ current_image,'。',1); (($ extension!=png)&&($ extension!=jpg))
{
die('Unknown extension');
}
$ time = date(fYhis);
$ new_image = $ time。 。 $扩展;
$ destination =./../ img / treatments /。new_image;
$ action = copy($ _ FILES ['image'] ['tmp_name'],$ destination);

但是现在我试图上传多个文件,我想我需要添加一个数组来命名它们但我不明白,我不想更改我的代码非常多,如果我可以。



此刻img只是我的数据库中的一个字段,这个问题有多大?



编辑

代码,但似乎无法弄清楚如何实现它,并使其工作...



 <?php 
/ **
*从$ _FILES数组中取出痛苦的类
* @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));


保护函数_normalize($ entry){
if(isset($ entry ['name'])&& is_array($ entry ['name']] )){
$ files = array();
foreach($ entry ['name'] as $ k => $ name){
$ files [$ k] = array(
'name'=> $ name,$ $ b'tmp_name'=> $ entry ['tmp_name'] [$ k],
'size'=> $ entry ['size'] [$ k],
'type' => $ entry ['type'] [$ k],
'error'=> $ entry ['error'] [$ k]
);
}
返回新的自我($文件);
}
return $ entry;
}
}
?>





 <?php 
$ files = new UploadedFiles($ _ FILES) ;
var_dump($ files ['userfile'] ['christiaan'] [0] [0] ['is'] ['gaaf'] [0]);
//或
foreach($ files ['userfile'] ['christiaan'] [0] [0] ['is'] ['gaaf'] as $ file){
后续代码var_dump($文件);
}
?>


解决方案

试试这个(你的问题似乎实际上是完全不知道如何在PHP中这一切工作,而不是一些具体的问题/错误/...): $ b $ p

whatever.php:

 < form enctype =multipart / form-dataaction =<?= $ _ SERVER ['PHP_SELF']?>> 
第一个文件:< input type =filename =uploads []/>< br>
第二档案:< input type =filename =uploads []/>< br>
一次多个文件(firefox,chrome,... not IE< 10):< input type =filename =uploads_multiple []multiple =multiple/>< br>
< input type =submitvalue =Go/>
< / form>
<?php
print_r($ _ FILES);

尝试使用不同的输入(文件大小,缺少文件...)在PHP中。我认为,如果发布一些完成的代码,您将从中受益。



编辑:另外,不要使用副本!大多数情况下它不会工作,有时可能会带来意想不到的结果,有时候你只会打开一堆蠕虫。有一个原因move_uploaded_file存在 - 使用IT!


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...

<?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);
}
?>
解决方案

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!

这篇关于上传多个文件到服务器,并写入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 11:35