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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32103025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:13:21  来源:igfitidea点击:

File not found at path when trying to copy

phplaravellaravel-5laravel-5.1

提问by Alex

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_rreturns (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 copyfunction works just great.

PS PHP 的copy功能很好用。

I can't find the problem here.

我在这里找不到问题。

Any suggestions?

有什么建议?

采纳答案by shock_gone_wild

First of all, if you are using Laravel's Storage Facade and with that the underlying Flysystemyou 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 的 Storage Facade 并且你必须知道底层的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 )

假设您没有在那里更改任何内容,默认的“磁盘”将是本地的,其根为 storage_path('app') (= Laravel 存储文件夹/应用程序的路径)

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' => [

'磁盘' => [

    '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”。

回答by Aditya Giri

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/wwwthat means it's looking for apk.land/var/wwwwhereas your file is located somewhere at /var/www. A quick fix to this can be use fileprotocol. Just use it like:

这里的错误是说它无法找到,var/www这意味着它正在寻找apk.land/var/www而您的文件位于/var/www. 对此的快速修复可以是使用file协议。只需像这样使用它:

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

回答by Halayem Anis

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

与尝试File::copy($file, $dest),而不是of Storage::copy($old, $new)
File::copy()是在包装PHP的copy()功能

回答by Alejandro Salamanca Mazuelo

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.

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

回答by aitbella

you have to choose the right disk somthing like this

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

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

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

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

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