F
F
First Name2018-06-06 20:20:55
Yii
First Name, 2018-06-06 20:20:55

How to upload a file to the server correctly and not get a 400 error?

Hello.
Trying to upload music to server .mp3
Model

<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\web\UploadedFile;

class MusicUpload extends Model{

    public $image;
    public function rules()
    {
        return [
            [['image'], 'required'],
//            [['image'], 'file', 'extensions' => 'jpg,png']
        ];
    }
    public function uploadFile(UploadedFile $file, $currentImage)
    {
        $this->image = $file;
        if($this->validate())
        {
            $this->deleteCurrentImage($currentImage);
            return $this->saveImage();
        }
    }
    private function getFolder()
    {
        return Yii::getAlias('@web') . 'uploads/musics/';
    }
    private function generateFilename()
    {
        return strtolower(md5(uniqid($this->image->baseName)) . '.' . $this->image->extension);
    }
    public function deleteCurrentImage($currentImage)
    {
        if($this->fileExists($currentImage))
        {
            unlink($this->getFolder() . $currentImage);
        }
    }
    public function fileExists($currentImage)
    {
        if(!empty($currentImage) && $currentImage != null)
        {
            return file_exists($this->getFolder() . $currentImage);
        }
    }
    public function saveImage()
    {
        $filename = $this->generateFilename();
        $this->image->saveAs($this->getFolder() . $filename);
        return $filename;
    }
}

Method from MusicController
public function actionSetFile($id)
    {
        $model = new MusicUpload();
        if (Yii::$app->request->isPost)
        {
            $article = $this->findModel($id);

            $file = UploadedFile::getInstance($model, 'image');

            if($article->saveFile($model->uploadFile($file, $article->path)))
            {
                return $this->redirect(['view', 'id'=>$article->id]);
            }
        }

        return $this->render('music', ['model'=>$model]);
    }

Music model
public function saveFile($filename)
    {
        $this->path = $filename;
        return $this->save(false);
    }

The form itself
<?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'image')->fileInput() ?>
    <div class="form-group">
        <?= Html::submitButton('Загрузить', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

I tried in the controller, after checking Yii::$app->request->isPost , to display what comes to the server, but immediately spits out a 400 error ...
Google suggested that there might be a problem in csrf tokens, but it is present in the form.
There is no such error on local.
Thank you for your help :)

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
F
First Name, 2018-06-08
@maxxtweek

Everything turned out to be quite simple - in the php settings it was necessary to increase the weight of the uploaded file :)

M
Mysterion, 2018-06-06
@Mysterion

Replace: with: In the validator. Well, it is desirable to replace the image name with a more suitable one. You are trying to upload a file under the guise of an image and the validator checks whether it is an image or not.

M
Maxim Timofeev, 2018-06-07
@webinar

Google suggested that the problem is probably in csrf tokens

So it is, turn on the debug panel and see what's there and check which token comes in and which one is expected. Clear cache and sessions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question