Search code examples
phpfile-uploadyii2http-status-code-301redactor

PHP post file through Yii2 & redactor text editor results in empty $_FILES (nothing uploaded)


Details

The details are that I am using https://github.com/yiidoc/yii2-redactor to enable the text editor into my Yii2 app.

view.php

 <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
 <?= $form->field($model, 'text')->widget(\yii\redactor\widgets\Redactor::className()) ?>

web.php

 'redactor' => [
        'class' => 'yii\redactor\RedactorModule',
        'uploadDir' => '/uploads/',
        'uploadUrl' => '/uploads/',
        'imageUploadRoute' => ['/uploads/'],
        'fileUploadRoute' => ['/uploads/'],
        'imageManagerJsonRoute' => ['/uploads/'],
        'fileManagerJsonRoute' => ['/uploads/'],
        'uploadUrl' => '/uploads/',
        'imageAllowExtensions'=>['jpg','png','gif']
    ]

uploads/index.php

as described here for the uploads.php http://imperavi.com/redactor/docs/upload-images/

root .htaccess

<IfModule mod_autoindex.c>
  Options -Indexes
</IfModule>
<IfModule mod_rewrite.c>
  RewriteCond %{SCRIPT_FILENAME} -d
  RewriteCond %{SCRIPT_FILENAME} -f
  RewriteRule "(^|/)\." - [F]
</IfModule>
<FilesMatch "(\.(bak|bat|config|sql|fla|md|psd|ini|log|sh|inc|swp|dist)|~|init|composer\.json|composer\.lock)$">
  Order allow,deny
  Deny from all
  Satisfy All
</FilesMatch>
<IfModule php5_module>
  php_value session.cookie_httponly true
</IfModule>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

and I don't have an .htaccess in the uploads folder for the time being.

file_uploads = On, post_max_size = 8M, and upload_max_filesize = 8M at php.ini

method="POST" automatically by Yii2 in forms and I have provided the enctype="multipart/form-data"


Reproduction

I try to upload a file (some kb only) and when I select it to be uploaded an alert pops up that has this message

The page at localhost says

true

By debugging I can see that it goes through the localhost/blah/uploads/index.php file and that the $_FILES variable is empty. At first, the post makes the request to localhost/blah/uploads and gets a 301 Moved Permanently and I can see the file in there, followed by a 200 OK GET from localhost/blah/uploads/. The referer where the view is, is another url, let's say localhost/blah/posts/update/1.

Sorry for the long post but I wanted to be thorough and have tried many options without resolving the issue. Thank you everyone in advance!


Solution

  • After 3 days, I just found out, after asking the question here, that I had to make:

    web.php

    'redactor' => [
        'class' => 'yii\redactor\RedactorModule',
        'uploadDir' => '/uploads/index.php',
        'uploadUrl' => '/uploads/index.php',
        'imageUploadRoute' => ['/uploads/index.php'],
        'fileUploadRoute' => ['/uploads/index.php'],
        'imageManagerJsonRoute' => ['/uploads/index.php'],
        'fileManagerJsonRoute' => ['/uploads/index.php'],
        'imageAllowExtensions'=>['jpg','png','gif']
    ]
    

    Apparently it needs the actual page to be addressed in full name!