首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用纯JQuery和PHP5的多文件上传器?

使用纯JQuery和PHP5的多文件上传器?
EN

Stack Overflow用户
提问于 2011-05-12 13:36:47
回答 1查看 3.1K关注 0票数 3

我需要一个由纯jquery/Javascript和php5创建的多文件上传器。不要使用任何闪存文件。如果任何机构有这方面的代码,请与我分享。我尝试使用文件API,但它不支持IE和Opera。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-12 13:40:54

客户端可以使用blueimp jQuery File Upload实现多个文件上传。有许多选项,是可定制的。

对于PHP端,你可能会对一些想法感兴趣(只是我上传图像表单的一些片段,所以这里没有定义一些变量,比如$log或$confImages,但你可以理解这个想法):

代码语言:javascript
复制
            //Сheck that we have a file
            if (empty($uploaded_image))
            return false;
        if ($uploaded_image['error']) {
            $log->LogError($uploaded_image['error'] . ": Error uploading file.");
            return false;   
        }

        list($dirname, $basename, $extension, $filename) = array_values(pathinfo($uploaded_image['name'])); //Extract variables: dirname, basename, extension, filename
        //Normalize file name. I may suggest using http://cubiq.org/the-perfect-php-clean-url-generator than this
        $filename = iconv("utf-8","ascii//TRANSLIT",$filename);
            $allowedExt = array("jpg","jpeg","gif","png");
        $extension = strtolower($extension);

        //Check if the image has allowed type
        if (empty($extension) || !in_array($extension, $allowedExt)) {
            $log->LogError("invalid_extension: Not allowed to upload file ".(empty($extension) ? "without extension" : "with extension $extension").". Allowed Extensions: ". implode(",",$allowedExt));
            return false;   
        }

            //Generate some random string in case if such file exists on server
        $postfix = substr( md5(uniqid(rand(),1)), 3, 5 );
        $newFile = $_SERVER['DOCUMENT_ROOT'].$upload_folder."/".$filename.'_'.$postfix.'.'.$extension; //new filename with appended random string to original filename);

        //Check if the file with the same name is already exists on the server
        if (file_exists($newFile)) {
            $log->LogError("file_exists: File couldn't be uploaded, because such filename exist ".basename($newFile)." jau eksistē");
        }

        //Attempt to move the uploaded file to it's new place
        if (move_uploaded_file($uploaded_image['tmp_name'],$newFile)) {
            //I do some resizing also using http://phpthumb.gxdlabs.com/
            $convertedimage = PhpThumbFactory::create($newFile, array("jpegQuality" => 80));
            if (isset($confImages["max_width"]) || isset($confImages["max_height"])) 
                $convertedimage->resize($confImages["max_width"], $confImages["max_height"]);
            $convertedimage->save( $newFile );

            //Save image path to database
            ...

            return $image;
        } else {
            $log->LogError("Couldn't copy file from temp folder " . $uploaded_image['tmp_name'] . " to location " . $newFile);
        }       

        return false;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5973718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档