SWFUpload - 有人熟悉吗?

发布于 2024-07-24 19:33:08 字数 156 浏览 6 评论 0原文

我正在尝试 SWFUpload ( http://swfupload.org ),我想知道在 PHP 中,它是否数据仍将位于 $_FILES 数组中。 如果没有,它会去哪里?

I'm experimenting with SWFUpload ( http://swfupload.org ) and I'm wondering if, in PHP, its data will still be in the $_FILES array. If not, where does it go?

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

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

发布评论

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

评论(2

乱了心跳 2024-07-31 19:33:08

SWFUploader 使用 HTTP POST 上传文件。 因此,从 PHP 的角度来看,它与通过文件发布没有什么不同。

该文件将位于 $_FILES 中,额外的 postvars 将位于 $_POST 中。

SWFUploader uses HTTP POST to upload the files. So from PHP's perspective, it is not different than a being posted with a file.

The file will be in $_FILES and the extra postvars will be in $_POST.

离笑几人歌 2024-07-31 19:33:08

我将发布一个使用 swfupload 和 PHP 的完整示例。 我相信 swfupload 文档对初学者来说不是很有帮助,这个例子会对很多人有所帮助。 您需要下载的是以下

内容首先复制/创建所有必需的文件

  • 下载v2.2.xy核心和示例
  • 然后创建以下文件夹结构

创建一个名为swfupload的文件夹并将以下文件从2.2.xy核心复制到其中

  • swfupload.js(来自core 根文件夹)
  • swfupload swf 文件(来自 core/Flash 文件夹)

将以下文件从 2.2.x demos 文件夹复制到我们的 swfupload 文件夹

  • default.css(来自 2.2.xy 样本/demos/css)
  • 复制 2.2.xy 样本/demos/simpledemo /js 文件夹(原样)
  • 复制 2.2.xy样本/demos/images 文件夹(原样)

现在创建一个 PHP 脚本来接收 swfupload 文件夹内的文件(称为receiver.php)。 现在,您的文档根目录中有一个名为 swfupload 的文件夹,其中包含所有必需的文件。

使用 swfupload 控件创建测试 html 页面,

 <!-- file upload component  -->
    <div class="fieldset flash" id="fsUploadProgress">
        <span class="legend">Upload Queue</span>
     </div>
     <div id="divStatus">No Files Uploaded</div>
      <div>
          <span id="spanButtonPlaceHolder"></span>
      <input id="btnCancel" type="button" value="Cancel All Uploads" onclick="swfu.cancelQueue();" disabled="disabled" style="margin-left: 2px; font-size: 8pt; height: 29px;" />
      </div>

包括与 swfupload 控件交互的 javascript,

现在您必须在我们的测试 html 中包含 swfupload css 文件和所需的 javascript 文件。 我们还必须初始化并配置与 swfupload 控件交互所需的 js。

<link href="/swfupload/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/swfupload/swfupload.js"></script>
<script type="text/javascript" src="/swfupload/js/swfupload.queue.js"></script>
<script type="text/javascript" src="/swfupload/js/fileprogress.js"></script>
<script type="text/javascript" src="/swfupload/js/handlers.js"></script>


<script type="text/javascript">
            //attach events
            $(document).ready(function(){
                $("a.removeMedia").live("click", function(event){
                    event.preventDefault();
                    var docId = $(this).attr("id");
                    //remove flexi data
                    webgloo.gMedia.table.removeRow(docId);
                }) ;

                //initialize gMedia table with documentId coming from server
                webgloo.gMedia.debug = false ;
                webgloo.gMedia.table.load();

            });

            //swffileupload related javascript
            var swfu;

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

                    // Button settings
                    button_image_url: "/swfupload/images/TestImageNoText_65x29.png",
                    button_width: "65",
                    button_height: "29",
                    button_placeholder_id: "spanButtonPlaceHolder",
                    button_text: '<span class="theFont">Upload</span>',
                    button_text_style: ".theFont { font-size: 16; }",
                    button_text_left_padding: 12,
                    button_text_top_padding: 3,

                    // The event handler functions are defined in handlers.js
                    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  // Queue plugin event
                };

                swfu = new SWFUpload(settings);
            };

        </script>

请查看您需要在设置中进行的更改。 现在我们准备好运行 PHP 脚本了。 我们将点击 PHP 脚本,PHP 脚本将返回一些我们的测试 html 可以使用的响应(例如文档位置,或者 doc_id,如果您存储在数据库中,则可以返回名称、大小、mime)等。

用于服务器端处理的 PHP 脚本

如果您知道如何在 PHP 中处理文件,那么您唯一需要知道的是 swfupload 发送的文件元素名称是 Filedata。 剩下的都是细节。 通过 $_FILES 处理文件上传后,您的接收器脚本可以返回一些数据,这些数据可用于更新带有文档详细信息的 html。 swfupload 论坛中包含的 PHP 上传示例纯粹是意大利面条。 如果您想查看面向对象的模型,请查看以下文件

http://code.google.com/p/localo/source/browse/job/web/swfupload/receiver.php
http://code.google.com/p/localo/source/browse/lib/webgloo/common/Upload.php
http://code.google.com/p /localo/source/browse/lib/webgloo/job/FileUpload.php

这仅用于说明目的。 该代码将无法正常工作,因为它依赖于我的库。

连接 PHP 脚本将数据返回到包含 swfupload 控件的 html 中

这是最后一步。 服务器脚本在成功上传时返回的内容应由 swfupload javascript 处理程序之一处理以更新 html(例如将返回的文件上传 URI 存储在文档中以进一步将其提交给其他脚本等)。您可以进行连线的方式是开放的/swfupload/js/handlers.js 并在那里进行更改。

function uploadSuccess(file, serverData) {
    // The php script may return an error message etc. but the handler event for swfupload
    // client is still uploadSuccess. we have to parse data returned from server to find known/script
    // error case.
    try {
        var progress = new FileProgress(file, this.customSettings.progressTarget);
        //try parsing server data now
        var dataObj ;

        try{
            if(webgloo.gMedia.debug) {
                alert("server returned => " + serverData);
            }

            dataObj = JSON.parse(serverData);
            //process server data
            if(dataObj.error === undefined || dataObj.error != 'yes'){
                //no error object or error is not yes!
                //process document object received from server
                webgloo.gMedia.table.addRow(dataObj.document.uuid, dataObj.document.originalName);
                progress.setComplete();
                progress.setStatus(dataObj.message);
                progress.toggleCancel(false);
            }else {
                //known error
                progress.setStatus("Error: " + dataObj.message);
            }

        } catch(ex) {
            //we need to gaurd against JSON parsing errors as well
            progress.setStatus("Error: " + ex.toString());
        }


    } catch (ex) {
        this.debug(ex);
    }
}

您可以在此处热连您自己的 javascript。 有关示例(我正在更新文档表并将返回的文档元数据存储为表单文档中的 json 字符串),请参阅此处:

http://code.google.com/p/localo/source/browse/job/web/js/main.js

我希望这些信息足以帮助您开始使用 swfupload。 快乐编码:)

I am going to post a complete example using swfupload and PHP. I believe swfupload documents are not very helpful for beginners and this example would help many people. what you need to download is following

Copy/create all necessary files first

  • Download v2.2.x.y core and samples
  • Then create the following folder structure

create a folder called swfupload and copy following files from 2.2.x.y core into it

  • swfupload.js (from core root folder)
  • swfupload swf file (from core/Flash folder)

copy following files from 2.2.x demos folder into our swfupload folder

  • default.css (from 2.2.x.y samples/demos/css)
  • copy 2.2.x.y samples/demos/simpledemo/js folder here (as it is)
  • copy 2.2.x.y samples/demos/images folder here (as it is)

Now create a PHP script to receive the file (call it receiver.php) inside swfupload folder. Now you have a folder called swfupload inside your document root that contains all the required files.

Create test html page with swfupload control

 <!-- file upload component  -->
    <div class="fieldset flash" id="fsUploadProgress">
        <span class="legend">Upload Queue</span>
     </div>
     <div id="divStatus">No Files Uploaded</div>
      <div>
          <span id="spanButtonPlaceHolder"></span>
      <input id="btnCancel" type="button" value="Cancel All Uploads" onclick="swfu.cancelQueue();" disabled="disabled" style="margin-left: 2px; font-size: 8pt; height: 29px;" />
      </div>

include javascript to interact with swfupload control

now you have to include swfupload css file and required javascript files inside our test html. we also have to initialize and configure the js required to interact with swfupload control.

<link href="/swfupload/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/swfupload/swfupload.js"></script>
<script type="text/javascript" src="/swfupload/js/swfupload.queue.js"></script>
<script type="text/javascript" src="/swfupload/js/fileprogress.js"></script>
<script type="text/javascript" src="/swfupload/js/handlers.js"></script>


<script type="text/javascript">
            //attach events
            $(document).ready(function(){
                $("a.removeMedia").live("click", function(event){
                    event.preventDefault();
                    var docId = $(this).attr("id");
                    //remove flexi data
                    webgloo.gMedia.table.removeRow(docId);
                }) ;

                //initialize gMedia table with documentId coming from server
                webgloo.gMedia.debug = false ;
                webgloo.gMedia.table.load();

            });

            //swffileupload related javascript
            var swfu;

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

                    // Button settings
                    button_image_url: "/swfupload/images/TestImageNoText_65x29.png",
                    button_width: "65",
                    button_height: "29",
                    button_placeholder_id: "spanButtonPlaceHolder",
                    button_text: '<span class="theFont">Upload</span>',
                    button_text_style: ".theFont { font-size: 16; }",
                    button_text_left_padding: 12,
                    button_text_top_padding: 3,

                    // The event handler functions are defined in handlers.js
                    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  // Queue plugin event
                };

                swfu = new SWFUpload(settings);
            };

        </script>

Please see the changes that you need to do in settings. Now we are set to hit the PHP script. we will hit the PHP script and PHP script will return some response that our test html can use (things like doc location, or doc_id if you are storing in a DB, name,size,mime) etc. can be returned.

PHP script for server side processing

if you know how to handle files in PHP then the only thing you need to know is that the file element name sent by swfupload is Filedata. rest all is details. After processing your file upload via $_FILES , your receiver script can return some data that can be used to update your html with document details. The PHP upload sample included in swfupload forum is pure spaghetti. if you want to look @ an object oriented model, look @ following files

http://code.google.com/p/localo/source/browse/job/web/swfupload/receiver.php
http://code.google.com/p/localo/source/browse/lib/webgloo/common/Upload.php
http://code.google.com/p/localo/source/browse/lib/webgloo/job/FileUpload.php

That is for illustrative purpose only. The code will not work as it is as it has dependencies on my library.

wiring PHP script return data back into html containing swfupload control

This is the last step. what the server script returns on a successful upload should be processed by one of swfupload javascript handlers to update the html (say storing returned file upload URI in document to further submit it to some other script etc.) The way you can do wiring is open up /swfupload/js/handlers.js and make changes there.

function uploadSuccess(file, serverData) {
    // The php script may return an error message etc. but the handler event for swfupload
    // client is still uploadSuccess. we have to parse data returned from server to find known/script
    // error case.
    try {
        var progress = new FileProgress(file, this.customSettings.progressTarget);
        //try parsing server data now
        var dataObj ;

        try{
            if(webgloo.gMedia.debug) {
                alert("server returned => " + serverData);
            }

            dataObj = JSON.parse(serverData);
            //process server data
            if(dataObj.error === undefined || dataObj.error != 'yes'){
                //no error object or error is not yes!
                //process document object received from server
                webgloo.gMedia.table.addRow(dataObj.document.uuid, dataObj.document.originalName);
                progress.setComplete();
                progress.setStatus(dataObj.message);
                progress.toggleCancel(false);
            }else {
                //known error
                progress.setStatus("Error: " + dataObj.message);
            }

        } catch(ex) {
            //we need to gaurd against JSON parsing errors as well
            progress.setStatus("Error: " + ex.toString());
        }


    } catch (ex) {
        this.debug(ex);
    }
}

You can hot wire your own javascript here. For a sample (I am updating a table of documents and storing returned documents meta data as a json string in form document) please see here:

http://code.google.com/p/localo/source/browse/job/web/js/main.js

I hope this is enough information to get you started with swfupload. Happy coding :)

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