英文:
Laravel's Storage::exists returns false while file_exists return true
问题
file_exists($outputPath)
返回true
,但Laravel的Storage::disk('local')->exists($outputPath)
返回false
。为什么会发生这种情况?
这种情况可能是由于Laravel的存储系统和PHP的file_exists
函数之间的差异导致的。Laravel的Storage::disk('local')->exists($outputPath)
方法会检查文件是否存在于Laravel的文件存储系统中,而file_exists
函数会检查文件是否存在于底层文件系统中。
可能的原因之一是文件权限问题。Laravel的存储系统可能需要特定的权限设置来执行exists
检查。请确保存储目录及其父目录具有正确的权限,以允许Laravel访问文件。
另一个可能的原因是文件路径的问题。请确保$outputPath
的路径与Laravel存储磁盘配置相匹配,并且不包含任何额外的斜杠或不必要的字符。
最后,您可以尝试使用Laravel的Storage::disk('local')->missing($outputPath)
方法,它用于检查文件是否不存在,而不是存在。这可能有助于解决问题。
总之,您应该仔细检查文件的权限和路径,以确保Laravel的存储系统能够正确检查文件的存在。如果问题仍然存在,您可能需要查看Laravel的存储配置和文件系统配置以进行进一步的调试。
英文:
I have the following Laravel process happening in a job:
- If the project is using S3 disk, the file is downloaded from S3 to local disk at
storage/app/videos-to-convert/{video-name}
- Use
ffmpeg
to copy the video and add the necessary metadata - Check some information on the metadata of the video and save to DB
- If S3 is in use, move the local file to S3.
The issue is that Laravel's Storage::disk('local')->exists($videoPath)
is returning false
while file_exists($videoPath)
is returning true
.
// We will need to move the S3 file into local disk so that we can run FFMPEG
if ($this->isUsingS3Bucket()) {
$s3File = Storage::disk('s3')->get("videos-to-convert/{$this->session->id}.{$this->session->video_extension}");
Storage::disk('local')->put("videos-to-convert/{$this->session->id}.{$this->session->video_extension}", $s3File);
}
// Over here, we are sure of these paths.
// I have double-checked them, and they are correct.
$inputPath = storage_path("app/videos-to-convert/{$this->session->id}.{$this->session->video_extension}");
$outputPath = storage_path("app/videos/{$this->session->id}.{$this->session->video_extension}");
// The video's headless information is sent by MediaRecorder in the browser.
// We need to copy the video with ffmpeg, so we can get meta information such as the duration of the video.
shell_exec("ffmpeg -y -i $inputPath -vcodec copy -acodec copy $outputPath");
$ffprobe = FFProbe::create();
$durationInSeconds = $ffprobe->format($outputPath)->get('duration');
echo "-------\n";
$o = shell_exec("ls -la /var/www/html/storage/app/videos");
// This `ls -la` shows that the video exists in the location used in Storage::disk('local')->exists($outputPath)
echo $o;
if ($this->isUsingS3Bucket()) {
$exists = file_exists($outputPath);
$existLaravel = Storage::disk('local')->exists($outputPath);
if ($exists) {
echo "FILE EXISTS 'file_exists'------\n";
}
if ($existLaravel) {
echo "FILE EXISTS WITH Storage::disk('local') LARAVEL-----\n";
}
// The rest of the code
}
The file_exists($outputPath)
returns true
, but Laravel's Storage::disk('local')->exists($outputPath)
returns false
. Why is this happening?
答案1
得分: 2
因为输出路径应该相对于存储目录,
所以相对于:
storage/app
或者
storage/app/public
取决于你的情况。
现在 Laravel 正在搜索 storage/app/app/videos/*
中的文件,但找不到它。
英文:
Because the output path should be relative to the storage,
so relative to :
storage/app
or
storage/app/public
depending on your case.
Right now laravel is searching for a file in storage/app/app/videos/*
and does not find it
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论