Php 尝试复制时在路径中找不到文件
我正在尝试将文件从一个位置复制到另一个位置。我很确定位置是正确的,但我仍然在标题中发现了错误 下面是一些代码:Php 尝试复制时在路径中找不到文件,php,laravel,laravel-5,laravel-5.1,Php,Laravel,Laravel 5,Laravel 5.1,我正在尝试将文件从一个位置复制到另一个位置。我很确定位置是正确的,但我仍然在标题中发现了错误 下面是一些代码: $oDirectory = new \RecursiveDirectoryIterator($extractFolder.'/res'); $oIterator = new \RecursiveIteratorIterator($oDirectory); foreach($oIterator as $oFile) { if ($oFile->getFilename() =
$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
返回(这意味着文件存在):
回波返回:
/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
功能非常有效
我在这里找不到问题
有什么建议吗?您说过错误如下:
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:///var/www/storage/apk.land/storage/extracted_apks/res/drawable-xxxhdpi-v4/icon.png
尝试使用文件::复制($File,$dest)
而不是存储::复制($old,$new)File::copy()
是PHP的copy()
函数的包装在var/www/apk.land/storage/extracted\u apks/res/drawable-xxxhdpi-v4/icon.png之前插入
路径是相对于当前目录的。购买如果您添加/
,则路径是根目录的绝对路径。首先,如果您使用的是Laravel的存储外观,并且您必须知道,它不打算像您那样使用绝对路径。这样做的好处是,您可以使用不同的存储磁盘,这些磁盘都有自己的配置,可以在config/filesystems.php文件中进行设置
假设您没有更改其中的任何内容,默认的“磁盘”将是本地的,其根为存储路径(“应用”)(=指向您的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
}
线路
$location = $this->applyPathPrefix($path);
将在config/filesystems.php中定义的根路径前加前缀
“磁盘”=>[
'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';
}
}
有(尽管没有文档化),它将返回迭代的当前子路径。在您的情况下,它是相对于$extractFolder.'/res'的
现在你必须确定你在打电话
Storage::copy($icon_source, $generated_icon_file);
其中$icon\u source和$generated\u icon\u file是相对于您定义的“root”的。您必须选择正确的磁盘,例如
$file2 ='public/'.$page->thumbnail;
$destination = 'public/bundles/ttt.png';
if( Storage::disk('local')->exists($file2)){
Storage::disk('local')->copy($file2,$destination);
}
看起来“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/…
不知道为什么。它只显示文件路径..没有任何东西可以验证它是否存在..但是你能从浏览器或其他东西打开此图像吗?你能进一步解释为什么这会解决问题吗?添加/不会解决问题,因为前导/在存储过程中被剥离::copy()正如您在我的代码中看到的,它已经有了一个/,但它已被Laravel删除。
Storage::copy($icon_source, $generated_icon_file);
$file2 ='public/'.$page->thumbnail;
$destination = 'public/bundles/ttt.png';
if( Storage::disk('local')->exists($file2)){
Storage::disk('local')->copy($file2,$destination);
}