yii 404从apache网站上传文件后,但可从文件管理器访问,文件夹和文件权限为644

rbl8hiat  于 2022-11-09  发布在  Apache
关注(0)|答案(1)|浏览(77)

网站基于Yii PHP框架,当一张图片从网站上传(不是ftp)到文件夹中,而文件夹中的图片通常是通过http访问的,这个图片只能通过ftp访问,并在http请求中导致404错误。
文件夹和文件权限为755和644。
换句话说,我有“bla/foo.jpg”和“bla/bla.png”,我可以通过输入“* http://my.url/bla/foo.jpg”和“http://my.url/bla/bla.png”或在 FileZilla 中使用相同的FTP地址来查看它们。然后,我从 index.html 页面上传“girl.jpg”,它会到达相同的 /bla 文件夹。
...我可以从FTP看到bla/girl.jpg,但不能从“
http://my.url/bla/girl.jpg *"看到。它与我从HTTP看到的 foo.jpgbla.png 具有相同的权限和相同的地址。
文件从html格式上传,并在内置Yii类的帮助下保存:

yii\web\UploadedFile;
yii\helpers\FileHelper;
yii\behaviors\TimestampBehavior;
yii\db\ActiveRecord;

public static function upload($file, $folder, $id = null, $title = null)
{
    if ($file) {
        $path = self::createFolder($folder) . '/' .
            Yii::$app->security->generateRandomString() . '.' .
            $file->extension;

        if ($file->saveAs(Yii::getAlias('@root' . $path))) {
            self::autorotateImage($file->extension, $path);

            $fileStorage = self::findOne($id);
            if ($fileStorage) {
                $fileStorage->deleteFile();
            } else {
                $fileStorage = new FileStorage();
            }
            $fileStorage->path = $path;
            $fileStorage->filename = $file->name;
            $fileStorage->title = $title;

            if ($fileStorage->validate() && $fileStorage->save()) {
                return $fileStorage->id;
            }
        }
    }

    return null;
}

以下是.htaccess内容:

RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com$
RewriteRule ^pages\/how\-it\-works$ "https\:\/\/mywebsite\.com\/pages\/how\-it\-works\/" [R=301,L]

RewriteEngine on
   # If a directory or a file exists, use the request directly
   # Otherwise forward the request to index.php
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule . index.php

RewriteCond %{HTTP_HOST} !^mywebsite.com$ [NC]
RewriteRule ^(.*)$ https://mywebsite.com/$1 [L,R=301]

   #php_value upload_max_filesize 500M
   #php_value post_max_size 550M
   #php_value memory_limit 512M
   #php_value max_input_time 500
   #php_value max_execution_time 500

# Manual editing of this file may result in unexpected behavior.

<IfModule php7_module>
   php_flag display_errors Off
   php_value max_execution_time 30
   php_value max_input_time 60
   php_value max_input_vars 1000
   php_value memory_limit 128M
   php_value post_max_size 8M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php70"
   php_value upload_max_filesize 2M
   php_flag zlib.output_compression Off
</IfModule>

# END cPanel-generated php ini directives, do not edit

# php -- BEGIN cPanel-generated handler, do not edit

# Set the “ea-php72” package as the default “PHP” programming language.

<IfModule mime_module>
  AddType application/x-httpd-ea-php72 .php .php7 .phtml
</IfModule>

# php -- END cPanel-generated handler, do not edit

统一产品数据:

我尝试了Alon Eitan的建议,但问题还是一样:

if ($fileStorage->validate() && $fileStorage->save()) {

            $oldFolder = dirname(Yii::getAlias('@root' . $path));
            $newFolder = $oldFolder . "_new";
            mkdir($newFolder, 0755);
            self::xcopy($oldFolder, $newFolder);
            self::delete_dir($oldFolder);
            rename($newFolder, $oldFolder);

            return $fileStorage->id;
        }
vsnjm48y

vsnjm48y1#

已解决:

目标文件夹是否已复制,其中.htaccess指针位于未接收传入文件的文件夹上¯_()_/¯

相关问题