Yii2 Get path of uploated file


(alexsultana) #1

Hello,

Is there a way to get the full path for the uploaded file like $file->baseName?

Thank You!


(Mehdi Achour) #2

The uploaded file has a tempName property, is that what you’re looking for?


(alexsultana) #3

Not really.
I need path where is selected file on Computer (Ex Desktop)
I need the full path for this:
$objPHPExcel = \PHPExcel_IOFactory::load($file);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
$file is the full path of excel file who i want to parse


(Mehdi Achour) #4

This is not how file uploads work, as this would be a big security issue if you could read from the user Desktop.

Usually, you make the user pick a file, a copy of the file is uploaded to your server, you store and name that copy like you want, and then you read from that copy in your script.


(alexsultana) #5

Thanks for your answer!

How can I upload a copy of file to my local yii server and use it from there ?
if I try something like this :
$this->myFile->saveAs(‘tmp/’.$this->myFile->baseName . ‘.’ . $this->myFile->extension); i have an error .

I need something like this : user select an excel file, then I display the data from that selected file without save it. I didn t find any solution,

Could you help me please?

Thanks


(Mehdi Achour) #6

What is the error you’re getting ?


(alexsultana) #7

The error is : failed to open stream: No such file or directory
I have tmp folder in my aplication. I think that saveAs want something like C:\basic\tmp.

Is there any way to get data from uploaded excel file without save it ?


(Mehdi Achour) #8

Make sure you provide a valid path for the upload, you can use aliases for example:

$this->myFile->saveAs(Yii::getAlias('@runtime/test.xls'));

Don’t hesitate to provide more code and the line where the error happens, it doesn’t seem to be related to that line