0

Basically trying to do this https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755/5 in a CakePHP 4.5.9 application.

I have a Model called Whitepapers and with I'm trying to validate a file upload. The code for the application has been created with bake initially.

The template file has a field called filename:

<?= $this->Form->create($whitepaper, ['type' => 'file']) ?>

<?= $this->Form->control('filename', ['label' => 'PDF File', 'type' => 'file']) ?>

<?= $this->Form->button(__('Save')) ?>
<?= $this->Form->end() ?>

In my Controller I have the following

public function add()
{
    $whitepaper = $this->Whitepapers->newEmptyEntity();

    if ($this->request->is('post')) {
        $whitepaper = $this->Whitepapers->patchEntity($whitepaper, $this->request->getData());

        // PDF file
        $file = $this->request->getUploadedFiles();

        // Filename stays the same as uploaded by the user.
        $uploadedFilename = $file['filename']->getClientFilename();

        // Filename for saving in DB
        $whitepaper->filename = $uploadedFilename;

        // ...
    }
 }

My intentions are:

  1. To have the file available to upload into cloud storage, which is what $file represents. Subsequent code does this and works correctly.
  2. Save the name of the corresponding file in the database in the whitepapers table in a column called filename.
  3. Perform validation such that there must be a file uploaded, it is a PDF, and is 10 MB or under.

The code I have in the Model looks like this, which is based off the linked article.

// src/Model/Table/WhitepapersTable.php
class WhitepapersTable extends Table
{
    $validator
        ->scalar('filename')
        ->maxLength('filename', 255)
        ->requirePresence('filename', 'create')
        ->notEmptyFile('filename');

    $validator->uploadedFile('filename', [
        'types' => [
            'application/pdf',
        ],
        'maxSize' => 10 * 1024 * 1024, // 10 MB
    ]);
}

This doesn't work. It will let me upload any file, of any size.

I'm not sure if filename is correct - on the line $validator->uploadedFile('filename') - since that seemingly represents both a column in the database (i.e. the filename) but somehow also the uploaded file itself?

I've cleared all the files in tmp/cache/* to make sure the Model isn't being cached.

What am I doing wrong?

1
  • I don't think cakephp have a built-in method uploadedFile(). Instead, you must manually validate file types and sizes using a custom validation rule using add() . Commented Apr 11 at 18:38

1 Answer 1

2

When patching your file is in there actually, as documented

$whitepaper = $this->Whitepapers->patchEntity($whitepaper, $this->request->getData())

debug($whitepaper);

You will see that it is now a field like any other

Instead of "filename" I would call the field "file".
And my recommendation is to use inline docblocks to give you autocomplete:

/** @var \Laminas\Diactoros\UploadedFile $file */
$file = $whitepaper->file;
$fileName = $file->getClientFilename();

etc

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.