我在某種程度上在我的CakePHP應用程序中實現了UPLOADIFIVE。一切似乎都很好,包括上傳多個文件並在數據庫中插入正確的信息。CakePHP 1.3和Uploadifive/Uploadify - 將上傳文件名更改爲隨機字符串
根據下面的代碼,我想上傳並保存每個文件,並帶有一個隨機名,以考慮當前日期或類似的事情。
我怎麼能做到這一點?
在我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" />
Chetanspeed,謝謝你的回答。我無法按照您的建議更改$ tempFile,因爲它包含上傳文件的實際位置(/tmp/file.jpg)。然而,基於你的建議,我能夠生成一個隨機字符串並將其添加到$ targetFile。感謝您的專業知識......請根據您的建議查看我爲解決方案添加的答案......信任您! –