We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Form file upload not working properly on some media files

Hi there. I am trying to build a CRUD section that accepts the details of a music article: title, description, image preview (album art or something similar) and then the music file itself...

I have created a form (using volt) and I have a controller/action to handle the upload.

My issue: I get a protected error on all music and video files. But images, text andd pdf files works without any issues.

Here is my add modal:

 <form class="form-horizontal" action="{{ postRoute }}/add" method="post" enctype="multipart/form-data">

                    <input type='hidden' name='{{ security.getTokenKey() }}'
                           value='{{ security.getToken() }}'/>

                <div class="modal-body">
                    <div class="card card-body">
                        <div class="card-body">
                            <div class="form-group row">
                                <label for="inputTitle" class="col-sm-2 col-form-label">Music Title</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="inputTitle" name="post_title" placeholder="Title">
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="inputContent" class="col-sm-2 col-form-label">Article Content</label>
                                <div class="col-sm-10">
                                    <textarea rows="4" class="form-control" id="inputContent" name="post_content" placeholder="Content"></textarea>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="inputImagePreview" class="col-sm-2 col-form-label">Image Preview</label>
                                <div class="col-sm-10">
                                    <div class="custom-file">
                                        <input type="file" {#accept="image/jpeg"#} class="custom-file-input" name="image_preview" id="inputImagePreview">
                                        <label class="custom-file-label" for="exampleInputFile">Choose file</label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="inputImagePreview" class="col-sm-2 col-form-label">Music File</label>
                                <div class="col-sm-10">
                                    <div class="custom-file">
                                        <input type="file" class="custom-file-input" name="music_file" id="inputFileUpload">
                                        <label class="custom-file-label" for="inputFileUpload">Choose file</label>
                                    </div>
                                </div>
                            </div>
                            {#<div class="form-group row">
                                <label for="selectStatus" class="col-sm-2 col-form-label"></label>
                                <div class="col-sm-10">
                                    <select name="select_status" class="form-control">
                                        <option value="Y">Active</option>
                                        <option value="N">In Active</option>
                                    </select>
                                </div>
                            </div>#}
                        </div>
                    </div>
                </div>
                <div class="modal-footer justify-content-between">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Submit Post</button>
                </div>
                </form>

And a partial code snippet from my action:

$imageFile = $musicFile = "";
        $files = null;

        // Check if the user has uploaded files
        if ($this->request->hasFiles()) {

            //Store the images temporarily
            $files = $this->request->getUploadedFiles();

        }

        foreach ($files as $file) {

            $fileKey = $file->getKey();
            $this->customlogger->info("I saw a file with the key: {$fileKey}");

            //Checks if the current object is containing an image
            if($file->getSize() > 0)
            {

                //Determine file contents
                if($fileKey == "image_preview")
                {
                    // Generates a custom file name
                    $imageFile = TokenGenerator::getRandomFileName(Constants::FILE_TYPE_IMAGE) . '.' . $file->getExtension();

                    // Move the file into the application
                    $file->moveTo(
                        $this->_imageStoreDir . $imageFile
                    );
                }

                //Determine file contents
                if($fileKey == "music_file")
                {
                    //$this->custtomlogger->info('I saw an audio download...');
                    // Generates a custom file name
                    $musicFile = TokenGenerator::getRandomFileName(Constants::FILE_TYPE_AUDIO) . '.' . $file->getExtension();

                    // Move the file into the application
                    $file->moveTo(
                        $this->_musicStoreDir . $musicFile
                    );
                }

            }
        }

        /*$this->view->disable();
        echo '<pre><tt>';
        print_r($files);
        echo '</tt></pre>';
        die;*/

I'll need your help on this please... I don't know what I'm doing wrong...

Thanks.

I added syntax highlighting to your post. Adding the language name after the three backticks (in this case "html" and "php" respectively), triggers the syntax highlighting.

To address your question - what is a "protected error"? Videos and audio tend to be larger than images and PDFs - could you be hitting the maximum file upload size?