尝试复制时在路径中找不到文件

File not found at path when trying to copy

我正在尝试将文件从一个位置复制到另一个位置。我很确定位置是正确的,但我仍然在标题中遇到错误。

这是一些代码:

$oDirectory = new \RecursiveDirectoryIterator($extractFolder.'/res');
$oIterator = new \RecursiveIteratorIterator($oDirectory);
foreach($oIterator as $oFile) {
    if ($oFile->getFilename() == 'icon.png') {
        $icons[filesize($oFile->getPath().'/icon.png')] = $oFile->getPath().'/icon.png';
    }
}
asort($icons);
print_r($icons);
$icon_source = end($icons);
echo $icon_source;
$generated_icon_file = str_slug($packagename.$version).'.png';
Storage::copy($icon_source, $generated_icon_file);

print_rreturns(表示文件存在):

Array ( [19950] => /var/www/apk.land/storage/extracted_apks/res/drawable-xxhdpi-v4/icon.png [31791] => /var/www/apk.land/storage/extracted_apks/res/drawable-xxxhdpi-v4/icon.png [6979] => /var/www/apk.land/storage/extracted_apks/res/drawable-hdpi-v4/icon.png [10954] => /var/www/apk.land/storage/extracted_apks/res/drawable-xhdpi-v4/icon.png )

回显returns:

/var/www/apk.land/storage/extracted_apks/res/drawable-xxxhdpi-v4/icon.png

准确的错误是:

File not found at path: var/www/apk.land/storage/extracted_apks/res/drawable-xxxhdpi-v4/icon.png

P.S。 PHP 的 copy 功能非常好用。

我在这里找不到问题。

有什么建议吗?

你说的错误是这样的:

File not found at path: var/www/apk.land/storage/extracted_apks/res/drawable-xxxhdpi-v4/icon.png

这里的错误是说它在 var/www 找不到,这意味着它正在寻找 apk.land/var/www 而你的文件位于 /var/www 的某个地方。可以使用 file 协议快速解决此问题。就像这样使用它:

file:///var/www/storage/apk.land/storage/extracted_apks/res/drawable-xxxhdpi-v4/icon.png

尝试使用 File::copy($file, $dest) 而不是 of Storage::copy($old, $new)
File::copy()PHP 的 copy() 函数

的包装器

var/www/apk.land/storage/extracted_apks/res/drawable-xxxhdpi-v4/icon.png

之前插入/

路径是相对于当前目录的。加/就买,绝对路径从根目录开始。

首先,如果您正在使用 Laravel 的 Storage Facade 以及底层的 Flysystem,您必须知道,它并不像您那样使用绝对路径.这样做的好处是,您可以使用不同的存储磁盘,它们都有自己的配置,可以在 config/filesystems.php 文件中设置。

假设你没有在那里改变任何东西,默认的 "disk" 将是本地的,根为 storage_path('app') (=你的 laravel 的路径存储 folder/app )

如果您想知道失败的原因,我们必须查看源代码,您将在文件 vendor\league\flysystem\src\Filesystem.php[=18 中找到以下代码=]

public function copy($path, $newpath)
{
    $path = Util::normalizePath($path); // <-- this will strip leading /
    $newpath = Util::normalizePath($newpath);
    $this->assertPresent($path); // this will cause the error in your case, because assertion cannot be fullfilled in case of missing leading / 
    $this->assertAbsent($newpath);

    return $this->getAdapter()->copy($path, $newpath); // calls the copy of your Adapter, assuming local in your case
}

看看,如果调用 $this->getAdapter()->copy($path, $newpath) 会发生什么:

文件(假设为本地存储磁盘):vendor\league\flysystem\src\Adapter\Local.php

public function copy($path, $newpath)
{

    $location = $this->applyPathPrefix($path);
    $destination = $this->applyPathPrefix($newpath);
    $this->ensureDirectory(dirname($destination));
    return copy($location, $destination);
}

$location = $this->applyPathPrefix($path);

将在 config/filesystems.php

中定义的根路径前添加

'disks' => [

    'local' => [
        'driver' => 'local',
        'root'   => storage_path('app'),
    ],

正如我在您的代码中看到的,您的文件未存储在 storage/app 中, 所以我认为你必须将其更改为 'root' => storage_path()

因此,如果您想使用 Storage::copy(),您只需提供与该文件夹相关的路径。由于很难看出您如何实现这一点,请看一下。

    foreach($oIterator as $oFile) {
        if ($oFile->getFilename() == 'icon.png') {
            $icons[filesize($oFile->getPath().'/icon.png')] = $oIterator->getSubPath().'/icon.png';
        }
    }

RecursiveDirectoryIterator::getSubPath(虽然没有记录,这将 return 你迭代的当前子路径。在你的情况下相对于 $extractFolder.'/res'.

现在你必须确保你正在调用

Storage::copy($icon_source, $generated_icon_file);

其中 $icon_source 和 $generated_icon_file 是相对于您定义的 'root'.

你必须像这样选择正确的磁盘

$file2 ='public/'.$page->thumbnail;

$destination = 'public/bundles/ttt.png';

if( Storage::disk('local')->exists($file2)){

      Storage::disk('local')->copy($file2,$destination);
 }