我正在尝试将文件从一个位置复制到另一个位置。我很确定位置是正确的,但我仍然在标题中收到错误。
这是一些代码:
$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
功能非常棒。
我在这里找不到问题。
有什么建议?
首先,如果你正在使用Laravel的存储外观,并且你必须知道底层的Flysystem,它不能像你那样使用绝对路径。这样做的好处是,您可以使用不同的存储磁盘,它们都有自己的配置,可以在config / filesystems.php文件中设置。
假设您没有更改anythig,默认的“磁盘”将是本地的,具有storage_path('app')的根(= laravel存储文件夹/ app的路径)
如果你想知道为什么它失败了,我们必须看一下你将在文件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中定义的根路径
'disks'=> [
'local' => [
'driver' => 'local',
'root' => storage_path('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相对于您定义的'root'。
你说错误是这样的:
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
该路径相对于当前目录。如果你添加/
购买,路径是从根目录绝对。
你必须选择这样的正确磁盘
$file2 ='public/'.$page->thumbnail;
$destination = 'public/bundles/ttt.png';
if( Storage::disk('local')->exists($file2)){
Storage::disk('local')->copy($file2,$destination);
}