laravel获取输入文件的真实路径 [英] laravel get real path of input file

查看:547
本文介绍了laravel获取输入文件的真实路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这样的脚本

<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>

在我的控制器中,我想显示文件的路径
可以说该文件位于D:\Data\product.xlsx
我用这个脚本来做到这一点

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

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"

我尝试使用->getFilename(),它也显示了临时文件名phpD745.tmp

i try with ->getFilename() it's show temp filename too phpD745.tmp

但是如果我使用的是->getClientOriginalName(),则显示为product.xlsx

but if i'm using ->getClientOriginalName()it's show exactly product.xlsx

我想知道如何获取文件的真实路径
ps:我正在使用laravel 4.2

i wonder how to get file real path
ps : i'm using laravel 4.2

推荐答案

上传后,文件将以随机生成的名称存储在临时目录中.

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
}

此处中了解更多功能.

这篇关于laravel获取输入文件的真实路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆