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 に答える