CakePHP 1.3 and Uploadifive/Uploadify - Change Upload Filename to a random string -
i have somehow implemented uploadifive in cakephp application. seems work great including uploading multiple files , inserting correct information in database.
based on following code, upload , save every file random name taking account current date or similar.
how accomplish this?
in photos controller have following function:
// function called @ every file upload. uploads file onto server // , save corresponding image name, etc, database table `photos`. function upload() { $uploaddir = '/img/uploads/photos/'; if (!empty($_files)) { debug($_files); $tempfile = $_files['filedata']['tmp_name'][0]; $uploaddir = $_server['document_root'] . $uploaddir; $targetfile = $uploaddir . $_files['filedata']['name'][0]; // validate file type $filetypes = array('jpg', 'jpeg', 'gif', 'png'); // allowed file extensions $fileparts = pathinfo($_files['filedata']['name'][0]); // validate filetype if (in_array($fileparts['extension'], $filetypes)) { // save file move_uploaded_file($tempfile,$targetfile); $_post['image'] = $_files['filedata']['name'][0]; $this->photo->create(); if ($this->photo->save($_post)) { $this->session->setflash($targetfile, 'default', array('class' => 'alert_success')); $this->redirect(array('action' => 'index')); } } else { // file type wasn't allowed //echo 'invalid file type.'; $this->session->setflash(__('the photo not saved. please, try again.', true)); } } } in view file - admin_add.ctp have added following function
$('#file_upload').uploadifive({ 'auto' : false, 'uploadscript' : '/photos/upload', 'buttontext' : 'browse files', 'method' : 'post', 'onaddqueueitem' : function(file) { this.data('uploadifive').settings.formdata = { 'photocategory_id' : $('#photophotocategoryid').val() }; } }); <input type="file" name="file_upload" id="file_upload" />
function upload() { $uploaddir = '/img/uploads/photos/'; if (!empty($_files)) { debug($_files); // $tempfile = $_files['filedata']['tmp_name'][0]; $uploaddir = $_server['document_root'] . $uploaddir; $targetfile = $uploaddir . $_files['filedata']['name'][0]; // validate file type $filetypes = array('jpg', 'jpeg', 'gif', 'png'); // allowed file extensions $fileparts = pathinfo($_files['filedata']['name'][0]); // validate filetype if (in_array($fileparts['extension'], $filetypes)) { // save file $tempfile = time()."_".basename($_files['filedata']['name'][0]); $_post['image'] = $tempfile; move_uploaded_file($tempfile,$targetfile); $this->photo->create(); if ($this->photo->save($_post)) { $this->session->setflash($targetfile, 'default', array('class' => 'alert_success')); $this->redirect(array('action' => 'index')); } } else { // file type wasn't allowed //echo 'invalid file type.'; $this->session->setflash(__('the photo not saved. please, try again.', true)); } } }
Comments
Post a Comment