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
?