When the submit button is pressed,the isset($_POST['ta'] works,but the file is not updated inside database with '---------'. any suggestion where I am going wrong?
if ( isset( $_POST['ta'] ) ) {
$handle = fopen('saw42.TextGrid', "a");
require('db_connection.php');
fwrite( $handle, "-----------");
fclose( $handle );
}
try this
if(isset($_POST['ta'])){
$handle=fopen('saw42.TextGrid',"a");
require('db_connection.php'); // don't know why this line is here
if ($handle===false){
echo 'Unable to open file';
}else{
fwrite($handle,"-----------");
fclose($handle);
}
}
Try to check your permissions on Unix OS , Is your file is 0644 or 0444
I pressume, the require-line fails and so the file is opened, but the script is aborted before something gets written inside. If errors are turned of (as is on some preconfigured systems), no error message will be shown.
Nevertheless the question is a bit confusing, since if a database (in the sense of a relational database system accessable through a database server) is meant, the code should not use any fopen-calls. If the 'database' is a simple file, the requirement of the db_connection.php seems to be unclear.
To clarify things a bit:
A (relational) database is a collection of tables (relations) that perhaps refer to each other. Such databases are typically filled and asked via SQL-language or some object-oriented interface (MySQL, MS-SQL, SQLITE, ...)
A database in the sense of 'some data' can also reference a simple file. In this case, you have to organize data by yourselves and use file access methods to access it.
Related
I want to create a registration system on my site where only limited users will be able to create their account. I want to use a .txt file for storing usernames and passwords.
I have the following code so far :
$uname=$_POST['usr'];
$pass=$_POST['pwd'];
if(empty($_POST["ok"])){echo "Could not insert data!";}
else
{$file=fopen("user.txt","w");
echo fwrite($file,$uname);
fclose($file);}
This receives the user data from a form and puts it in user.txt file.
My problem is that when new data is inserted to txt file the old data get deleted.
I want to keep the data in txt file like
foo:12345~bar:1111
username and password are seprated by : and new user is seprated by ~ ,later I will use regex to get the data from txt file.
How can i correct my code to keep both new and old data?
You need to open file in append mode
http://php.net/manual/en/function.fopen.php
<?php
$uname = $_POST['usr'];
$pass = $_POST['pwd'];
if (empty($_POST["ok"])) {
echo "Could not insert data!";
} else {
$file = fopen("user.txt", "a");
$srt="foo:".$uname."~bar:".$pass;// create your string
echo fwrite($file, $srt);
fclose($file);
}
If we want to add on to a file we need to open it up in append mode.
So you need to change from write only mode to append mode.
$file=fopen("user.txt","a");
To answer your question: you have to explicitly pass $mode argument to fopen() function equals to 'a'.
However, it looks like a bad idea to use plain files for this task. Mainly because of concurent writes troubles.
This is really a bad choice: there are a lot of drawbacks for security, for read/write times, for concurrent requests and a lot more.
Using a database isn't difficult, so my suggestion is to use one.
Anyway, your question is asked yet here: php create or write/append in text file
Simple way to append to a file:
file_put_contents("C:/file.txt", "this is a text line" . PHP_EOL, FILE_APPEND | LOCK_EX);
I have over 750 JSON files I need to create from a MySQL Database table.
It is the WordPress "wp_options" table, but this is a MySQL question.
The wp_options table has the following properties.
option_id, option_name, option_value, autoload
The "option_name" is to become the JSON file name.
I am fine if I "have to" rename each file name manually.
The "option_value" is to become the JSON data.
Is there a way I can do this more efficiently instead of creating an empty JSON file for each row and then copying the data base option_value to the JSON file?
My main concern is with 750 files to make I am a little weary I will miss something or double up on something, and this information has to be exact.
NOTE: I've read this stack article (which is the closest I could find) # http://goo.gl/RnV5cf. But, It doesn't seem to be working as expected given the Wordpress wp_options values I think.
If I needed to do this, and only needed to do it once, I'd probably just run a little php script locally.
Assuming you have grabbed this table as an array (here I've called it $wp_options), you could just iterate over it using fopen, fwrite and fclose to make your files. I've also assumed you want the files to have '.json' extensions but obviously you can strip that out.
foreach ($wp_options as $wpo) {
$newFile = fopen($wpo['option_name'].'.json', 'w'); // w=write mode
fwrite($newFile, json_encode($wpo['option_value']));
fclose($newFile);
}
The above is untested, but I think that would work.
Sounds like you just need a local script:
<?php
// ...
foreach ($wp_options as $wp_option)
{
$fileName = __DIR__ . '/' . $wp_option['option_name'] . '.json';
file_put_contents($fileName, json_encode($wp_option['option_value']));
}
Is there any way I can save the data of a specific table of the sugarcrm database into a doc file ?
I have a custom module with username,some notes and date. I want to write this into the database and into a file as well.
Its not just a php file. I want to use logic hooks and write the code. I want to use the logic hooks to access database and then write the data into the file.
Thanks in advance
Saving as a DOC file probably isn't the best idea, since it is primarily used for formatting information. A standard .txt file is usually what you would use for such a process.
With that said, there isn't any methods built into sugar that will let you do this. You will need to build the capability into the module.
What exactly are you attempting to accomplish? There is a very powerful auditing tool set, which is good for seeing revisions to a module object. If you are just wanting to monitor changes to the table, you can setup logging for that table/database inside of SQL.
+++Ok, if you are just looking to write to a file after saves, follow the instructions at: http://cheleguanaco.blogspot.com/2009/06/simple-sugarcrm-logic-hook-example.html for a quick how-to on getting the logic hooks working. You are going to want to make a php file that simply uses the data passed to it via the bean class, and either writes to the file directly from the data within bean, or uses the bean->id parameter to do a SQL query and write to the file from that data.
Also, is this a DOC file that is going to be immediately generated and then destroyed at the end of the transaction? Or is it more of a log file that will be persistent?
++++That is simple enough then
Where you have the Query right now, replace it with:
$file = fopen($pathAndNameOfFile, 'a+') or die('Could not open file.');
$query = "SELECT * FROM data_base.table";
$result = $bean->db->query($query,true);
$dbRowData = $bean->db->mysql_fetch_assoc($result);
$printedArray = print_r($dbRowData);
fwrite($file, $printedArray) or die('Could not write to file.');
fclose($file);
*A quick note, you might need to set permissions in order to be able to read/write to the file, but those are specific to the machine type, so if you encounter errors with either do a search for setting permissions for your particular server type.
**Also, 'SELECT * FROM database.table' is going to return ALL of the rows in the entire table. This will generate a very large file, and be a performance hindrance as the table grows. You should use the bean class to update the last saved tuple:
$file = fopen($pathAndNameOfFile, 'a+') or die('Could not open file.');
$query = "SELECT * FROM data_base.table WHERE id = '".$focus->id."';";
$result = $bean->db->query($query,true);
$dbRowData = $bean->db->mysql_fetch_assoc($result);
$printedArray = print_r($dbRowData);
fwrite($file, $printedArray) or die('Could not write to file.');
fclose($file);
You can export/dump mysql databases into SQL files using mysqldump
mysqldump -u userName -p databaseName tableName > fileName.sql
I need to include one PHP file and execute function from it.
After execution, on end of PHP script I want to append something to it.
But I'm unable to open file. It's possible to close included file/anything similar so I'll be able to append info to PHP file.
include 'something.php';
echo $somethingFromIncludedFile;
//Few hundred lines later
$fh = fopen('something.php', 'a') or die('Unable to open file');
$log = "\n".'$usr[\''.$key.'\'] = \''.$val.'\';';
fwrite($fh, $log);
fclose($fh);
How to achieve that?
In general you never should modify your PHP code using PHP itself. It's a bad practice, first of all from security standpoint. I am sure you can achieve what you need in other way.
As Alex says, self-modifying code is very, VERY dangerous. And NOT seperating data from code is just dumb. On top of both these warnings, is the fact that PHP arrays are relatively slow and do not scale well (so you could file_put_contents('data.ser',serialize($usr)) / $usr=unserialize(file_get_contents('data.ser')) but it's only going to work for small numbers of users).
Then you've got the problem of using conventional files to store data in a multi-user context - this is possible but you need to build sophisticated locking queue management. This usually entails using a daemon to manage the queue / mutex and is invariably more effort than its worth.
Use a database to store data.
As you already know this attempt is not one of the good ones. If you REALLY want to include your file and then append something to it, then you can do it the following way.
Be aware that using eval(); is risky if you cannot be 100% sure if the content of the file does not contain harmful code.
// This part is a replacement for you include
$fileContent = file_get_contents("something.php");
eval($fileContent);
// your echo goes here
// billion lines of code ;)
// file append mechanics
$fp = fopen("something.php", "a") or die ("Unexpected file open error!");
fputs($fp, "\n".'$usr[\''.$key.'\'] = \''.$val.'\';');
fclose($fp);
I have made a class file which are running on my server.I am using it as a API.my some client will use it.In their program i have to create an object remotely of this class.
How will use my class file as RMI that object can create easily on other server.
Thanks
I don't think anything close to Java's RMI (as far as I understand it) can be done in PHP.
The best thing that comes to my mind is
Create an object in the remote script
Serialize that object
Return the serialized data to the calling script
Unserialize the data back into an object (note that all class definitions must be present locally for this to work!)
Note that things like active database connections, file handles and so on can not be transferred this way.
Whether this is a good - and fast enough - way to do what you want is hard to say. Maybe it helps.
To connect as a user other than 'anonymous', you need to specify the username (and possibly password) within the URL, such as 'ftp://user:password#ftp.example.com/path/to/file'. (You can use the same sort of syntax to access files via HTTP when they require Basic authentication.)
<?php
$file = fopen ("ftp://ftp.example.com/incoming/outputfile", "w");
if (!$file) {
echo "<p>Unable to open remote file for writing.\n";
exit;
}
/* Write the data here. */
fwrite ($file, $_SERVER['HTTP_USER_AGENT'] . "\n");
fclose ($file);
?>
http://php.net/manual/en/features.remote-files.php
include ("ftp://username:password#ftp.example.com/public_html/example.php");
$class = new Ford();
$dataRow = $class->consulta();
var_dump($conexion);