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');
Related
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/
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));
I have this diretorio.php file that grabs the user id in Joomla and creates a directory with that id (if it doesn't exist yet):
/* Get the current user id */
$user = JFactory::getUser();
$usr_id = $user->get('id');
/*Define the path to this user's directory */
$diretorio=$_SERVER['DOCUMENT_ROOT']."/Apps/files/".$usr_id;
/*Create the user's directory if it doesn't exist */
if (!file_exists($diretorio) and !is_dir($diretorio)) {
mkdir($diretorio, 0755);
};
Now, I want to save a file with data in an object, using an Ajax that triggers another PHP file to the same directory created above:
$myFile = $diretorio."/dados.json";
$fh = fopen($myFile, 'w') or die("não é possível abrir o ficheiro");
$stringData = $_POST['data'];
$stringData='{ "data":'.json_encode($stringData).'}';
fwrite($fh, $stringData);
fclose($fh);
However, the file isn't created. If I replace the first line to:
$myFile = "dados.json";
It will create the file in the same directory where this PHP script is stored.
I would recommend using Joomla coding standards like so:
$user = JFactory::getUser();
$usr_id = $user->get('id');
$diretorio = JPATH_SITE . "/Apps/files/" . $usr_id;
if (!JFolder::exists($diretorio)) {
JFolder::create($diretorio, 0775);
}
$myFile = $diretorio."/dados.json";
$stringData = $_POST['data'];
$stringData = '{ "data":'.json_encode($stringData).'}';
JFile::write($myFile, $stringData);
JPATH_SITE is the root of your Joomla site
I am working on this website(for minecraft servers) that when you enter in a few things about your server, it will upload the info onto the list of servers. The thing is, I am a complete noob at PhP.
Here is my form code:
http://pastie.org/8061636
And here is my php code:
<?php
$name = $_POST['sName'];
$ip = $_POST['sIp'];
$port = $_POST['sPort'];
$desc = $_POST['sDesc'];
$finalName = $ip."(".$port.").txt";
$file = fopen($finalName, "w");
$size = filesize($finalName);
if($_POST['submit']) fwrite($file, "$name, $ip, $port, $desc");
header( 'Location: http://www.maytagaclasvegas.com/uniqueminecraftservers/upload/' ) ;
?>
Now what I am trying to do it get do is create a new file name using $ip and $port, and put this into a table. Can anyone help a newbie out? Thanks
Try something like this
file_put_contents("/path/to/files/".$ip."-".$port.".dat",
$_POST['sName'].",".$_POST['sIp'].",".$_POST['sPort'].",".
$_POST['sDesc']);
Then to create your table you would need to do something like this.
$files = glob("/path/to/files/*.dat");
echo "<table>";
foreach($files as $file){
echo "<tr><td>".implode("</td><td>", explode(",",file_get_contents($file),4))."</td></tr>";
}
echo "</table>";
It would be a lot safer though to just use a database.
Try this one:
<?php
$name = $_POST['sName'];
$ip = $_POST['sIp'];
$port = $_POST['sPort'];
$desc = $_POST['sDesc'];
$finalName = $ip."(".$port.").txt";
if($_POST['submit']) {
$file = $finalName;
// Append a new person to the file
$content = "$name, $ip, $port, $desc";
// Write the contents back to the file
file_put_contents($file, $current);
}
?>
Note: Be sure you have write (may be 777 in linux) permission on your folder where you are saving the file.
I create form to load sql file and used fopen function top open file and read this but when want to execute this data to database not work?
What is wrong in my code?
$ofile = trim(basename($_FILES['sqlfile']['name']));
$path = "sqlfiles/".$ofile;
//$data = settype($data,"string");
$file = "";
$connect = mysql_connect('localhost','root','');
$selectdb = mysql_select_db('files');
if(isset($_POST['submit']))
{
if(!move_uploaded_file($_FILES['sqlfile']['tmp_name'],"sqlfiles/".$ofile))
{
$path = "";
}
$file = fopen("sqlfiles/".$ofile,"r") or exit("error open file!");
while (!feof($file))
{
$data = fgetc($file);
settype($data,"string");
$rslt = mysql_query($data);
print $data;
}
fclose($file);
}
maybe you should use fgets instead of fgetc while youre reading lines.
Your script assumes that there is exactly one SQL statement per line, with no blanks or comments. If this is not the case then you'll need to either read and parse the file manually, or redirect the file to the mysql executable and let it handle the SQL statements.