开发者

Uploading data into the $_FILES without a form

开发者 https://www.devze.com 2023-02-28 01:40 出处:网络
Alright, so a little preface: I\'ve been working on adding drag and drop file uploading to the course management system called Moodle (specifically 2.0.1). This version of Moodle uses the YUI3 framewo

Alright, so a little preface: I've been working on adding drag and drop file uploading to the course management system called Moodle (specifically 2.0.1). This version of Moodle uses the YUI3 framework and uploads the form data with the file to be uploaded and the save-as name through an io-upload-iframe. The file is stored in the super global $_FILES until the filesystem is sent the relevant data on where to store it permanently.

The trouble I'm having is that instead of using YUI3's drag and drop features (Which, from a cursory look at their website is not the kind of drag and drop I need anyways) I'm using the native HTML5 drag and drop code. This seems to work in most major browsers (I haven't had the time to test much, and it's outside the scope of this project). The trouble I'm having is that this design of DND immediately gives you a file from the Event object in javascript. I cou开发者_运维技巧ld send this file object off to wherever I want, but the filesystem is only designed to handle variables temporarily stored in the $_FILES variable. I've not been able to find any easy way of getting this file stored there, unfortunately. I could to an HTTP request of various forms (either one of YUI3's special Y.io() requests or an XHR), but this requires a lot of duplicated code from the original source code.

Anybody have some suggestions?


Hard to tell what your problem is. But whatever your server or filesystem is, it has nothing to do with the temporarity of the $_FILES array.

When you receive the DND event, and YUI subsequently sends the file, then you will receive some data either in $_FILES or in $_POST. If so, just use move_uploaded_file or file_put_contents and store it elsewhere.

Assign that moved file a md5() hash as name, and have that returned as file identifier for your AJAX-DND upload request.

You can then use that hash id in your Javascript code, and refer to the already uploaded image file by this reference. Should your app initiate a normal form request after the drag'n'dropping, then you just include the collected image reference ids. And thus your server-side code can associate it again.

You could even reconstruct the $_FILES array if you want to:

foreach ((array)$_POST["prev_image_ids"] as $md5) {
    $md5 = basename($md5);
    $_FILES["image"][] = array(
        "tmp_name" => $fn="./already-uploaded/$md5",
        "size" => filesize($fn), "type"=>"image/whatever",
        "name" => "unknown.jpg", "error"=>UPLOAD_ERR_OK,
    );   // you could store away those original attributes too
}
0

精彩评论

暂无评论...
验证码 换一张
取 消