【问题标题】:File not found at path when trying to copy尝试复制时在路径中找不到文件
【发布时间】:2019-10-09 03:28:06
【问题描述】:

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

这里有一些代码:

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

回声返回:

/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

附: PHP 的 copy 函数效果很好。

我在这里找不到问题。

有什么建议吗?

【问题讨论】:

  • 看起来“var”作为网址开头的一部分正在渗入。这是故意的吗?
  • @JJFord3 你什么意思?我不明白:)
  • 报错中的路径是:var/www/apk.land/storage/extracted_apks/res/drawable-xxxhdpi-v4/icon.png 应该是www/apk.land/storage/extracted_apks /res/drawable-xxxhdpi-v4/icon.png?
  • 有点像存储驱动程序在复制时删除了路径前的初始斜杠,注意它在错误消息中显示 var/www/... 而不是 /var/www/... 不知道为什么。
  • 它只显示文件路径..没有什么可以验证它是否真的存在..但是你能从浏览器或其他东西打开这个图像吗?

标签: php laravel laravel-5 laravel-5.1


【解决方案1】:

你说的错误是这样的:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      假设你在那里没有改变任何东西,默认的“磁盘”将是本地的,根目录为 storage_path('app') (=你的 laravel 存储文件夹/应用程序的路径)

      如果你想知道它为什么会失败,我们必须看看你会在文件 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
      }
      

      所以看看,如果 $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 中定义的根路径

      '磁盘' => [

          '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(虽然没有记录,但它会返回您迭代的当前子路径。在您的情况下,相对于 $extractFolder.'/res'。

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

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

      其中 $icon_source 和 $generated_icon_file 相对于您定义的“根”。

      【讨论】:

        【解决方案4】:

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

        路径是相对于当前目录的。加/购买,路径是根目录的绝对路径。

        【讨论】:

        • 您能否进一步解释为什么这会解决问题?
        • 添加 / 不会解决问题,因为在 Storage::copy() 期间前导 / 会被剥离
        • 它已经有一个 /,你可以在我的代码中看到,但是它被 Laravel 删除了。
        【解决方案5】:

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

        $file2 ='public/'.$page->thumbnail;
        
        $destination = 'public/bundles/ttt.png';
        
        if( Storage::disk('local')->exists($file2)){
        
              Storage::disk('local')->copy($file2,$destination);
         }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-09-14
          • 2014-08-16
          • 1970-01-01
          • 2015-11-03
          • 1970-01-01
          • 1970-01-01
          • 2014-09-14
          相关资源
          最近更新 更多