Home > php教程 > php手册 > 使用SWFUpload插件上传文件,swfupload上传文件

使用SWFUpload插件上传文件,swfupload上传文件

WBOY
Release: 2016-06-13 08:57:05
Original
1188 people have browsed it

使用SWFUpload插件上传文件,swfupload上传文件

演示代码由两部分组成,包括前台文件和后台文件:

1.前台文件index.html

<span><!</span><span>DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span>></span>
<span><</span><span>html </span><span>xmlns</span><span>="http://www.w3.org/1999/xhtml"</span> <span>></span>
<span><</span><span>meta </span><span>http-equiv</span><span>="Content-Type"</span><span> content</span><span>="text/html; charset=gb2312"</span> <span>/></span>
<span><</span><span>head</span><span>></span>
<span><</span><span>title</span><span>></span>SWFUpload<span></</span><span>title</span><span>></span>
<span><</span><span>link </span><span>href</span><span>="css/default.css"</span><span> rel</span><span>="stylesheet"</span><span> type</span><span>="text/css"</span> <span>/></span>
<span><!--</span><span>Swfupload插件begin</span><span>--></span>
<span><</span><span>script </span><span>type</span><span>="text/javascript"</span><span> src</span><span>="swfupload/swfupload.js"</span><span>></</span><span>script</span><span>></span>
<span><</span><span>script </span><span>type</span><span>="text/javascript"</span><span> src</span><span>="js/swfupload.queue.js"</span><span>></</span><span>script</span><span>></span>
<span><</span><span>script </span><span>type</span><span>="text/javascript"</span><span> src</span><span>="js/fileprogress.js"</span><span>></</span><span>script</span><span>></span>
<span><</span><span>script </span><span>type</span><span>="text/javascript"</span><span> src</span><span>="js/handlers.js"</span><span>></</span><span>script</span><span>></span>
<span><!--</span><span>Swfupload插件end</span><span>--></span>
<span><</span><span>script </span><span>type</span><span>="text/javascript"</span><span>></span>
        <span>var</span><span> swfu;

        window.onload </span><span>=</span> <span>function</span><span>() {
            </span><span>var</span><span> settings </span><span>=</span><span> {
                flash_url : </span><span>"</span><span>swfupload/swfupload.swf</span><span>"</span><span>,
                upload_url: </span><span>"</span><span>upload.php</span><span>"</span><span>,    </span><span>//</span><span> 后台文件</span>
<span>                post_params: {</span><span>"</span><span>PHPSESSID</span><span>"</span><span> : </span><span>"</span><span><?php echo session_id(); ?></span><span>"</span><span>},
                file_size_limit : </span><span>"</span><span>100 MB</span><span>"</span><span>,
                file_types : </span><span>"</span><span>*.*</span><span>"</span><span>,
                file_types_description : </span><span>"</span><span>All Files</span><span>"</span><span>,
                file_upload_limit : </span><span>100</span><span>,
                file_queue_limit : </span><span>0</span><span>,
                custom_settings : {
                    progressTarget : </span><span>"</span><span>fsUploadProgress</span><span>"</span><span>,
                    cancelButtonId : </span><span>"</span><span>btnCancel</span><span>"</span><span>
                },
                debug: </span><span>false</span><span>,

                </span><span>//</span><span> 按钮设置</span>
<span>                button_image_url: </span><span>"</span><span>images/TestImageNoText_65x29.png</span><span>"</span><span>,    </span><span>//</span><span> Flash样式图片文件</span>
<span>                button_width: </span><span>"</span><span>65</span><span>"</span><span>,
                button_height: </span><span>"</span><span>29</span><span>"</span><span>,
                button_placeholder_id: </span><span>"</span><span>spanButtonPlaceHolder</span><span>"</span><span>,
                button_text: </span><span>'</span><span><span class="theFont">浏览</span></span><span>'</span><span>,
                button_text_style: </span><span>"</span><span>.theFont { font-size: 16; }</span><span>"</span><span>,
                button_text_left_padding: </span><span>12</span><span>,
                button_text_top_padding: </span><span>3</span><span>,
                
                </span><span>//</span><span> 句柄设置</span>
<span>                file_queued_handler : fileQueued,
                file_queue_error_handler : fileQueueError,
                file_dialog_complete_handler : fileDialogComplete,
                upload_start_handler : uploadStart,
                upload_progress_handler : uploadProgress,
                upload_error_handler : uploadError,
                upload_success_handler : uploadSuccess,
                upload_complete_handler : uploadComplete,
                queue_complete_handler : queueComplete
            };

            swfu </span><span>=</span> <span>new</span><span> SWFUpload(settings);
         };
    </span><span></</span><span>script</span><span>></span>
<span></</span><span>head</span><span>></span>
<span><</span><span>body</span><span>></span>
<span><</span><span>div </span><span>id</span><span>="header"</span><span>></span>
    <span><</span><span>h1 </span><span>id</span><span>="logo"</span><span>><</span><span>a </span><span>href</span><span>="/"</span><span>></span>SWFUpload<span></</span><span>a</span><span>></</span><span>h1</span><span>></span>
    <span><</span><span>div </span><span>id</span><span>="version"</span><span>></span>v2.2.0<span></</span><span>div</span><span>></span>
<span></</span><span>div</span><span>></span>

<span><</span><span>div </span><span>id</span><span>="content"</span><span>></span>
    <span><</span><span>form </span><span>id</span><span>="form1"</span><span> action</span><span>="index.php"</span><span> method</span><span>="post"</span><span> enctype</span><span>="multipart/form-data"</span><span>></span>
        <span><</span><span>p</span><span>></span>点击&ldquo;浏览&rdquo;按钮,选择您要上传的文档文件后,系统将自动上传并在完成后提示您。<span></</span><span>p</span><span>></span>
        <span><</span><span>p</span><span>></span>请勿上传包含中文文件名的文件!<span></</span><span>p</span><span>></span>
        <span><</span><span>div </span><span>class</span><span>="fieldset flash"</span><span> id</span><span>="fsUploadProgress"</span><span>></span>
            <span><</span><span>span </span><span>class</span><span>="legend"</span><span>></span>快速上传<span></</span><span>span</span><span>></span>
      <span></</span><span>div</span><span>></span>
        <span><</span><span>div </span><span>id</span><span>="divStatus"</span><span>></span>0 个文件已上传<span></</span><span>div</span><span>></span>
            <span><</span><span>div</span><span>></span>
                <span><</span><span>span </span><span>id</span><span>="spanButtonPlaceHolder"</span><span>></</span><span>span</span><span>></span>
                <span><</span><span>input </span><span>id</span><span>="btnCancel"</span><span> type</span><span>="button"</span><span> value</span><span>="取消所有上传"</span><span> onclick</span><span>="swfu.cancelQueue();"</span><span> disabled</span><span>="disabled"</span><span> style</span><span>="margin-left: 2px; font-size: 8pt; height: 29px;"</span> <span>/></span>
            <span></</span><span>div</span><span>></span>

    <span></</span><span>form</span><span>></span>
<span></</span><span>div</span><span>></span>
<span><</span><span>div </span><span>align</span><span>="center"</span><span>></span>Hanization By <span><</span><span>a </span><span>href</span><span>="http://imll.net"</span><span> target</span><span>="_blank"</span><span>></span>Leo.C,<span></</span><span>a</span><span>></span>
<span></</span><span>div</span><span>></span>
<span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span>
Copy after login


2.后台文件upload.php

<?<span>php
</span>
<span>//</span><span> 传递session值(由于Flash与session不兼容,只能通过参数传递获取)</span>
    <span>if</span> (<span>isset</span>(<span>$_POST</span>["PHPSESSID"<span>])) {
        </span><span>session_id</span>(<span>$_POST</span>["PHPSESSID"<span>]);
    } </span><span>else</span> <span>if</span> (<span>isset</span>(<span>$_GET</span>["PHPSESSID"<span>])) {
        </span><span>session_id</span>(<span>$_GET</span>["PHPSESSID"<span>]);
    }

    </span><span>session_start</span><span>();

</span><span>//</span><span> 设置POST最大值</span>
    <span>$POST_MAX_SIZE</span> = <span>ini_get</span>('post_max_size'<span>);
    </span><span>$unit</span> = <span>strtoupper</span>(<span>substr</span>(<span>$POST_MAX_SIZE</span>, -1<span>));
    </span><span>$multiplier</span> = (<span>$unit</span> == 'M' ? 1048576 : (<span>$unit</span> == 'K' ? 1024 : (<span>$unit</span> == 'G' ? 1073741824 : 1<span>)));

    </span><span>if</span> ((int)<span>$_SERVER</span>['CONTENT_LENGTH'] > <span>$multiplier</span>*(int)<span>$POST_MAX_SIZE</span> && <span>$POST_MAX_SIZE</span><span>) {
        </span><span>header</span>("HTTP/1.1 500 Internal Server Error"<span>);
        </span><span>echo</span> "POST exceeded maximum allowed size."<span>;
        </span><span>exit</span>(0<span>);
    }

</span><span>//</span><span> 基本设置</span>
    <span>$save_path</span> = <span>getcwd</span>() . "/file/";                           <span>//</span><span> 文件上传位置</span>
    <span>$upload_name</span> = "Filedata"<span>;
    </span><span>$max_file_size_in_bytes</span> = 2147483647;                         <span>//</span><span> 2GB</span>
    <span>$extension_whitelist</span> = <span>array</span>("doc", "txt", "jpg", "gif", "png");    <span>//</span><span> 允许文件类型</span>
    <span>$valid_chars_regex</span> = '.A-Z0-9_ !@#$%^&()+={}\[\]\',~`-';            <span>//</span><span> 文件名规则
    
// 其他变量</span>
    <span>$MAX_FILENAME_LENGTH</span> = 260<span>;
    </span><span>$file_name</span> = ""<span>;
    </span><span>$file_extension</span> = ""<span>;
    </span><span>$uploadErrors</span> = <span>array</span><span>(
        </span>0=>"文件上传成功",
        1=>"上传的文件超过了 php.ini 文件中的 upload_max_filesize directive 里的设置",
        2=>"上传的文件超过了 HTML form 文件中的 MAX_FILE_SIZE directive 里的设置",
        3=>"上传的文件仅为部分文件",
        4=>"没有文件上传",
        6=>"缺少临时文件夹"<span>
    );


</span><span>//</span><span> 检测文件是否上传正确</span>
    <span>if</span> (!<span>isset</span>(<span>$_FILES</span>[<span>$upload_name</span><span>])) {
        HandleError(</span>"No upload found in \$_FILES for " . <span>$upload_name</span><span>);
        </span><span>exit</span>(0<span>);
    } </span><span>else</span> <span>if</span> (<span>isset</span>(<span>$_FILES</span>[<span>$upload_name</span>]["error"]) && <span>$_FILES</span>[<span>$upload_name</span>]["error"] != 0<span>) {
        HandleError(</span><span>$uploadErrors</span>[<span>$_FILES</span>[<span>$upload_name</span>]["error"<span>]]);
        </span><span>exit</span>(0<span>);
    } </span><span>else</span> <span>if</span> (!<span>isset</span>(<span>$_FILES</span>[<span>$upload_name</span>]["tmp_name"]) || !@<span>is_uploaded_file</span>(<span>$_FILES</span>[<span>$upload_name</span>]["tmp_name"<span>])) {
        HandleError(</span>"Upload failed is_uploaded_file test."<span>);
        </span><span>exit</span>(0<span>);
    } </span><span>else</span> <span>if</span> (!<span>isset</span>(<span>$_FILES</span>[<span>$upload_name</span>]['name'<span>])) {
        HandleError(</span>"File has no name."<span>);
        </span><span>exit</span>(0<span>);
    }
    
</span><span>// 检测文件尺寸</span>
    <span>$file_size</span> = @<span>filesize</span>(<span>$_FILES</span>[<span>$upload_name</span>]["tmp_name"<span>]);
    </span><span>if</span> (!<span>$file_size</span> || <span>$file_size</span> > <span>$max_file_size_in_bytes</span><span>) {
        HandleError(</span>"File exceeds the maximum allowed size"<span>);
        </span><span>exit</span>(0<span>);
    }
    
    </span><span>if</span> (<span>$file_size</span> <= 0<span>) {
        HandleError(</span>"File size outside allowed lower bound"<span>);
        </span><span>exit</span>(0<span>);
    }


</span><span>//</span><span> 检测文件名字为空</span>
    <span>$file_name</span> = <span>preg_replace</span>('/[^'.<span>$valid_chars_regex</span>.']|\.+$/i', "", <span>basename</span>(<span>$_FILES</span>[<span>$upload_name</span>]['name'<span>]));
    </span><span>if</span> (<span>strlen</span>(<span>$file_name</span>) == 0 || <span>strlen</span>(<span>$file_name</span>) > <span>$MAX_FILENAME_LENGTH</span><span>) {
        HandleError(</span>"Invalid file name"<span>);
        </span><span>exit</span>(0<span>);
    }


</span><span>//</span><span> 检测重名文件</span>
    <span>if</span> (<span>file_exists</span>(<span>$save_path</span> . <span>$file_name</span><span>)) {
        HandleError(</span>"File with this name already exists"<span>);
        </span><span>exit</span>(0<span>);
    }

</span><span>//</span><span> 检测后缀名</span>
    <span>$path_info</span> = <span>pathinfo</span>(<span>$_FILES</span>[<span>$upload_name</span>]['name'<span>]);
    </span><span>$file_extension</span> = <span>$path_info</span>["extension"<span>];
    </span><span>$is_valid_extension</span> = <span>false</span><span>;
    </span><span>foreach</span> (<span>$extension_whitelist</span> <span>as</span> <span>$extension</span><span>) {
        </span><span>if</span> (<span>strcasecmp</span>(<span>$file_extension</span>, <span>$extension</span>) == 0<span>) {
            </span><span>$is_valid_extension</span> = <span>true</span><span>;
            </span><span>break</span><span>;
        }
    }
    </span><span>if</span> (!<span>$is_valid_extension</span><span>) {
        HandleError(</span>"Invalid file extension"<span>);
        </span><span>exit</span>(0<span>);
    }</span>


<span>//</span><span> 保存文件</span>
    <span>if</span> (!@<span>move_uploaded_file</span>(<span>$_FILES</span>[<span>$upload_name</span>]["tmp_name"], <span>$save_path</span>.<span>$file_name</span><span>)) {
        HandleError(</span>"文件无法保存."<span>);
        </span><span>exit</span>(0<span>);
    }

</span><span>//</span><span> 成功输出</span>

    <span>echo</span> "File Received"<span>;
    </span><span>exit</span>(0<span>);<br /></span>
<span>function</span> HandleError(<span>$message</span><span>) {
    </span><span>header</span>("HTTP/1.1 500 Internal Server Error"<span>);
    </span><span>echo</span> <span>$message</span><span>;
}
</span>?>
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template