简体   繁体   English

在多个文件上传中管理php会话

[英]Managing php sessions on multiple file uploads

I'm working in a multiple image upload social web app. 我正在使用多张图片上传社交网络应用程序。 I'm assigning a $_session['file_id'] to each uploaded file as an array index (file id comes from db). 我为每个上传的文件分配一个$_session['file_id']作为数组索引(文件ID来自db)。 But I only want users to upload a maximum of 4 images per post. 但我只希望用户每个帖子最多上传4张图片。 So I'm counting the images.length before upload, and if it's == 4 , alerting the user he can't upload more files at the same time, else, the normal upload happens. 因此,我在上载之前先计算images.length ,如果== 4 ,则警告用户他不能同时上传更多文件,否则会发生正常的上传。 For security purposes I have an unset($_session['file_id']) for making sure the 4 files won't be uploaded on errors, or user changes on javascript (a for loop on every upload, to count $_session['file_id']) . 为了安全起见,我有一个unset($_session['file_id'])来确保不会因错误而上传这4个文件,或者确保用户不会在javascript上进行更改(每次上载都进行for循环),以计算$_session['file_id']) The problem I'm having is that the js itself ain't enought to make sure the file ain't uploaded 4 times, I guess ajax upload stay on queue, and check for file number before the 4th file is uploaded. 我遇到的问题是js本身不足以确保文件没有上传4次,我猜ajax上传仍在队列中,并在上传第4个文件之前检查文件号。 Is there any other approach I can use direct on php, remembering I can't unset files for common users (4 files are already upload and waiting for send button), but I need plus to unset files if user reflesh, or quit the page? 还有什么其他方法可以直接在php上使用,请记住我无法为普通用户取消设置文件(4个文件已经上传并等待发送按钮),但是如果用户刷新或退出页面,我需要加取消设置文件? Thank your for your time. 感谢您的宝贵时间。

JavaScript JavaScript的

var checkfilecount = 0;
$('#file').change(function() {
    checkfilecount++;
    checkfile = $("#images).length;
    if (checkfile < 4 && checkfilecount < 4) {
        $("#filesend").submit();
    }
    else {
        setTimeout(function(){
            alert("Sorry, You can`t upload too many files at once")
        },3000)
    }
})  

and on #postsend: 并在#postsend上:

checkfilecount = 0;

PHP PHP

if (isset($_SESSION['files_id'])) {
    $counting = 0; 
    foreach($_SESSION['files_id'] as $key => $val)
    {
        $counting++;
        if ($counting == 4) {
            unset($_SESSION['files_id']);       
            exit("error on session array"); 
        }   
    }   
}

Assign a random classname to the <input> s for example: <input>分配一个随机的类名,例如:

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file[]" class="fileuploadCounter">
    <input type="file" name="file[]" class="fileuploadCounter">
    <input type="file" name="file[]" class="fileuploadCounter">
    <input type="file" name="file[]" class="fileuploadCounter">
</form>

After init, $('.fileuploadCounter').length() will return the amount of elements with that class, in this example, four. 初始化之后, $('.fileuploadCounter').length()将返回该类的元素数量,在本示例中为4。 See the jQuery documentation . 请参阅jQuery文档


Old answer underneath 下面的旧答案

Change the name on the <input> tags to images[] , and count $_POST['images'] server side. <input>标记上的名称更改为images[] ,并计数$_POST['images']服务器端。

Eg: 例如:

 <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="file[]"> <input type="file" name="file[]"> <input type="file" name="file[]"> <input type="file" name="file[]"> </form> <?php if($_SERVER['REQUEST_METHOD'] == "POST"){ if(isset($_FILES['file'])){ // Because we added [] in the name tag and they match names, $_FILES['file'] is now an array containing the images. if(count($_FILES['file']) > 4){ // More than 4 files } else { // Less then four! foreach($_FILES['file'] as $file){ move_uploaded_file($file["file"]["tmp_name"], "uploads/" . $files["file"]["name"]); } } } } ?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM