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);
Related
I am working on linux (raspbian). Code works locally, but not on the server.
I can't understand what the problem is in the code. The addLineToFile function works perfectly, while deleteLineFromFile does not.
I call the functions with:
if (isset($_POST['delete'])){
$valTemp = $array[$_POST['delete']];
addLineToFile($directory."linkOffline.txt",$valTemp);
deleteLineFromFile($directory."output.txt",$valTemp);
}
where
$directory = "/home/pi/linkFinder/server/";
then
function addLineToFile($dirFile, $line){
$myfile = fopen($dirFile, "a") or die("Unable to open file!");
fwrite($myfile, "\n". $line);
fclose($myfile);
}
function deleteLineFromFile($dirFile, $line){
$contents = file_get_contents($dirFile);
$contents = str_replace($line."\r\n", '', $contents);
file_put_contents($dirFile, $contents);
}
I tried to assign all the permissions to the txt files, with chmod 777.
I also thought there might be some problem with file_get_contents and file_put_contents. So I rewrote the function like this, but it still doesn't work.
function fileGetContents2($url) { //alternativa a file_get_contents
$file = fopen($url, 'r');
$data = stream_get_contents($file);
fclose($file);
return $data;
}
function deleteLineFromFile($dirFile, $line){
$contents = fileGetContents2($dirFile);
$contents = str_replace($line."\r\n", '', $contents);
$myfile = fopen($dirFile, "w") or die("Unable to open file!");
fwrite($myfile, $contents);
fflush($myfile);
fclose($myfile);
}
The txt files are in a /home/user/... folder and are generated by a python script.
Thanks in advance
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.
I am trying to use the putcsv function in php to write an array to a text file but the file is blank, I tried changing the extension to .csv and it works but I need to write it to .txt as per specifications of my assignment. I attempted to change the filename to .csv then write to it then back to .txt, my filesize changed but the file is still blank.
Here is my code
$fp = fopen('logfile.txt','w') or die ('No file!!!');
fputcsv($fp,$csv);
fclose($fp);
I got your problem, if its not allow to write CSV format to text file then write in to CSV file and rename(using rename() in PHP) to text file after writing in to CSV is done.
Here is my sample code:
$fp = fopen('logfile.csv','w') or die ('No file!!!');
fputcsv($fp,$csv);
fclose($fp);
rename('logfile.csv','logfile.txt');
Let me know, if your problem is solved.
Second Method:
str_putcsv()
function str_putcsv($fields, $delimiter = ',', $enclosure = '"', $escape_char = '\\' ) {
foreach ($fields as &$field) {
$field = str_replace($enclosure, $escape_char.$enclosure, $field);
$field = $enclosure . $field . $enclosure;
}
return implode($delimiter, $fields) . "\n";
}
Simply you can call
$file = fopen("newfile.txt", "w") or die("Unable to open file!");
$csvStr = str_putcsv($csv);
fwrite($file, $csvStr);
fclose($fp);
Use fwrite()
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
I want to filter one text file by writing the results to a second text file.
I have a little bit of code and it doesn't work like it should work, it is only writing the LAST line of the first text file into a separate backup text file.
Code:
//filter the ingame bans
$search = "permanently";
$logfile = "ban_list.txt";
$timestamp = time();
// Read from file
$file = fopen($logfile, "r");
while( ($line = fgets($file) )!= false)
{
if(stristr($line,$search))
{
$cache_ig = "ingamebanlist.txt";
$fh = fopen($cache_ig, 'w') or die("can't open file");
$content = "\n";
fwrite($fh, $content);
$content = $line;
fwrite($fh, $content);
fclose($fh);
}
}
I personally do not see any errors in my code, please help.
REMEMBER: It does kind of work, but it only writes the LAST line of the ban_list.txt file into the ingamebanlist.txt file...
What happens with your code is that you open(using write mode), write and close inside your loop so it will only ever write 1 entry then overwrite it until the last entry, thus only saving the last item.
What you want is to have it outside the loop like this:
<?php
$search = "permanently";
$logfile = "ban_list.txt";
$cache_ig = "ingamebanlist.txt";
$timestamp = time();
$read = fopen($logfile, "r") or die("can't read file");
$write = fopen($cache_ig, 'w') or die("can't write to file");
while(($line = fgets($read)) !== false)
{
if(stristr($line,$search))
{
fwrite($write, $line . "\n");
}
}
fclose($write);
fclose($read);
An alternative solution would be to use a instead of w at your fopen since w will:
Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
While a will:
Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
Which would allow your code to work as is without any changes but the line:
$fh = fopen($cache_ig, 'w') or die("can't open file");
To:
$fh = fopen($cache_ig, 'a') or die("can't open 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!");