fopen() URL failure - php

I have 2 servers, the main and the test one. They're on the same network.
The users have files in a shared folder, to which both have access, using a virtual directory with read access.
The PHP.ini are pretty much the same on core and extensions.
I'm using MPDF to generate a PDF, and in the HTML there is a URL to an image in the shared folder. If I echo the HTML, it shows image, if I do $mpdf->Output(); it fails with this fopen() error:
failed to open stream: A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection
Both servers have allow_url_fopen but only the test server can generate the PDF well.

It looks like a permission issue. You have 2 users with read access to a virtual directory, and you get an error with $mpdf->Output(); that looks like it may require write access.

Related

file_put_contents(Z:/test/39361_1657094148_elicense.pdf): failed to open stream: No such file or directory in network shared drive/folder in laravel

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

TCPDF unable to output file by saving. Path on Mac?

I checked some similar questions here, but i didn't got an answer, solving my problem.
I use TCPDF to generate a PDF with PHP.
When I use the $pdf->Output($file_total, 'I');
it's all good and the file is shown in Browser.
If I use the save to localhost option $pdf->Output($file_total, 'F'); I get this error:
"failed to open stream: Permission denied in /opt/lampp/htdocs/project/tcpdf_min/include/tcpdf_static.php on line 1821"
"TCPDF ERROR: Unable to create output file"
The Path I set for saving the PDF file is: "/opt/lampp/htdocs/project/files/2021"
What am I doing wrong? is my path wrong?
The forced saving $pdf->Output($file_total, 'D'); works just fine, so i must doing something wrong with the path on mac...
please help!
Thanks
In comments we both went through possible reasons such as finding an absolute rather than relative location and checking out folder permissions for non admin process as that was also suspected. Thus between us the conclusion was:-
use address as
"localhost:8080/projects..."
and ensure target folder was set to permissions
"read/write" for all users
For production use the write access needs to be trimmed back to the process and admin, not all users.

uploading file to subdomain on same server-permission denied

I'm using this code to upload file to my server. It works just fine if I am uploading to same server but it does not upload if I am uploading to subdomain which is in the same server but different cpanel and everything.
Here is my code:
<?php
$url = 'http://www.indiancinemagallery.com/gallery/vaani-kapoor/Vaani-Kapoor-at-Radio-Mirchi-Stills-(9)9678.jpg';
$img = '/home/path_A/something/test/flower.gif';
file_put_contents($img, file_get_contents($url));
?>
I have given correct path for subdomain.
If I give path of same server from where file is being executed it will upload otherwise it will not upload. The main problem is mysql is there in main server so i want to execute the file from main server and store the photo in subdomain and update the details in main server.
Warning: (flower.gif: failed to open stream: Permission denied in /
This is likely a file permission problem. Your script must have write access to wherever $img points to on the server.
Can you try adding the following at the top of your PHP script to see what, if any, errors/warnings that PHP is throwing?
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
That should tell you if it is failing to write to the file and give you some info.
If after changing permissions, you still cannot write to that location, I imagine it is enforced by your web server management software (Web Host Manager, cPanel, etc.) and how it configures the file space. At that point, you might want to try using PHP's FTP functions (preferably SFTP for security): http://php.net/manual/en/wrappers.ssh2.php

php fopen can't find file that definitely exists

I'm trying to load data from a CSV on my Windows PC into a database, something I've successfully done previously. fopen can't find my input file.
Here's the specific code I'm having trouble with:
<?php
ini_set('track_errors', '1');
$handle = fopen("C:/Users/Sam/Documents/test.csv", 'r') or die("can't open file: $php_errormsg");
?>
The error printed is:
[function.fopen]: failed to open stream: No such file or directory
The file definitely exists, and I get the same problem on Unix machines. How do I fix this?
Windows 7 (and Vista?) only lets a user access his own home directory and does not allow Apache (or other users) to. Unfortunately, this is a major headache, and I would suggest that you just move the file somewhere public.
This type of behavior is easier to fix in Linux, but you're still better off moving the file out of your directory into some path where Apache has read access.

not able to upload file on remote server

I am trying to upload image file from my one server to another remote server (owned by me). but its giving me error
Warning: move_uploaded_file(http://www.mysite.org/photo/color-sample-colorize12-10-2010-09-14-09.jpg)
[function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections.
Thanks for any suggestion or help in advance
You cant do that.... You need the sites to be on the same physical server and have the directory youre moving to have the proper permissions and be mapped in to both sites.
You could however use ftp or cURL functions to actually upload the file to the remote server, just not move_uploaded_file.
You need to get the file from the remote server using something like file_get_contents and then save it to a file on the local server using file_put_contents, or ftp, or curl if you have the permissions. You can't just copy a file like you would if it was on the same server. (I assume this is what you're trying to do, right?)

Categories