我正在尝试自动获取已上传文件的某些详细信息,但有两个小问题。我无法管理获取文件名以显示没有扩展名,绝对路径变得很糟糕,如 C:\ Users \ Tyrx \ Desktop \ php part 2 \ FileDepository - 返回\ public / assets / 7ofmMvm.jpg
有没有人有机会指导我朝着正确的方向前进?这是用于生成文件名和文件路径的两段代码。
文件名(例如,这会产生7ofmMvm.jpg而不是7ofmMvm)
$filelist->name = $this->request->file('asset')->getClientOriginalName();
档案路径
$fullfilepath = $destinationPath . $filelist->name;
$filelist->file_path = $fullfilepath;
功能的完整代码
public function store()
{
$filelist = new Filelist($this->request->all());
//store file details
$filelist->name = $this->request->file('asset')->getClientOriginalName();
$filelist->file_type = $this->request->file('asset')->getClientOriginalExtension();
$filelist->file_size = filesize($this->request->file('asset'));
$filelist->uploader_name = Auth::user()->email;
$filelist->save();
// Save uploaded file
if ($this->request->hasFile('asset') && $this->request->file('asset')->isValid()) {
$destinationPath = public_path() . '/assets/';
$fileName = $filelist->id . '.' . $this->request->file('asset')->guessClientExtension();
$this->request->file('asset')->move($destinationPath, $fileName);
}
$fullfilepath = $destinationPath . $filelist->name;
$filelist->file_path = $fullfilepath;
$filelist->save();
return redirect('filelists');
}
if ($this->request->hasFile('asset') && $this->request->file('asset')->isValid()) {
$destinationPath = public_path() . '/assets/';
$fileName = $filelist->id . '.' . $this->request->file('asset')->guessClientExtension();
$this->request->file('asset')->move($destinationPath, $fileName);
}
$fullfilepath = $destinationPath . $filelist->name;
$filelist->file_path = $fullfilepath;
$filelist->save();
答案 0 :(得分:1)
来自php手册
<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>
以上示例将输出:
/www/htdocs/inc
lib.inc.php
php
lib.inc
和windows + php混淆了/和...的路径。
Legoas在手册http://php.net/manual/en/function.pathinfo.php
上做出了这样的贡献&#34;获取当前解析的PHP脚本文件夹的绝对路径的最佳方法是:
<?php
if (DIRECTORY_SEPARATOR=='/')
$absolute_path = dirname(__FILE__).'/';
else
$absolute_path = str_replace('\\', '/', dirname(__FILE__)).'/';
?>
这将产生一个绝对的unix风格的路径,在Windows下的PHP5上也可以正常工作,其中混合&#39; \&#39;和&#39; /&#39;可能会给你带来麻烦。 &#34;