使用 XMLHttpRequest 上傳檔案

參考自 webreflection.blogspot.com

http://webreflection.blogspot.com/2009/03/safari-4-multiple-upload-with-progress.html

name = basename($headers['X-File-Name']);
        $file->size = $headers['X-File-Size'];
        $file->content = file_get_contents("php://input");

        // if everything is ok, save the file somewhere
        if(file_put_contents($_COOKIE['path'].'/'.$file->name, $file->content))
        exit('OK.請重新載入頁面!');
    }

    // if there is an error this will be the output instead of "OK"
    exit('Error');
}
?>



電音大帝國 Alpha
        
        

        /** basic Safari 4 multiple upload example
         * @author  Andrea Giammarchi
         * @blog    WebReflection [webreflection.blogspot.com]
         */
        onload = function(){

            function size(bytes){   // simple function to show a friendly size
                var i = 0;
                while(1023 < bytes){
                    bytes /= 1024;
                    ++i;
                };
                return  i ? bytes.toFixed(2) + ["", " Kb", " Mb", " Gb", " Tb"][i] : bytes + " bytes";
            };

            // create elements
            var input = document.body.appendChild(document.createElement("input")),
                bar = document.body.appendChild(document.createElement("div")).appendChild(document.createElement("span")),
                div = document.body.appendChild(document.createElement("div"));

            // set input type as file
            input.setAttribute("type", "file");

            // enable multiple selection (note: it does not work with direct input.multiple = true assignment)
            input.setAttribute("multiple", "true");

            // auto upload on files change
            input.addEventListener("change", function(){

                // disable the input
                input.setAttribute("disabled", "true");

                sendMultipleFiles({

                    // list of files to upload
                    files:input.files,

                    // clear the container
                    onloadstart:function(){
                        div.innerHTML = "Init upload ... ";
                        bar.style.width = "0px";
                    },

                    // do something during upload ...
                    onprogress:function(rpe){
                        div.innerHTML = [
                            "上傳檔案佇列: " + this.file.fileName,
                            "目前檔案進度: " + size(rpe.loaded) + " of " + size(rpe.total),
                            "總共檔案進度: " + size(this.sent + rpe.loaded) + " of " + size(this.total)
                        ].join("
"); bar.style.width = (((this.sent + rpe.loaded) * 200 / this.total) >> 0) + "px"; }, // fired when last file has been uploaded onload:function(rpe, xhr){ div.innerHTML += ["", "Server Response: " + xhr.responseText ].join("
"); bar.style.width = "200px"; // enable the input again input.removeAttribute("disabled"); }, // if something is wrong ... (from native instance or because of size) onerror:function(){ div.innerHTML = "The file " + this.file.fileName + " is too big [" + size(this.file.fileSize) + "]"; // enable the input again input.removeAttribute("disabled"); } }); }, false); bar.parentNode.id = "progress"; }; <?php echo "
n"; echo "path is $path
n"; if( @!chdir("$path") ){ echo "error when chdir to $path
n"; $path= "."; } exec("ls", $file); $end = strrpos($path, "/"); $upper_path = substr($path, 0, $end); echo "上一層
n"; foreach( $file as $per_file ){ $string = filetype("$per_file"); if( $string == "file" && @ereg(".mp3",$per_file) ){ echo "$per_file
n"; } elseif( $string == "file" && @ereg(".MP3",$per_file) ){ echo "$per_file
n"; } elseif( $string == "file" && @ereg(".Mp3",$per_file) ){ echo "$per_file
n"; } elseif($string == "dir"){ echo "$per_file
n"; } } ?>
/** Basic upload manager for single or multiple files (Safari 4 Compatible)
 * @author  Andrea Giammarchi
 * @blog    WebReflection [webreflection.blogspot.com]
 * @license Mit Style License
 */

var sendFile = 10240000; // maximum allowed file size
                        // should be smaller or equal to the size accepted in the server for each file

// function to upload a single file via handler
sendFile = (function(toString, maxSize){
    var isFunction = function(Function){return  toString.call(Function) === "[object Function]";},
        split = "onabort.onerror.onloadstart.onprogress".split("."),
        length = split.length;
    return  function(handler){
        if(maxSize && maxSize < handler.file.fileSize){
            if(isFunction(handler.onerror))
                handler.onerror();
            return;
        };
        var xhr = new XMLHttpRequest,
            upload = xhr.upload;
        for(var
            xhr = new XMLHttpRequest,
            upload = xhr.upload,
            i = 0;
            i < length;
            i++
        )
            upload[split[i]] = (function(event){
                return  function(rpe){
                    if(isFunction(handler[event]))
                        handler[event].call(handler, rpe, xhr);
                };
            })(split[i]);
        upload.onload = function(rpe){
            if(handler.onreadystatechange === false){
                if(isFunction(handler.onload))
                    handler.onload(rpe, xhr);
            } else {
                setTimeout(function(){
                    if(xhr.readyState === 4){
                        if(isFunction(handler.onload))
                            handler.onload(rpe, xhr);
                    } else
                        setTimeout(arguments.callee, 15);
                }, 15);
            }
        };
        xhr.open("post", handler.url || "?upload=true", true);
        xhr.setRequestHeader("If-Modified-Since", "Mon, 26 Jul 1997 05:00:00 GMT");
        xhr.setRequestHeader("Cache-Control", "no-cache");
        xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        xhr.setRequestHeader("X-File-Name", handler.file.fileName);
        xhr.setRequestHeader("X-File-Size", handler.file.fileSize);
        xhr.setRequestHeader("Content-Type", "multipart/form-data");
        xhr.send(handler.file);
        return  handler;
    };
})(Object.prototype.toString, sendFile);

// function to upload multiple files via handler
function sendMultipleFiles(handler){
    var length = handler.files.length,
        i = 0,
        onload = handler.onload;
    handler.current = 0;
    handler.total = 0;
    handler.sent = 0;
    while(handler.current < length)
        handler.total += handler.files[handler.current++].fileSize;
    handler.current = 0;
    if(length){
        handler.file = handler.files[handler.current];
        sendFile(handler).onload = function(rpe, xhr){
            if(++handler.current < length){
                handler.sent += handler.files[handler.current - 1].fileSize;
                handler.file = handler.files[handler.current];
                sendFile(handler).onload = arguments.callee;
            } else if(onload) {
                handler.onload = onload;
                handler.onload(rpe, xhr);
            }
        };
    };
    return  handler;
};

發表留言