多次上传,只上传最后一个文件 [英] Multiple upload, only the last file is uploaded

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

问题描述

我目前正在做一个网站项目,需要以相同的形式上传多张图片.

I am currently working on a website project, and need to have more than one image uploaded in the same form.

提交表单时,只上传了最后一张图片,不知道为什么.我一直在谷歌、这个网站和许多其他网站上寻找答案,但我找不到任何有完全相同问题的人来找到解决方案.

When the form is submitted, only the last picture is uploaded, and I can't figure out why. I've been looking on an answer on Google, this website and many others, but I couldn't find anyone with exactly the same problem to find a solution.

我已经用 WAMP 和在线测试了这个基本代码,问题仍然存在...

I have tested this basic code both with WAMP and online, and the problem remains the same...

表格如下:

<form action="index.php?action=add" method="post" enctype="multipart/form-data">
<input type="file" name="file1"/><br/>
<input type="file" name="file2"/><br/>
<input type="file" name="file3"/><br/>
<input type="hidden" name="add" value="1"/>
<input type="submit" value="ok"/>

这是我用于上传的代码:

And here is the code I use for the upload :

function move_avatar($avatar)
{
    $extension_upload = strtolower(substr(  strrchr($avatar['name'], '.')  ,1));
    $name = time();
    $nomavatar = str_replace(' ','',$name).".".$extension_upload;
    $name = "images/".str_replace(' ','',$name).".".$extension_upload;
    move_uploaded_file($avatar['tmp_name'],$name);
    return $nomavatar;
}
if(!empty($_POST['add'])){
    for($i=1;$i<=3;$i++){
        if(!empty($_FILES['file'.$i]['size'])){
            $extensions_valides = array( 'jpg' , 'jpeg' , 'gif' , 'png', 'bmp' );           
            $extension_upload = strtolower(substr(strrchr($_FILES['file'.$i]['name'], '.')  ,1));
            if(in_array($extension_upload,$extensions_valides))     
            $img =(!empty($_FILES['file'.$i]['size']))?move_avatar($_FILES['file'.$i]):'';
            else $img = 'defaultImg.png';
        }else $img = 'defaultImg.png';
    }
    print_r($_POST);
}else include('test.php');

有什么想法吗?:/

推荐答案

来自 Facebook 的 Caroline !

it's Caroline from Facebook !

即使您在 Facebook 上阅读了我的回答,我也会发布此回答以表明此问题已解决.

Even if you read my answer on Facebook, I post this answer to indicate that this problem is solved.

在测试您的脚本后,我发现了问题所在.问题出在您的函数中,您在其中声明了 $name 变量:

After testing your script, I found what's wrong. The problem is located in your function, where you declare the $name variable :

$name = time();

当您同时上传多张图片时,它们都具有相同的时间戳,因此具有相同的名称!这就是为什么只发送最后一张图片!

When you upload several pics at the same time, they all have the same timestamp and thus, the same name ! That's why only the last pic is sent !

为了解决这个问题,我在您的函数中添加了一个参数,以便添加一个数字,使文件名彼此不同:

To fix this, I added an argument in your function in order to add a digit that will make the filenames different from each other :

function move_avatar($avatar,$number)

然后我将这个新变量添加到第一个 $name 变量中:

Then I added this new variable to the first $name variable :

    $name = time().$number;

最后,我在使用函数时使用了 $i 变量:

And finally, I use the $i variable when the function is used :

                $img =(!empty($_FILES['file'.$i]['size']))?move_avatar($_FILES['file'.$i],$i):'';

通过这些修改,所有上传的图片现在都有一个不同的名称,最后一位数字不同.

With these modifications, all images uploaded have now a distinct name, the last digit being different.

这篇关于多次上传,只上传最后一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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