dongming6201 2013-01-07 13:06
浏览 51

html5文件上传api和laravel

I have created a HTML5 drag and drop file upload for my laravel app, however I having some trouble setting the filename, as variable that is available to all my controller.

The process the user takes to upload a file, is they fill out some member information, and on submission of the form, a new step is loaded in via ajax, this step is the HTML5 drag and drop file upload.

The problem is not the file upload, that functionality works fine, my problem is that the upload action (below),

public function action_upload()
{
    $uploadDir = "public/uploads/";
    $file = Input::get('value');
    $name = Input::get('name');

    //Get the mime
    $getMime = explode(".", $name);
    $mime = end($getMime);

    //seperate the data out
    $fileData = explode(',', $file);

    //Encode it correctly
    $encodedData = str_replace(' ', '+', $fileData[1]);
    $decodedData = base64_decode($encodedData);

    //create a random name, will help with not overwriting filesystems.
    $this->randomName = substr_replace(sha1(microtime(true)), '', 12).'.'.$mime;

    if(file_put_contents($uploadDir.$this->randomName, $decodedData)) {
        echo $this->randomName.":uploaded successfully";
    } else {
        echo "Something went wrong. Check that the upload is not corrupted";
    }
}

As you can see I am setting a variable randomName, after the ajax/HTML5 upload is complete the following code get called,

public function action_source()
{
    if(Input::get('step') == "step3") {
        //echo $this->randomName;
        return json_encode(array('next' => $this->data['url']."/project/launch", 'file' => $this->randomName));
    }
    return View::make('project.source', $this->data);
}

However the in the JSON object that is return, file is returned as NULL and I dont understand why, at the start of my class i have public $randomName so I should be able to access it via, $this->randomName;

And the order of the function calls if correct,

action_upload() > action_source()

Why can I not access $randomName?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集