0

我以某种方式在我的 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" />
4

2 回答 2

1
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

     $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 {
        // The file type wasn't allowed
        //echo 'Invalid file type.';
        $this->Session->setFlash(__('The photo could not be saved. Please, try again.', true));
    }
   }
   } 
于 2012-06-05T05:50:40.043 回答
0

Chetanspeed非常感谢您迅速提供帮助。根据他的解决方案,我能够使其工作。下面是对我有用的代码,它与Chetanspeed

function upload() {
    $uploadDir = '/img/uploads/photos/';

    if (!empty($_FILES)) {

        $tempFile   = $_FILES['Filedata']['tmp_name'][0]; // Temp file should not be changed since it contains the physical location of the file /tmp/file.jpg
        $uploadDir  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;
        $randomString = time(); // Save this random string to a variable
        $targetFile = $uploadDir . $randomString."_".basename($_FILES['Filedata']['name'][0]); //randomString is added to target...

        // 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)) {

                            //image name posted to database containing the randomString generated from time...thanks Chetanspeed
            $_POST['image'] = $randomString."_".basename($_FILES['Filedata']['name'][0]);

            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 {
            // The file type wasn't allowed
            //echo 'Invalid file type.';
            $this->Session->setFlash(__('The photo could not be saved. Please, try again.', true));
        }
    }
}
于 2012-06-05T13:26:06.487 回答