我以某种方式在我的 CakePHP 应用程序中实现了 UPLOADIFIVE。一切似乎都很好,包括上传多个文件和在数据库中插入正确的信息。
根据以下代码,我想上传并保存每个带有随机名称的文件,并考虑当前日期或类似的东西。
我怎么能做到这一点?
在我的Photos Controller
我有以下功能:
// This function is called at every file upload. It uploads the file onto the server
// and save the corresponding image name, etc, to the 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 the file type
$fileTypes = array('jpg', 'jpeg', 'gif', 'png'); // Allowed file extensions
$fileParts = pathinfo($_FILES['Filedata']['name'][0]);
// Validate the filetype
if (in_array($fileParts['extension'], $fileTypes)) {
// Save the 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 {
// The file type wasn't allowed
//echo 'Invalid file type.';
$this->Session->setFlash(__('The photo could not be saved. Please, try again.', true));
}
}
}
在我的View file - admin_add.ctp
我添加了以下功能
$('#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" />