【问题标题】:CakePHP 1.3 and Uploadifive/Uploadify - Change Upload Filename to a random stringCakePHP 1.3 和 Uploadifive/Uploadify - 将上传文件名更改为随机字符串
【发布时间】:2012-06-09 03:10:56
【问题描述】:

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

【问题讨论】:

    标签: cakephp cakephp-1.3


    【解决方案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));
        }
       }
       } 
    

    【讨论】:

    • Chetanspeed,非常感谢您的回答。我无法按照您的建议更改 $tempFile,因为它包含上传文件的物理位置(/tmp/file.jpg)。但是,根据您的建议,我能够生成一个随机字符串并将其添加到 $targetFile。感谢您的专业知识...查看我根据您的建议为有效的解决方案添加的答案...感谢您!
    【解决方案2】:

    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));
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-29
      • 1970-01-01
      • 2011-04-22
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-24
      • 1970-01-01
      相关资源
      最近更新 更多