从 apache 网站上传文件后出现 404,但可从文件管理器访问。文件夹和文件权限都是644

404 after uploading file from the apache website but accessible from filemanager. Folder and file permissions are 644

网站基于 Yii PHP 框架。当图片从网站(不是ftp)上传到具有同类图片的文件夹时,通常通过http访问,该图片只能在ftp中访问并导致http请求中出现404错误.

文件夹和文件权限为 755 和 644。


换句话说,我有“bla/foo.jpg”和“bla/bla.png”我可以通过输入“http://my.url/bla/foo.jpg" and "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/ [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

UPD:

用这个机制尝试了 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;
        }

已解决:

目标文件夹是否已在未收到传入文件的文件夹上使用 .htaccess 指针进行复制¯\_(ツ)_/¯