I want the following functionality using php
I have a csv file. Each file corresponds to a row in my database
There is a html form that will allow me to choose the csv file.
Then once the form is submitted, I must parse the csv file and insert data into the db accordingly
How do I go about doing this ?
Reading a CSV file can generally be done using the fgetcsv function (depending on your kind of CSV file, you might have to specify the delimiter, separator, ... as parameters)
Which means that going through you file line by line would not be much harder than something like this :
$f = fopen('/path/to/file', 'r');
if ($f) {
while ($line = fgetcsv($f)) { // You might need to specify more parameters
// deal with $line.
// $line[0] is the first column of the file
// $line[1] is the second column
// ...
}
fclose($f);
} else {
// error
}
(Not tested, but example given on the manual page of fgetcsv should help you get started)
Of course, you'll have to get the correct path to the uploaded file -- see the $_FILE superglobal, and the section on Handling file uploads, for more informations about that.
And, to save the data into your database, you'll have to use the API which suits your DB engine -- if using MySQL, you should use either :
mysqli
Note that you should prefer mysqli, instead of the old mysql extension (which doesn't support features added in MySQL >= 4.1)
or PDO
Related
I need to remove various useless log rows from a huge log file (200 MB)
/usr/local/cpanel/logs/error_log
The useless log rows are in array $useless
The way I am doing is
$working_log="/usr/local/cpanel/logs/error_log";
foreach($useless as $row)
{
if ($row!="") {
file_put_contents($working_log,
str_replace("$row","", file_get_contents($working_log)));
}
}
I need to remove about 65000 rows from the log file;
the code above does the job but it works slow, about 0.041 sec to remove each row.
Do you know a faster way to do this job using php ?
If the file can be loaded in memory twice (it seems it can if your code works) then you can remove all the strings from $useless in a single str_replace() call.
The documentation of str_replace() function explains how:
If search is an array and replace is a string, then this replacement string is used for every value of search.
$working_log="/usr/local/cpanel/logs/error_log";
file_put_contents(
$working_log,
str_replace($useless, '', file_get_contents($working_log))
);
When the file becomes too large to be processed by the code above you have to take a different approach: create a temporary file, read each line from the input file and write it to the temporary file or ignore it. At the end, move the temporary file over the source file:
$working_log="/usr/local/cpanel/logs/error_log";
$tempfile = "/usr/local/cpanel/logs/error_log.new";
$fin = fopen($working_log, "r");
$fout = fopen($tempfile, "w");
while (! feof($fin)) {
$line = fgets($fin);
if (! in_array($line, $useless)) {
fputs($fout, $line);
}
}
fclose($fin);
fclose($fout);
// Move the current log out of the way (keep it as backup)
rename($working_log, $working_log.".bak");
// Put the new file instead.
rename($tempfile, $working_log);
You have to add error handling (fopen(), fputs() may fail for various reasons) and code or human intervention to remove the backup file.
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']));
}
I want to allow the user to select a file from which data is exported and saved into a database. I found the following code to import the data:
$fp = fopen("people.csv", "r");
while ($line = fgets($fp)) {
$char = split("\t",$line,4);
list($name, $age,$height,$date) = $char;
//Split the line by the tab delimiter and store it in our list
$sql = "insert into people(name,age,height,date) values('$name','$age','$height','$date')"; // Generate our sql string
mysql_query($sql) or die(mysql_error()); // Execute the sql
//fclose($line);
I'm not sure if it works but i'm yet to get that far. My question is how do I get the full path name of the selected file so I can feed it into the following command rather than hard coding the filename:
$fp = fopen("people.csv", "r");
I have spent alot time researching this but to no avail.
If you want to let users upload this file, you should implement file upload. Please check W3Schools Tutorial for instructions.
The main idea is to get file path from $_FILES["file"]["tmp_name"]:
$fp = fopen($_FILES["file"]["tmp_name"], "r");
If you store your file statically in your web project folder, you can use the path including your DOCUMENT_ROOT:
$path = $_SERVER["DOCUMENT_ROOT"] + 'relative/path/to/your/file/' + 'people.csv';
You can obtain the path of the current file via the __DIR__ magic constant;
Magic constants
In your case;
echo __DIR__ . 'people.csv';
Also, you may consider using the built-in CSV functions that PHP offers;
http://php.net/manual/en/function.fgetcsv.php
If you want to skip processing the file via PHP altogether, MySQL offers ways to directly import data from a CSV file;
https://dev.mysql.com/doc/refman/5.5/en/load-data.html
i am creating an under construction page where users can leave their email to be notified when the site launch
i need to add those emails to a text file called list.txt
my question is two parts
how can i add user#example.com to the text file ?
and how i can later delete certain email from a text file ?
thanks for help
You'd be better off using a database because these operations can step on each other.. but:
Add:
$fp = fopen("list.txt","a"); //a is for append
fputs($fp,"user#example.com" . "\n");
fclose($fp);
Remove:
$file = file_get_contents("list.txt");
unlink("list.txt"); //delete existing file
$fp = fopen("list.txt","w"); //w is for write/new
$lines = split("\n",$file);
while (list(,$email) = each($lines)) {
if ($email != "user#example.com") fputs($fp,$email . "\n");
}
Again... highly recommended to use a database... this is not optimal.
As for saving, you can fopen() in appending mode and just fwrite() to it. As for deleting a certain email: you'll have to load the whole file as a string and save it to file (effectively replacing the entire contents). Without some elaborate locking mechanism a race condition can occur when saving the file, causing you to lose the / a latest signup(s).
I would recommend a simple drop-in sqlite database (or another database if you already have one in production), so you can easily save & delete certain emails, and locking / avoiding race conditions is done automatically for you. If you still need a text file for some other purpose, export the subscription list to that file before using it.
I have a config.inc file in a web application that I am building. It contains an array with configuration values for things like the MySQL database, etc. I would like these to be entered by using a simple form, that asks for the server, login/password for the database, etc, then these get written to the configuration file.
Is there a preferred method of doing this? I am not sure how to write to a file, and update an array.
You just want writing, correct? Is it a serialized array or is it parsed?
One way to read a config file is parse_ini_file(). I wouldn't necessarily call it preferred, but it's a method. You'd still need to write the file.
Another way would to write a "config.inc.php" and just include it in, to write it you'd just output actual PHP code (e.g. $var = "myval";).
This is a way you could write a simple "output" function that took an array of configuration values and output them as name=value, assuming $config was an associative array.
foreach ($config as $name => $value) {
$output .= $name . '=' . $value . "\n";
}
if (!file_put_contents($filename, $output)) {
die("Error writing config file.");
}
There's a lot of decent ways to do it. It's really based on your requirements. Does it need to be in a specific format or do you have leeway?
It is not recommended to modify PHP configuration files via your application, you should use CSV files or a database table.
In case you want to save it in a CSV file then I suggest you keep a CSV file for each configuration type (e.g CSV file for database configurations) and always overwrite the previous one using file_put_contents
Save data example:
$csvStructure = array("dbUser","dbPassword","dbHostname","dbPort"); // array used for both loading data and saving it
$csvData = array();
foreach ($csvStructure as $field) {
$csvData[] = $_POST[$field]; // so it'd get $_POST["dbUser"],$_POST["dbPasword"], etc..
}
file_put_contents("filename",implode("\t",$csvData));
Load data example:
$csvStructure = array("dbUser","dbPassword","dbHostname","dbPort"); // array used for both loading data and saving it
$dbConfig = array();
$csvData = explode("\t",file_get_contents("filename"));
foreach ($csvStructure as $key => $field) { // $key would have the location of the requested field in our CSV data (0,1,2, etc..).
$dbConfig[$field] = $csvData[$key]; // populate $dbConfig["dbUser"],$dbConfig["dbPasword"], etc..
}
I believe using an ini file is a wise option, because user, password, schema, paths, etc. are things that usually will be modified by hand, so using var_export isn't because modifying it by hand it's not so clean and may crash your application if you make a mistake in the PHP syntax.
But parsing big ini files can be expensive, so it would be OK to cache the ini with var_export() or serlialize(). It's a better choice, I think, and read the ini only when the cache file doesn't exists.
PHP has a dedicated function for this, its called var_export();
Just do:
file_put_contents("config.php",var_export($config,true));
Well, to write a file, fwrite() php function does exactly what you want. From its PHP.NET documentation page (see example below).
Now, on the question as to what to output to that file - I'm assuming that file will have to be included as a configuration .php file into the rest of the project. I'm imagining you'll do something like this - where you're creating strings with PHP code on the fly, based on the submitted form:
$strDatabaseConfig = "\$databaseConfig = array('" . $_POST['login'] . "," . $_POST['password'] . "');";
And here's the snippet for fwrite:
$filename = 'test.txt';
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
Here's one way: wp-admin/setup-config.php from WordPress.
I prefer to have a file with a bunch of define statements.
These are constants globally available (and of course immutable) which is what you need for configuration settings.
Constants offer better memory management and efficiency in reading as they don't need the extra memory required by a variable so that it can be changed.
Let's say your config.inc file looks like this:
$config = array(
'blah' => 'mmm',
'blah2' => 'www',
//...
);
You want to update it, so you create a simple form, fill text fields with current values. PHP script that overwrites current configuration could looks like this:
$newConfig = ...; // data from form - of course validate it first
$config = ...; // data from config.inc
$config = array_merge($config, $newConfig);
file_put_contents('config.inc', '<?php $config = ' . var_export($config, true));
And you're done.