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.
Related
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
I'm working with two different APIs trying to transfer documents from one services to another. So far, the API has been kind of clunky to work with, but I was getting it to work, until now with the documents problem. Here is what I have to do:
1. I need to get the url of the document from the one service (which I have done).
2. Download it to my own servers to a folder called filesToUpload (which I have created).
3. Transfer it to the other services (which I have done with a document already on my servers.)
So as you can see, the problem isn't transferring it to the other service, the real problem is downloading it to my own servers. Here is what I have tried:(the $doc['url'] is the response from the one server, and I have confirmed that the url supplied is an existent document)
$fileToPost = str_replace(" ", "+", $doc["url"]);
$fileName = strchr($fileToPost, "-");
$fileName = substr($fileName, 1);
file_put_contents("/api/ricochet-clean/filesToUpload/$fileName", fopen($fileToPost, 'r'));
Here is my error message:
file_put_contents(/api/ricochet-clean/filesToUpload/uploaded-leads.s3.amazonaws.com/219-2018-06-10-10-19-04-Assign6.docx): failed to open stream: No such file or directory in <b>/Applications/XAMPP/xamppfiles/htdocs/SoftwareAPIs/api/ricochet-clean/index.php</b> on line <b>191</b><br />
In the file_put_contents() function call, I have tried the /api/ricochet-clean/filesToUpload/$fileName absolute path and I have also tried a relative path of filesToUpload/$fileName.
I saw this question: file_put_contents: Failed to open stream, no such file or directory
The answer to this was the developer hadn't created the directory he was trying to download the file to, that isn't my problem because I've confirmed I created the directory AND it is spelled the same both places.
What am I missing here?
The file you wish to write is
/api/ricochet-clean/filesToUpload/$fileName
but the file being written is
/api/ricochet-clean/filesToUpload/uploaded-leads.s3.amazonaws.com/219-2018-06-10-10-19-04-Assign6.docx
Assuming the directory uploaded-leads.s3.amazonaws.com does not exist, you will need to create it, or write code to create it.
The quotation marks you're using here are absolutely fine.
Will you try like this?
file_put_contents("'/api/ricochet-clean/filesToUpload/$fileName'", fopen($fileToPost, 'r'));
I trying to create new file in Ubuntu system using PHP script,
but when I run this script the error
Unable to Open file
is appear
although I sure that the file's path is right and I have permissions to access this file I don't know where is the wrong
this is my code
$myfile = fopen('inc/users/future.php', "w")or die("Unable to open file!") ;
$text='<?
$host ="'.$host.'";
$user ="'.$db_admin.'" ;
$pass ="'.$db_password.'";
$db ="'.$database.'" ;
$myconn;?>';fwrite($myfile, $text);
fclose($myfile);
the path of this script is
/var/www/html/ghost/index.php
and the path of the file which I wish to open is
/var/www/html/ghost/inc/users/future.php
in other hand when I run this script in windows machine every thing is go fine
In your script use
fopen(dirname(__FILE__) . '/inc/users/future.php', 'w')
This will create a filepath from the directory your index.php. If you script is called from another file, php might search coming from that file.
Also check if the php process has sufficient file permissions to read and open the file. Try setting the file to chmod 777 just to test if that is the case (do not leave it on 777 though).
Keep in mind that if the file is a symbolic link, the 'w' parameter of fopen will not work.
Situation:
Ubuntu 16.04
PHP 7
NFS share from an OpenVMS host
Trying to copy a file from the NFS share to a local directory.
The PHP copy() function copies the file, but the resulting local file has a bunch of junk at the end of it. I suspect it's the allocated but unused area at the end of the physical OpenVMS file, but haven't proved that.
Using copy( 'ftp://...' ) results in
failed to open stream: FTP server reports 502 SIZE is unimplemented
file_put_contents( ..., file_get_contents( ... )) does work, though.
Can somebody please explain to me why file_get_contents() gets the correct data and copy() does not?
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