cib88's avatar
Level 2

PHPExcel_Reader_Exception: Could not open "File Name" for reading! File does not exist.

I have a user upload a CSV which is handled by my controller then this dispatches a job where i'm going to process the data and update or insert into the database.

The issue i'm facing is that I get the following error

"PHPExcel_Reader_Exception: Could not open /Users/craig/Sites/location-finder/uploads/OGOU3SAArhOhW88W8tjXfBFIDN3EscsVDjmiuKIR.txt for reading! File does not exist. in /Users/craig/Sites/location-finder/vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/Excel2007.php:82"

The file is been uploaded to public/uploads but the Job doesn't pick that up. I don't mind if the file is stored somewhere else, just not sure why it's not passing the right path.

My controller is

public function upload(Request $request) {

        if($request->file('imported-file')) {

            $file = $request->file('imported-file')->store('/uploads', 'public');

            ProcessCSV::dispatch($file);

            Session::flash('success', 'Your file was uploaded sucessfully. We will email once this has been processed.');
            return back();

        } else {

            Session::flash('error', 'Please select a file to upload!!!!');
            return back();

        }

    }

My Job

class ProcessCSV implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
    * @var File
    */

    public $file;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(string $file)
    {
        $this->file = $file;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

        $data = Excel::load($this->file, function($reader) {})->get();

        var_dump($data);

        // do more code here

    }
}
0 likes
4 replies
bobbybouwmann's avatar

The first question here would be if the file path is correct?

What does the store method actually return here? I believe it returns a relative path and not an absolute path!

1 like
cib88's avatar
cib88
OP
Best Answer
Level 2

After @bobbybouwmann 's comment I dig some digging into the paths. The store function was doing the right job and placing the file where I needed it to be in public/uploads but my job was looking in the root not public.

I changed my code to include the public_path function. Code below, for anyone else who has this issue!

$path = public_path($this->file);
$data = Excel::load($path, function($reader) {})->get();
kirciemre's avatar

i'm having the same problem? I'm using Vesta CP on Ubuntu, it may be due to sever permission settings.

Please or to participate in this conversation.