Yii2上传一些文件,但是没有文件

时间:2018-08-07 04:20:45

标签: yii2 yii2-advanced-app

在我的Yii网站上,我尝试上传mp3文件,但很奇怪的是,有些文件被上传了,但有些文件没有上传,我不知道它们具有相同的扩展名,但是没有上传;我的两个文件是

1-beautiful_violin.mp3,大小为394 KB,文件类型:MP3格式声音(.mp3)

2-guitar_vs_violin.mp3,大小为400 KB,文件类型:MP3格式声音(.mp3)

第一首歌曲上传,但第二首歌曲未上传;其他mp3都发生这种情况,上传一些文件而没有上传一些文件。

我没有使用多个上传,每个帖子上传一个文件;这是单个文件上传。

我的模特是:

public $PMediaFile;

public function rules()
{
    return [
        [['post_text'], 'string'],
        [['post_crdate', 'post_update'], 'safe'],
        [['post_status', 'post_likeplus', 'post_likemoin', 'permission_id', 'filestype_id', 'channel_id', 'user_id'], 'integer'],
        [['permission_id', 'filestype_id', 'channel_id', 'user_id'], 'required'],
        [['post_title', 'post_uid'], 'string', 'max' => 300],
        [['post_file', 'post_thimage', 'post_bgimage', 'post_cssclass', 'post_cssid'], 'string', 'max' => 350],
        [['post_icon'], 'string', 'max' => 50],
        [['post_title'], 'unique'],
        [['PMediaFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'mp3', 'maxSize'=>2000000, 'tooBig' => 'Limit is 2M'],
        [['channel_id'], 'exist', 'skipOnError' => true, 'targetClass' => Channel::className(), 'targetAttribute' => ['channel_id' => 'channel_id']],
        [['filestype_id'], 'exist', 'skipOnError' => true, 'targetClass' => Filestypes::className(), 'targetAttribute' => ['filestype_id' => 'filestype_id']],
        [['permission_id'], 'exist', 'skipOnError' => true, 'targetClass' => Permission::className(), 'targetAttribute' => ['permission_id' => 'permission_id']],
        [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'user_id']],
    ];
}

在我的控制器中

$isMember = AxChnPermission::ChannelMemberChnNID($this->chn_nid);

        if ($isMember)
        {
            $model = new PostMedia();       $model_Permission = new Permission; 

            $chn_id = $isMember->channel->channel_id;
            $chn_uid = $isMember->channel->channel_uid;
            $chn_folder = $isMember->channel->channel_folder;

            if ($model->load(Yii::$app->request->post()) ) 
            {
                AxPostMedia::CreateMedia($model, $chn_id, $chn_folder);
                header('Location: ' . Url::current() );
                exit;
            }

            return $this->render('/post-insert/widget/_formInsertMedia', [
                'model' => $model,
                'model_Permission' => $model_Permission,
            ]);
        }

AxPostMedia:

class AxPostMedia
{   
    public function UploadAudio($MPMEDIA, $MAUDIO, $ly_MediaName, $ly_MediaFolder)
    {   
        mkdir($ly_MediaFolder, 0777, true);

        $MPMEDIA->PMediaFile->saveAs($ly_MediaFolder .'/'. $ly_MediaName . '.' . $MPMEDIA->PMediaFile->extension);

        $MAUDIO->audio_path = $ly_MediaFolder .'/'. $ly_MediaName . '.' . $MPMEDIA->PMediaFile->extension;

        $MAUDIO->save(false);
    }

    public static function CreateMedia($MPMEDIA, $CHNID, $chn_folder)
    {
        $USRID = Yii::$app->user->id;
        $css_and_folder = AxValuePost::CssID();

        $MPMEDIA->user_id = $USRID;
        $MPMEDIA->channel_id = $CHNID;
        $MPMEDIA->post_uid = AxValuePost::PSTUID();
        $MPMEDIA->post_cssid = AxValuePost::CssID();
        $MPMEDIA->post_update = time();

        $ly_MediaName = AxValuePost::FileName();

        $MPMEDIA->PMediaFile = UploadedFile::getInstance($MPMEDIA, 'PMediaFile');

        if ($MPMEDIA->PMediaFile->extension == 'mp3')
        {
            $MPMEDIA->filestype_id = 300;
        }

        $ly_MediaFolder = AxValuePost::PostFolder($chn_folder, $css_and_folder, $MPMEDIA->PMediaFile->extension);

        if ($MPMEDIA->PMediaFile->extension === 'mp3')
        {
            if ($MPMEDIA->save())
            {

            $MAUDIO = new Audio;

            $MAUDIO->post_id = $MPMEDIA->post_id;
            $MAUDIO->channel_id = $CHNID;
            $MAUDIO->user_id = $USRID;

            (new self)->UploadAudio($MPMEDIA, $MAUDIO, $ly_MediaName, $ly_MediaFolder);
            }
        }
        return $MPMEDIA->save(false);
    }
}

我的观点是:

<?php $form = ActiveForm::begin(['options' => ['id' => 'upload-media-form', 'enctype' => 'multipart/form-data']]) ?>

                <?= $form->field($model, 'PMediaFile')->fileInput() ?>

                <?= $form->field($model, 'post_text')->textarea(['rows' => 2]) ?>

                <?= $form->field($model, 'permission_id')->dropdownList($model_Permission->PermissionType()) ?>

                <div class="text-right"><?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?></div>

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

0 个答案:

没有答案