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
Related
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');
When I want to do folder for the member who registered into my site Inside this folder is a folder for photos and the main file of the member.
this is problem :-
The first folder is done with the member name successfully but the photo folder and the profile page of the member do not succeed.
thank you .....
this is my code I changed the code by the guy who helped me
but the same problem exists
The first folder is successfully completed
The problem is in
file settings, folders, images, and index
<?php
$us = "";
if(isset($_POST['username'])){ $us= $_POST['username']; }
if(isset($_POST['username'])){
mkdir("../profiles/$us", 0777, true);
mkdir("../profiles/$us/images", 0777, true);
$filename = "../profiles/$us/index.php";
$ourFileName = $filename;
$ourFileHandle = fopen($ourFileName, 'w');
$written = "
<html>
<body>
<?php
echo \"hi man \";
</body>
</html>
";
fwrite($ourFileHandle, $written);
fclose($ourFileHandle);
$myfile = fopen("../profiles/$us/setting.php", "w") or die("Unable to open file!");
fwrite($myfile, $txt);
$txt = "Minnie Mouse\n";
fwrite($myfile, $txt);
fclose($myfile);
}
?>
You cant assign $us = $_POST["username"] inside of brackets and then use $us again later.
Anything inside of brackets is constrainted within those brackets.
You have to define $us = "" outside of the scope of the IF statement on your first statement.
<?php
$us = "";
if(isset($_POST['username'])){ $us= $_POST['username']; }
if(isset($_POST['username'])){
etc etc
}
So I have a JSON file containing basketball player information in the following format:
[{"name":"Lamar Patterson","team":1,"yearsLeft":0,"position":"PG","PPG":17},{"name":"Talib Zanna", "team":1,"yearsLeft":0,"position":"SF","PPG":13.1},....]
I want a user to be a able to add their own custom players to this file. To do this i try the following:
<?php
$json = file_get_contents('json/players.json');
$info = json_decode($json, true);
$info[] = array('name'=>$name, 'team'=>$team, 'yearsLeft'=>4, 'position'=>$position, 'PPG'=>$ppg);
file_put_contents('json/players.json', json_encode($info));
?>
This "sort of" works. But when I check the JSON file, I find that there are 3 new entries rather than 1:
{"name":"","team":null,"yearsLeft":4,"position":"","PPG":""},{"name":"","team":"3","yearsLeft":4,"position":"","PPG":""},{"name":"Jeff","team":null,"yearsLeft":4,"position":"C","PPG":"23"}
assuming $name="Jeff" $team=3 and $ppg=23 (populated via POST submission).
What's going on and how can I fix it?
You could try doing the following:
Untested code
<?php
if(!empty($name) && !empty($team) && !empty($position) && !empty($ppg)) {
$fh = fopen('json/players.json', 'r+') or die("can't open file");
$stat = fstat($fh);
ftruncate($fh, $stat['size']-1);//removes last ] char
fclose($fh);
$fh = fopen('json/players.json', 'a');
$info = array('name'=>$name, 'team'=>$team, 'yearsLeft'=>4, 'position'=>$position, 'PPG'=>$ppg);
fwrite($fh, ','.json_encode($info).']');
fclose($fh);
}
?>
This will append the only the new json to the file instead of opening the file, making php parse all the json and then writing it to the file again. In addition to that it will only store the data if the variables actually contain data.
Try this:
<?php
//get the posted values
$name = $_POST['name'];
$team = $_POST['team'];
$position = $_POST['position'];
$ppg = $_POST['ppg'];
//verify they're not empty
if(!empty($name) && !empty($team) && !empty($position) && !empty($ppg)) {
//Open the file
$fh = fopen('json/players.json', 'r+') or die("can't open file");
//get file info/stats
$stat = fstat($fh);
//final desired size after trimming the trailing ']'
$size = $stat['size']-1;
//file has contents? then remove the trailing ']'
if($size>0) ftruncate($fh, $size);
//close the current handle
fclose($fh);
// reopen the file for append
$fh = fopen('json/players.json', 'a');
//build your data array
$info = array('name'=>$name, 'team'=>$team, 'yearsLeft'=>4, 'position'=>$position, 'PPG'=>$ppg);
//if this is not the first item on file
if($size>0) fwrite($fh, ','.json_encode($info).']'); //append with comma
else fwrite($fh, '['.json_encode($info).']'); //first item on file
fclose($fh);
}
?>
Maybe your php config is not set to convert the post/get variables to global variables. This happened to me a couple of times so I rather create the variables I'm expecting from the post/get request. Also watch out for the page encoding, from personal experience you could be getting empty strings there.
I'm writing a script for a lead contact form that needs to send the first 10 leads to email 1, the second 10 leads to email 2, and so on until it gets to email 4, and then it goes back to email 1.
this is a rotator i have that was built for landing pages, but it rotates 1 each time rather than waiting 10 times, and then rotating. how would I modify this to suit my needs?
Also, it cant happen on every 'refresh' obviously. there would need to be a separate group of code which would go in the action="whatever.php" of the form and thats the code that would increment it.
<?php
//these are the email addresses to be rotated
$email_address[1] = 'email1#email.com';
$email_address[2] = 'email2#email.com';
$email_address[3] = 'email3#email.com';
$email_address[4] = 'email4#email.com';
//this is the text file, which will be stored in the same directory as this file,
//count.txt needs to be CHMOD to 777, full privileges, to read and write to it.
$myFile = "count.txt";
//open the txt file
$fh = #fopen($myFile, 'r');
$email_number = #fread($fh, 5);
#fclose($fh);
//see which landing page is next in line to be shown.
if ($email_number >= count($email_address)) {
$email_number = 1;
} else {
$email_number = $email_number + 1;
}
//write to the txt file.
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $email_number . "\n";
fwrite($fh, $stringData);
fclose($fh);
//include the landing page
echo $email_address[$email_number];
//terminate script
die();
?>
What I understood from your question is there would be form to submit leads and when a lead is submitted, it has to follow your logic. Correct me if I'm wrong.
If that be the case,
use two a text file like track.txt. The initial contents of this text file would be 1,0. Which means leads are sent to first email id 0 times.
So in the action script of the form include the following code.
<?php
$email_address[1] = 'email1#email.com';
$email_address[2] = 'email2#email.com';
$email_address[3] = 'email3#email.com';
$email_address[4] = 'email4#email.com';
$myFile = "track.txt";
//open the txt file
$fh = #fopen($myFile, 'r');
$track = #fread($fh, 5);
#fclose($fh);
$track = explode(",",$track);
$email = $track[0];
$count = $track[1];
if($count >= 10)
{
$count=0;
if($email >= count($email_address))
{
$email = 1;
}
else
{
$email++;
}
}
else
{
$count++;
}
$track = $email.",".$count;
//write to the txt file.
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $track);
fclose($fh);
//send lead to $email
?>
I want to fetch json script and write it to a txt file undecoded, exactly how it was originally. I do have a script that I use that I am modifying but unsure what to commands to use. This script decodes, which is what I want to advoid.
//Get Age
list($bstat,$bage,$bdata) = explode("\t",check_file('./advise/roadsnow.txt',60*2+15));
//Test Age
if ( $bage > $CacheMaxAge ) {
//echo "The if statement evaluated to true so get new file and reset $bage";
$bage="0";
$file = file_get_contents('http://somesite.jsontxt');
$out = (json_decode($file));
$report = wordwrap($out->mainText, 100, "\n");
//$valid = $out->validTo;
//write the data to a text file called roadsnow.txt
$myFile = "./advise/roadsnow.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $report;
fwrite($fh, $stringData);
}
else {
//echo the test evaluated to false; file is not stale so read local cache
//print "we are at the read local cache";
$stringData = file_get_contents("./advise/roadsnow.txt");
}
// if/else is done carry on with processing
//Format file
$data = $stringData
Try this:
// Get JSON Data
$json_data = file_get_contents('http://somesite.jsontxt');
// Write JSON to File
file_put_contents('json_data.txt', $json_data);