laravel 获取输入文件的真实路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33774352/
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
laravel get real path of input file
提问by Neversaysblack
i have script like this in my view
在我看来,我有这样的脚本
<form method="post" enctype="multipart/form-data" action="{{ URL::route('import.produk.post') }}">
<input type="file" name="import">
<button class="btn btn-primary form-button" type="submit">Save</button>
</form>
in my controller i wanna show path of file
lets say that file located in D:\Data\product.xlsx
i using this script to do that
在我的控制器中,我想显示文件的路径
让我们说位于D:\Data\product.xlsx
我中的文件使用这个脚本来做到这一点
public function postImportProduk() {
$input = Input::file('import')->getRealPath();;
var_dump($input);
}
but it's not display real path of the input file instead temp file like this output
但它不显示输入文件的真实路径,而是像这样输出的临时文件
string(24) "C:\xampp\tmp\phpD745.tmp"
i try with ->getFilename()
it's show temp filename too phpD745.tmp
我也尝试使用->getFilename()
它的显示临时文件名phpD745.tmp
but if i'm using ->getClientOriginalName()
it's show exactly product.xlsx
但如果我正在使用->getClientOriginalName()
它,它会准确显示product.xlsx
i wonder how to get file real path
ps : i'm using laravel 4.2
我想知道如何获取文件的真实路径
ps:我正在使用 Laravel 4.2
回答by Andrius
After you do the upload, the file is stored in a temporary directory, with a randomly generated name.
上传后,文件将存储在临时目录中,并使用随机生成的名称。
Now that you have the file in your server, you want to move it to some specific folder that you have to designate. A simple example in your case would be this:
现在您的服务器中有该文件,您希望将其移动到某个必须指定的特定文件夹中。您的情况的一个简单示例是:
public function postImportProduk() {
$input = Input::file('import');
$destinationPath = '/uploads/'; // path to save to, has to exist and be writeable
$filename = $input->getClientOriginalName(); // original name that it was uploaded with
$input->move($destinationPath,$fileName); // moving the file to specified dir with the original name
}
Read more on the functionality here.
回答by EvelynRose
Getting the original file path is not possible for security purposes.
出于安全目的,无法获取原始文件路径。
查看更多 如何使用 javascript、jquery-ajax 在更改 <input type='file'> 时获取所选文件的完整路径?