0

I am developing a web application using laravel, and the application requires me to create a pdf file and save it to be downloaded later.I have successfully created the file from view using laravel-snappy and i can download it on the browser.The problem is that; when i try to save the file in a network shared drive using the file_put_contents function, I get the error captioned on the question's title.

When I change the path from network drive, to let say disk D:/test of the server, the file is successfully saved in that location.

I have tried the following ways to achieve it as below

$saving = "Z:/test"; Z is the network drive (drive of th shared folder)

1.By using barry laravel snappy facade

$pdf = PDF::loadView('file',compact('data'));

When I return it as below it works return $pdf->download($fileName);//download the file

But when saving it in the path it gives me the error I mentioned above $pdf->save($savePath);//saving the file

  1. Using the file_put_contents function

$output = $pdf->output();

file_put_contents($savePath,$output);

Still getting the same error

3.Using the copy function In the same drive/disk but different folders it works $path1 = "D:/files"; $path2 = "D:/test"; copy($path1,$path2);

But when i change $path2 to the shared network drive/folder

$path1 = "D:/files"; $path2 = "Z:/test"; copy($path1,$path2);

It does not work and I get a different error as shown below copy(Z:/test/39361_1657094148_elicense.pdf): failed to open stream: No such file or directory

When i change from Z:/test to IP of the shared folder as \\x.x.x.x\test it gives me another error as copy(\\x.x.x.x\test/39361_1657094148_elicense.pdf): failed to open stream: Invalid argument

I have tried this for 3 days now but nothing is working When I save to that shared folder/drive

I really need help. Anyone with an insight on how I can get out of this problem I will appreciate

Thanks in Advance

kidume
  • 21
  • 3
  • Does the user under which the website account runs have that network drive mapped? If not, try using the equivalent UNC path instead of the drive-letter version. And make sure that account has permissions to it as well – ADyson Jul 08 '22 at 07:00
  • Yes, the network drive is mapped – kidume Jul 08 '22 at 07:52
  • And @ADyson, sorry what's the UNC path and how do i use it – kidume Jul 08 '22 at 07:56
  • Regarding permission, the folder has full control set – kidume Jul 08 '22 at 07:57
  • UNC path is like `\\server\share\folder\file.txt ` format. Every network drive is accessible by such a path (mapping it as a drive is just a convenience). See https://stackoverflow.com/questions/21482825/find-unc-path-of-a-network-drive for ways to find out the UNC path of your drive. – ADyson Jul 08 '22 at 07:58
  • `the folder has full control set `...full control for who, though? As I explained above, your web application will run under a different account than the one you log into the computer with. Assuming you're using IIS, you can check the IIS application pool settings for the PHP site, to find out and/or modify what account it uses. (But don't set it to run as yourself, instead use a service account which doesn't belong to a specific human being, or one of the built-in accounts). If you're using Apache or something else as the server, consult the relevant documentation for how to achieve that. – ADyson Jul 08 '22 at 08:00
  • Using the UNC path, it is giving me another error as ```file_put_contents(\\x.x.x.x\Data/39361_1657271757_elicense.pdf): failed to open stream: Invalid argument``` with x.x.x.x being the ip address and Data is the shared folder – kidume Jul 08 '22 at 09:16
  • Seems like you forgot to put the path in quote marks...it's still a string! Also you need to use backslashes throughout, not forward slashes. – ADyson Jul 08 '22 at 09:55
  • I am putting it like file_put_contents("\\\\x.x.x.xx\\Data\\39361_1657275298_elicense.pdf", $output); but getting it like ```file_put_contents(\\x.x.x.x\Data\39361_1657275298_elicense.pdf): failed to open stream: Invalid argument``` without quotes – kidume Jul 08 '22 at 10:17

1 Answers1

2

the issue was with the permission, my application had no right to write to that network shared folder The solution was to open the directory and then save the file

I find the solution from this link https://www.codedwell.com/post/21/reading-file-list-from-a-mapped-windows-network-drive

Thanks for the insight @ADyson

kidume
  • 21
  • 3