Save database connection details - php

I'm creating a setup script for a project, where a form would ask the user for database connection details (such as host, username and password), and it should store these in a file which the main system can use in the future.
How could this be accomplished - by generating an .ini file, or by using PHP to edit another PHP file?

Check This link
$my_file = 'db.ini';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
/*Writing*/
$data = 'This is the data';
fwrite($handle, $data);
/*Reading*/
$handle = fopen($my_file, 'r');
$data = fread($handle,filesize($my_file));

Related

Problem new line doesn't create and replace the first line

if(isset($_POST['login_source']))
{
$email = $pass = "";
// get email id
$email = $_POST["email"];
// get password
$myfile = fopen("data.txt", "a") or die("Unable to open file!");
$pass = $_POST["pass"];
$txt = "$email:$pass\n";
fwrite($myfile, $txt);
fclose($myfile);
}
He doesn't create a new line in data.txt and he replace the first line and i try this on localhost
Open the file in append mode:
fopen("data.txt", "a")
The problem is in your file mode usage fopen("data.txt","xxxx") so check this table,
Mode
Description
"r"
Opens a file for reading. The file must exist.
"w"
Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file.
"a"
Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist.
"r+"
Opens a file to update both reading and writing. The file must exist.
"w+"
Creates an empty file for both reading and writing.
"a+"
Opens a file for reading and appending.
"rb"
Opens a binary file for reading. The file must exist.
"wb"
Creates an empty binary file for writing. If the file exists, its contents are cleared unless it is a logical file.
"ab"
Opens a binary file in append mode for writing at the end of the file. The fopen function creates the file if it does not exist.
"rb+"
Opens a binary file for both reading and writing. The file must exist.
"wb+"
Creates an empty binary file for both reading and writing. If the file exists, its contents will be cleared unless it is a logical file.
"ab+"
Opens a binary file in append mode for writing at the end of the file. The fopen() function creates the file if it does not exist.
For solving your problem you can use appending modes a, a+, ab, ab+.
Change your code like this:
if(isset($_POST['login']))
{
$email = $pass = "";
// get email id
$email = $_POST["email"];
// get password
$myfile = fopen("data.txt", "a") or die("Unable to open file!");
$pass = $_POST["pass"];
$txt = "$email:$pass\n";
fwrite($myfile, $txt);
fclose($myfile);
}

PHP str_replace deletes file content

I'm making a simple setup form where you are asked to enter your database credentials which are stored in another PHP file but when the user submits it the contents in the database credentials file are deleted and the file is just empty. I have tried debugging my code but still can't figure out what is causing the problem.
My database credentials file:
<?php
define("DATABASE_HOST", "{DB_HOST}");
define("DATABASE_USER", "{DB_USER}");
define("DATABASE_PASSWORD", "{DB_PASSWORD}");
define("DATABASE_DATABASE", "{DB_NAME}");
My code:
$databasehost = $_POST['databasehost'];
$databaseuser = $_POST['databaseuser'];
$databasepassword = $_POST['databasepassword'];
$databasename = $_POST['databasename'];
$searchF = array('{DB_HOST}','{DB_USER}','{DB_PASSWORD}','{DB_NAME}');
$replaceW = array($databasehost, $databaseuser, $databasepassword, $databasename);
$fh = fopen("../static/database.php", 'w');
$file = file_get_contents('../static/database.php');
$file = str_replace($searchF, $replaceW, $file);
fwrite($fh, $file);
fclose($fh, $file);
Thanks,
Nimetu.
You read the file with the call
$file = file_get_contents('../static/database.php');
after you have opened the file using w. Opening it for write will automatically blank the file. So change the order to
$file = file_get_contents('../static/database.php');
$fh = fopen("../static/database.php", 'w');

Unable to append (read and write) text file with php. Using fopen() and fwrite() together

I recently installed Apache, PHP and started working on a small project.
I have the following code.
<?php
$tim=time();
$ip=$_SERVER['REMOTE_ADDR'];
$ipadd=$tim."IPaddress".$ip;
$fp="user_log.txt";// file address
$myfilea = fopen($fp,"a");//open file
fwrite($myfilea,$ipadd.PHP_EOL);//add data to file
echo fread($myfilea,filesize($fp));//read file
fclose($myfilea);//close file
?>
Here is what I can do... I can either use "a" mode to add text or I can use "r" mode to read text. I cant do both. I tried using "a+","r+","ar" etc.
Did I miss something during my setup ???
I am running this on windows 8.1.
Thanks for your help.
You need to rewind the file pointer.
$tim = time();
$ip = $_SERVER['REMOTE_ADDR'];
$ipadd = $tim.'IPaddress'.$ip;
// file address
$fp = 'user_log.txt';
//open file
$myfilea = fopen($fp, 'a+');
//add data to file
fwrite($myfilea, $ipadd.PHP_EOL);
// your file pointer is at the end of the file now
// so rewind before you read
rewind($myfilea);
//read file
echo fread($myfilea, filesize($fp));
//close file
fclose($myfilea);
Try this code, use file_put_contents
file_put_contents = Write a string to a file
$fp="user_log.txt";
$tim=time();
$ip=$_SERVER['REMOTE_ADDR'];
$ipadd=$tim."IPaddress".$ip;
$myfile = file_put_contents($fp, $ipadd.PHP_EOL , FILE_APPEND | LOCK_EX);
And for your code try this, it will check able to open file or not
fopen("logs.txt", "a") or die("Unable to open file!");

How to a guest can edit .txt file in PHP?

I'm creating a tool like Pastebin for train myself in PHP (I'm a beginner yet).
Everyone without log in/sign up can create .txt file and share it easily.
Anyway, I'm facing a problem:
How can a guest edit the .txt file when he created it?
<?php
$titleFile = $_POST['title'];
$textFile = $_POST['text'];
$newFile = fopen($titleFile . ".txt", "w") or die("Unable to open file!");
fwrite($newFile, $textFile);
$url = $titleFile . ".txt";
header("Location: $url");
?>
http://mattewdev.altervista.org/

opening and viewing a file in php

how do i open/view for editing an uploaded file in php?
i have tried this but it doesn't open the file.
$my_file = 'file.txt';
$handle = fopen($my_file, 'r');
$data = fread($handle,filesize($my_file));
i've also tried this but it wont work.
$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data = 'This is the data';
fwrite($handle, $data);
what i have in mind is like when you want to view an uploaded resume,documents or any other ms office files like .docx,.xls,.pptx and be able to edit them, save and close the said file.
edit: latest tried code...
<?php
// Connects to your Database
include "configdb.php";
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM employees") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
//Outputs the image and other data
//Echo "<img src=localhost/uploadfile/images".$info['photo'] ."> <br>";
Echo "<b>Name:</b> ".$info['name'] . "<br> ";
Echo "<b>Email:</b> ".$info['email'] . " <br>";
Echo "<b>Phone:</b> ".$info['phone'] . " <hr>";
//$file=fopen("uploadfile/images/".$info['photo'],"r+");
$file=fopen("Applications/XAMPP/xamppfiles/htdocs/uploadfile/images/file.odt","r") or exit("unable to open file");;
}
?>
i am getting the error:
Warning: fopen(Applications/XAMPP/xamppfiles/htdocs/uploadfile/images/file.odt): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/uploadfile/view.php on line 17
unable to open file
the file is in that folder, i don't know it wont find it.
It might be either:
A permissions issue on the server. If it's a linux machine, try chmod 754 Applications/XAMPP/xamppfiles/htdocs/uploadfile/images/file.odt (you may need root).
An issue with apache (or whatever webserver you might be using). Make sure you've defined a directory entry in the config file for that site. Documentation here.
Although, if I recall properly, an odt file will just be binary data rather than the text information. That might be what you're looking for, I don't know. If you just want to read the actual text, and you aren't interested in using some library to extract it, you need to save it as plain text. If you're actually trying to edit these files in the browser, you're gonna need a lot more than just fopen.
For text based files you can use file_get_contents and file_put_contents
However, ODT files are zip compressed XML files so you need to decompress them first:
$zip = new ZipArchive;
$res = $zip->open('Applications/XAMPP/xamppfiles/htdocs/uploadfile/images/file.odt');
if ($res === TRUE) {
$zip->extractTo('/tmp/myOdt.xml');
$zip->close();
}
$data = file_get_contents('/tmp/myOdt.xml');
$data .= 'add this to the end';
file_put_contents('path/to/file.txt', $data);
However, it seems your problem regards the actual path of the file. Try using a relative path instead: "images/file.odt"

Categories