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

时间:2022-06-05 20:34:14

I'm trying to copy a file from one location to another. I'm pretty sure the location is correct, but I'm still getting the error in the title.

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

Here's some code:

这是一些代码:

$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);

The print_r returns (which means the files exist):

print_r返回(表示文件存在):

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 )

The echo returns:

回声返回:

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

And the exact error is:

确切的错误是:

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

在路径找不到文件:var / www / apk.land / storage / extracted_apks / res / drawable-xxxhdpi-v4 / icon.png

P.S. PHP's copy function works just great.

附: PHP的复制功能非常棒。

I can't find the problem here.

我在这里找不到问题。

Any suggestions?

有什么建议么?

4 个解决方案

#1


3  

First of all, if you are using Laravel's Storage Facade and with that the underlying Flysystem you have to know, that it is not intended to work with absolute paths like you did. The benefit of that is, that you could potentially work with different storage disks, that all have own configurations, that can be set in your config/filesystems.php file.

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

Assuming you did not change anythig there, the default "disk" would be local with an root of storage_path('app') ( = the path to your laravel storage folder/app )

假设你没有在那里更改anythig,默认的“磁盘”将是本地的,具有storage_path('app')的根(= laravel存储文件夹/ app的路径)

If you want to know, why it is failing, we have to take a look at the source code you will find in the following code in the file vendor\league\flysystem\src\Filesystem.php

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

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
}

So have a look, what would go on, if $this->getAdapter()->copy($path, $newpath) was called:

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

File (assuming local storage disk): vendor\league\flysystem\src\Adapter\Local.php

文件(假设本地存储磁盘):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);
}

The line

这条线

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

will prepend the root path as defined in config/filesystems.php

将添加config / filesystems.php中定义的根路径

'disks' => [

'disks'=> [

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

As I can see in your code your files are are not stored in storage/app, so i think you have to change this to 'root' => storage_path()

正如我在你的代码中看到的,你的文件没有存储在存储/应用程序中,所以我认为你必须把它改成'root'=> storage_path()

So if you want to use Storage::copy() you have just to provide paths relative to that folder. And as it is hard to see, how you could achieve this, take a look at that.

因此,如果您想使用Storage :: copy(),您只需提供相对于该文件夹的路径。而且很难看到,如何实现这一目标,请看一下。

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

There is RecursiveDirectoryIterator::getSubPath (Although quite undocumentated, which would return you the current subpath of your iterations. In your case relative to $extractFolder.'/res'.

有RecursiveDirectoryIterator :: getSubPath(虽然完全没有记录,它会返回迭代的当前子路径。在你的情况下相对于$ extractFolder。'/ res'。

Now you have to make sure you are calling

现在你必须确保你正在打电话

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

Where $icon_source and $generated_icon_file are relative to your defined 'root'.

$ icon_source和$ generated_icon_file相对于您定义的'root'。

#2


9  

You said that the error is like this:

你说错误是这样的:

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

Here error is saying it cannot find at var/www that means it's looking for apk.land/var/www whereas your file is located somewhere at /var/www. A quick fix to this can be use file protocol. Just use it like:

这里的错误是说它找不到var / www这意味着它正在寻找apk.land/var/www,而你的文件位于/ var / www。对此的快速解决方法可以是使用文件协议。只需使用它:

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

#3


3  

Try with File::copy($file, $dest) instead of Storage::copy($old, $new)
File::copy() is the wrapper on PHP's copy() function

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

#4


3  

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

插入/之前var / www / apk.land / storage / extracted_apks / res / drawable-xxxhdpi-v4 / icon.png

The path is relative to current directory. Buy if you add /, the path is absolute from root directory.

该路径相对于当前目录。如果你添加/购买,路径是根目录的绝对路径。

#1


3  

First of all, if you are using Laravel's Storage Facade and with that the underlying Flysystem you have to know, that it is not intended to work with absolute paths like you did. The benefit of that is, that you could potentially work with different storage disks, that all have own configurations, that can be set in your config/filesystems.php file.

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

Assuming you did not change anythig there, the default "disk" would be local with an root of storage_path('app') ( = the path to your laravel storage folder/app )

假设你没有在那里更改anythig,默认的“磁盘”将是本地的,具有storage_path('app')的根(= laravel存储文件夹/ app的路径)

If you want to know, why it is failing, we have to take a look at the source code you will find in the following code in the file vendor\league\flysystem\src\Filesystem.php

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

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
}

So have a look, what would go on, if $this->getAdapter()->copy($path, $newpath) was called:

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

File (assuming local storage disk): vendor\league\flysystem\src\Adapter\Local.php

文件(假设本地存储磁盘):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);
}

The line

这条线

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

will prepend the root path as defined in config/filesystems.php

将添加config / filesystems.php中定义的根路径

'disks' => [

'disks'=> [

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

As I can see in your code your files are are not stored in storage/app, so i think you have to change this to 'root' => storage_path()

正如我在你的代码中看到的,你的文件没有存储在存储/应用程序中,所以我认为你必须把它改成'root'=> storage_path()

So if you want to use Storage::copy() you have just to provide paths relative to that folder. And as it is hard to see, how you could achieve this, take a look at that.

因此,如果您想使用Storage :: copy(),您只需提供相对于该文件夹的路径。而且很难看到,如何实现这一目标,请看一下。

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

There is RecursiveDirectoryIterator::getSubPath (Although quite undocumentated, which would return you the current subpath of your iterations. In your case relative to $extractFolder.'/res'.

有RecursiveDirectoryIterator :: getSubPath(虽然完全没有记录,它会返回迭代的当前子路径。在你的情况下相对于$ extractFolder。'/ res'。

Now you have to make sure you are calling

现在你必须确保你正在打电话

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

Where $icon_source and $generated_icon_file are relative to your defined 'root'.

$ icon_source和$ generated_icon_file相对于您定义的'root'。

#2


9  

You said that the error is like this:

你说错误是这样的:

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

Here error is saying it cannot find at var/www that means it's looking for apk.land/var/www whereas your file is located somewhere at /var/www. A quick fix to this can be use file protocol. Just use it like:

这里的错误是说它找不到var / www这意味着它正在寻找apk.land/var/www,而你的文件位于/ var / www。对此的快速解决方法可以是使用文件协议。只需使用它:

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

#3


3  

Try with File::copy($file, $dest) instead of Storage::copy($old, $new)
File::copy() is the wrapper on PHP's copy() function

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

#4


3  

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

插入/之前var / www / apk.land / storage / extracted_apks / res / drawable-xxxhdpi-v4 / icon.png

The path is relative to current directory. Buy if you add /, the path is absolute from root directory.

该路径相对于当前目录。如果你添加/购买,路径是根目录的绝对路径。