Read and write to the same file - php

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!");

Related

Unable to open a file from PHP using fopen

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.

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!");

PHP: Downloading 2 Created Text Files As Zip

I'm trying to create 2 text files which should only be placed in a downloadable zip file.
Twice use of
fopen("php://output","w")
with
header() things
works and helps avoiding file creation on server but I end up mixing the two strings to into one downloadable text file.
Here is a local version of creating 2 files in a local directory:
$dirname = "C:/texts";
if (!is_dir($dirname)) {
mkdir($dirname);
}
$myfile = fopen("C:/texts/one".$dateInputted.".txt", "w") or die("Unable to open file!");
fwrite($myfile, $one);
fclose($myfile);
$myfile = fopen("C:/texts/two".$dateInputted.".txt", "w") or die("Unable to open file!");
fwrite($myfile, $two);
fclose($myfile);
Use ZipArchive plugin of PHP
$zip = new ZipArchive();
$zip->open('path/to/zipfile.zip', ZipArchive::CREATE);
$zip->addFile('some-file.pdf', 'subdir/filename.pdf');
$zip->addFile('another-file.xlxs', 'filename.xlxs');
$zip->close();
You can find more help here :
http://akrabat.com/php/creating-a-zip-file-with-phps-ziparchive/
http://php.net/manual/en/zip.examples.php

read and execute commands from text file

I have a txt file with all the sql commands. I need to open the file, read the commands and execute against postgresql.
$file = fopen("createme.sql.txt", "r") or exit("Unable to open file!");
while(!feof($file))
{
pg_query($conn, fgets($file));
}
fclose($file);
I expected the above code to work. But is shows an error:
PHP Warning: pg_query(): Query failed:
How do I execute the commands from a text file?
fgets get file content line by line.
Using fread to get all content of file, set the second parameter to size of your files.
$fileName = "createme.sql.txt";
$file = fopen($fileName, "r") or exit("Unable to open file!");
// checking if opening file is error.
if(!feof($file))
{
$str = fread($file, filesize($fileName));
pg_query($conn, $str);
}
fclose($file);
Another trick thanks to #smassey
$fileName = "createme.sql.txt";
$str = file_get_contents($fileName);
pg_query($conn, $str);
fclose($file);
try trim() to cutoff "\r\n" from the end of each line
$file = fopen("createme.sql.txt", "r") or exit("Unable to open file!");
while(!feof($file))
{
pg_query($conn, trim(fgets($file)));
}
fclose($file);

Categories