lundi 22 mai 2017

test file upload with cakephp

Is it possible to test the file-upload in your own computer with an emulated server, or it's only possible to a real server???

If i try to upload a file, i get the message 'The download could not be saved. Please, try again.'. (The message is coming from my own DownloadController.) Nothing is saved to upload-path or stored in the database.

So CakePHP runs a build-in Web-Server. I thougt i can upload a file to my own computer (i know its not a real upload, but there is my file, the web-server, the webroot with upload-path...) But maybe i'm totally wrong trying this??? Really don't find anything on web...

In my application i want to give the users opportunity to download files. So what i've done yet.

Installed the upload-plugin Josegonzalez.

DownloadsTables.php

$this->addBehavior('Josegonzalez/Upload.Upload', [
            'file' => [
                    'path' => 'webroot{DS}downloads{DS}',
                    'fields' => [
                            // if these fields or their defaults exists
                            // the values will be set.
                            'dir' => 'file_dir', //defaults to 'dir'
                            'size' => 'file_size', //defaults to 'size'
                            'type' => 'file_type', //defaults to 'type'
                    ],
            ],              
    ]);

In TemplateDownloads/add.ctp

<?= $this->Form->create('Download', ['type' => 'file']) ?>
<fieldset>
    <legend><?= __('Add Download') ?></legend>
    <?php
        echo $this->Form->control('Download.name');
        echo $this->Form->control('Download.file', ['type' => 'file']);
        echo $this->Form->control('Download.file_dir', ['type' => 'hidden']);
        //echo $this->Form->control('meetings._ids', ['options' => $meetings]);
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

In DownloadsController.php

public function add()
{
    $download = $this->Downloads->newEntity();
    // @ToDo download auf Server aktivieren

    if ($this->request->is('post')) {
        //debug ($this->request);
        //debug ($this->Downloads);
        $download = $this->Downloads->patchEntity($download, $this->request->getData());
        debug ($download);
        debug ($this->addBehavior);
        if ($this->Downloads->save($download)) {
            $this->Flash->success(__('The download has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The download could not be saved. Please, try again.'));
    }
    $meetings = $this->Downloads->Meetings->find('list', ['limit' => 200]);
    $this->set(compact('download', 'meetings'));
    $this->set('_serialize', ['download']);
}

bootstrap.php

Plugin::load('Josegonzalez/Upload');

So I'm waiting hopefully for your help and thank you for your advice


Edit:

My CakePHP Version is 3.4.6

Entitiy before saving

object(App\Model\Entity\Download) {

'Download' => [
    'name' => 'test1',
    'file_dir' => '',
    'file' => [
        'tmp_name' => '/tmp/phpiBp9GI',
        'error' => (int) 0,
        'name' => 'test1',
        'type' => 'application/octet-stream',
        'size' => (int) 15
    ]
],
'[new]' => true,
'[accessible]' => [
    '*' => true,
    'id' => false
],
'[dirty]' => [
    'Download' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Downloads'}

and after saving

object(App\Model\Entity\Download) {

'Download' => [
    'name' => 'test1',
    'file_dir' => '',
    'file' => [
        'tmp_name' => '/tmp/phpiBp9GI',
        'error' => (int) 0,
        'name' => 'test1',
        'type' => 'application/octet-stream',
        'size' => (int) 15
    ]
],
'file' => null,
'[new]' => true,
'[accessible]' => [
    '*' => true,
    'id' => false
],
'[dirty]' => [
    'Download' => true,
    'file' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Downloads'}

Aucun commentaire:

Enregistrer un commentaire