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

发布于 2024-11-06 20:25:13 字数 113 浏览 5 评论 0原文

我需要一个由纯 jquery/Javascript 和 php5 创建的多文件上传器。不使用任何 flash 文件。如果有人有相关代码,请与我分享。我尝试使用 File API,但它不支持 IE 和 Opera。

I need a multiple file uploader created by pure jquery/Javascript and php5. Not use any flash files. If any body have code for that please share with me. I tried with File API but its not supporting IE and Opera.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮是我掰弯的 2024-11-13 20:25:14

您可以在客户端使用 blueimp jQuery 文件上传 来进行多个文件上传。有多种选择,可定制。

对于 PHP 方面,您可能对一些关于做什么的想法感兴趣(只是我的图像上传表单的一些片段,因此这里没有定义一些变量,如 $log 或 $confImages,但您可以理解):

            //С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;

You can use blueimp jQuery File Upload for the client side to have multiple file uploads. Has many options, is customizable.

For a PHP side you may be interested in some ideas on what to do (just some snippet for my upload form for images, so some variables are not defined here like $log or $confImages, but you can get the idea):

            //С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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文