拖动;下载上传

时间:2012-09-24 18:28:34

标签: javascript

我正在尝试制作一个Drag& Drop uploader。我做了拖放部分, 但是现在我必须制作PHP并且我被困住了,不知道该怎么做。 我的问题是,我不能使用move_uploaded_file();因为没有

<input type="file">

已提交。 有没有办法解决这个问题以及我应该使用/做什么?

我不是要求一个完整的代码示例,而只是了解我的下一步是什么。

2 个答案:

答案 0 :(得分:1)

调查this implementation以获得一个想法:

/**
 * Save the file to the specified path
 * @return boolean TRUE on success
 */
function save($path) {    
    $input = fopen("php://input", "r");
    $temp = tmpfile();
    $realSize = stream_copy_to_stream($input, $temp);
    fclose($input);

    if ($realSize != $this->getSize()){            
        return false;
    }

    $target = fopen($path, "w");        
    fseek($temp, 0, SEEK_SET);
    stream_copy_to_stream($temp, $target);
    fclose($target);

    return true;
}

我在我的一个项目中使用了这样的实现:

$savePath = '/home/user/uploaded_files/picture.png';

if(!isset($_FILES['qqfile']['tmp_name'])) {
    $input = fopen('php://input', 'r');
    $temp = tmpfile();
    stream_copy_to_stream($input, $temp);
    fclose($input);
    $target = fopen($savePath, 'w');
    fseek($temp, 0, SEEK_SET);
    stream_copy_to_stream($temp, $target);
    fclose($target);
} else {
    move_uploaded_file($_FILES['qqfile']['tmp_name'], $savePath);
}

答案 1 :(得分:-2)

使用PHP,您很可能不得不实际提交表单,即使它是通过框架中的javscript完成的,这样它实际上并不会移动您的页面。重新开始。

提交真实表单后,您能够使用move_uploaded_file(),只是不要忘记enctype="multipart/form-data"标记中的form属性!