Unable to open a file from PHP using fopen - php

i am trying to read contents from a text file in php. i am using wamp on windows. i m getting this error:
Warning: fopen(/input.txt): failed to open stream: No such file or directory in C:\wamp\www\cycle_gps_sender.php on line 3
this is my code:
$location = fopen("/input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);
both the php file and input.txt are placed in www folder of wamp.

Hope this will help you:
$File = "log_post.txt";
$fh = fopen ($File, 't') or die("can't open file");
fclose($fh);

$location = fopen("input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);
Use this code and keep the input.txt file in the same directory where this code is written.

First check if file exist or not?
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
chmod($filename, 0777);
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}

$location = file_get_contents('./input.txt', FILE_USE_INCLUDE_PATH);
echo $location;
or
$location = file_get_contents('input.txt');
echo $location;
hope it will help

Add full path to file.
On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes.
$location = fopen("C:\\folder\\input.txt", "r");

Remove '/'(Slash)
$location = fopen("input.txt", "r") or die("Unable to open file!");

$file = fopen("path/input.txt","a+") or die("Unable to open file!");
.....
fclose($file);
You have to create file before you READ or if you open file by 'r' , so if you open file by 'a+' , your file will be created automatically.

Related

Warning: chmod() [function.chmod]: No such file or directory in

I'm trying to give a php file a permission to write to JSON file, but it does not seem to work. here is my code
<?php
$light = $_GET['light'];
if($light == "on") {
$file = fopen("light.json", "w") or die("can't open file");
fwrite($file, '{"light": "on"}');
fclose($file);
}
else if ($light == "off") {
$file = fopen("light.json", "w") or die("can't open file");
fwrite($file, '{"light": "off"}');
fclose($file);
}
chmod(" /home/daffo/public_html/ard/light.json", 0755);
?>
You must remove the space before the first slash:
chmod("/home/daffo/public_html/ard/light.json", 0755);
Other than the obvious, removing the space before your first slash:
chmod("/home/daffo/public_html/ard/light.json", 0755);
You'll run into trouble creating your own json. Do it using in-built functions:
fwrite($file, json_encode(array("light"=> "off")));

How to place content of uploaded text file on page in wordpress?

I am developing a website in wordpress i want to put a txt file in header for read at time of load
<?php
$myfile = fopen("keyword.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
its giving no file of directory found please help me how to do it n wordpress
Assuming you have keyword.txt in your template directory.
$myfile = fopen( dirname(__FILE__) . "/keyword.txt", "r") or die("Unable to open file!");

creating text file throws error

I am a php beginner.I want every time when the web page is open to create a file that does not exist. But every time when I run the program I have an error teling me that the file was not created.
This is my code:
$ip=$_SERVER["REMOTE_ADDR"];
if(!isset($_COOKIE['firsttime'])){
setcookie('firsttime', 'no');
$myfile = 'file/form'.$ip.'.txt';
if(file_exists($myfile) == FALSE){
$fo = fopen($myfile, 'w');
$code = '<form action = "" method = "post">';
fwrite($fo, $code);
fclose($fo);
}else{
unlink($myfile);
$file = new File();
}
}
where is my mistake?
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
Do this to open a file, and it will create the file if it doesn't exist.
Not completely sure but that would result in a pretty weird file name.
$myfile = 'file/form'.$ip.'.txt';
If my ip is 1.0.0.01.23 (really random and pretty weird) the file name would be:
file/form.1.0.0.01.23..txt
Try saving a file with that name in notepad.

file creation failing in php

I am learning file handling in php. I have written the following code to create file:
<?php
$fileName = "testFile.txt";
$fileHandle = fopen($fileName,"w") or die ("can't open file");
fclose($fileHandle);
phpinfo();
?>
The problem is I am getting "can't open file". I changed the permission of the directory containing this file and all the files in the directory to 777 still the problem persists.
Can somebody please help me in solving this issue?
Thanks
have you tried using an absolute path for the $filename
$fileName = "/path/to/file/testFile.txt";
or you can change dir prior to opening the file
chdir('/path/to/file');
http://nz.php.net/manual/en/function.chdir.php
AMENDED ANSWER, TRY THIS
<?php
$path = "/path/to/file";
$fileName = "testFile.txt";
if (! file_exists($path)) {
die ("$path doesn't exist");
}
$fileHandle = fopen("$path/$fileName","w") or die ("can't open file");
fclose($fileHandle);
phpinfo();
is the php script and file in the same folder?
if not you need to specify the relative path to the testFile.txt
Try:
$fileHandle = fopen( dirname(__FILE__) . '/' . $fileName , 'w' ) ....

Read and write to the same file

Im trying to read and write to/from the same file, is this possible?
Here is what I am getting negative results with:
<?php
$file = fopen("filename.csv", "r") or exit("Unable to open file!");
while (!feof($file)) {
$line = fgets($file);
fwrite($file,$line);
}
fclose($file);
?>
You're opening the file in read-only mode. If you want to write to the file as well, do
fopen("filename.csv", "r+")
You'll need to open the file with more 'r+' instead of just 'r'. See the documentation for fopen: http://php.net/manual/en/function.fopen.php
You opened the file in "read only" mode. See the docs.
$file = fopen("filename.csv", "r+") or exit("Unable to open file!");

Categories