Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 days ago.
Improve this question
I'm using php to automate a backup process of my databases, process runs with no error and create the .sql.gz files, but all of them are of a fixed size of 350 byte, useless. I'm using exec() function to read the return value and get the output from mysqldump.
return is always 0 and I cannot print the output to see if there is an error.
This is the code I'm using:
#Process begins
$conn = new mysqli("localhost", $user, $pwd);
$result = $conn->query("SHOW DATABASES");
if ($conn->affected_rows > 0){
$conn->close();
while( $db = mysqli_fetch_row($result)){
$return=null;
$output=null;
if (($db[0]!="information_schema") && ($db[0]!="mysql")) {
$filename = "$targetdir/$db[0]-$filetime.sql";
$cmd = "mysqldump -u $user -p$pwd $db | gzip > $filename.gz";
exec($cmd, $output, $return);
if ($return==0) {
file_put_contents($logfile, serialize($output), FILE_APPEND);
} else {
file_put_contents($logfile, $return, FILE_APPEND);
file_put_contents($logfile, serialize($output), FILE_APPEND);
}
}
}
}else{
file_put_contents($logfile, "No databases found to backup", FILE_APPEND);
}
exit(0);
If anybody can help, specially to read the output I will gladly appreciate it
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to build a webhook receiver.
I created this simple PHP-Script and uploaded it to my webhost:
$raw_payload = file_get_contents('php://input', true);
$payload = json_decode($raw_payload, true);
if($payload){
$myfile = fopen("log.txt", "a+") or die("Unable to open file!");
fwrite(date('Y-m-d H:i:s', time()).PHP_EOL);
fclose($myfile);
}
then, in the App who sends the Webhook (actually Wekan) I entered the URL to the file:
http://myurl.com/dir/receiver.php
I sent some webhooks then, but nothing is written into the file.
The webhooks were sent correctly (I tested it with a webhook-tester and received some correct data there but not in my php-file).
The webhook data wich should be send looks like this (JSON):
{
text: '{{wekan-username}} moved "{{card-title}}" from "{{old-list-name}}" to "{{new-list-name}}"\nhttp://{{wekan-host}}/b/{{board-id}}/{{board-name}}/{{card-id}}',
cardId: '{{card-id}}',
listId: '{{new-list-id}}',
oldListId: '{{old-list-id}}',
boardId: '{{board-id}}',
user: '{{wekan-username}}',
card: '{{card-title}}',
description: 'act-moveCard'
}
What am I doing wrong here? (Can it be a problem with my webhost? The webhook-tester uses https, but for my php-file Im using http, does this cause the error?)
Your not passing the file resource handle to fwrite().
Then perhaps make your code more verbose, and keep an eye on the error log:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$raw_payload = file_get_contents('php://input');
$payload = json_decode($raw_payload, true);
if (is_array($payload)) {
$fh = fopen("log.txt", "a+");
if ($fh) {
fwrite($fh, date('Y-m-d H:i:s', time()).PHP_EOL);
fclose($fh);
} else {
trigger_error("Unable to open file!");
}
} else {
trigger_error("Invalid payload!");
}
} else {
trigger_error("Invalid request!");
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm writing a php function which must open and read line by line the content of a txt file saved in the same directory of the php file and checks if the first value of the line is equal to a number which I give it as a parameter of the function and if this number isn't in the file I will create a new line with the number followed by another information separated by a space. This funcion is in a webhook file of a Facebook Messenger bot and it doesn't work if the function isn't commented out and I don't know why because I checked the code several times and I haven't found the error.
The code of the function is:
function messagesNumber($userid)
{
$fo = ("url of the files", "a+");
$found = false;
if($fo) {
$line = fgets($fo);
while(! feof($fo) && $found == false) {
$words = explode(" ", $line, 2);
if($words[0] == $userid) {
$found = true;
$words[1] += 1;
$messagges = $words [1];
str_replace($line, $words, $fo);
}
$line = fgets($fo);
}
if(! $found) {
fprintf($fo, "%d 1", $userid);
$messages = 1;
}
}
else
$messagges = -1;
fclose($fo);
return $messagges;
}
It returns: -1 if the file can't be open or an integer that coincides with the second number of every line.
It is an example of the txt file:
282318736127 5
827813487273 3
...
The first number is the id which Facebook assign to every user and the second number is a counter who counts how many messages a user writes to the bot and I increase this last number every times that a specific user sends a message to the bot.
easy ... your missing a $
if(! $found) {
fprintf($fo, "%d 1", $userid);
messages = 1; // missing $
}
to
if(! $found) {
fprintf($fo, "%d 1", $userid);
$messages = 1;
}
okay edit ...
easy your missing an fopen
$fo = ("url of the files", "a+");
to
$fo = fopen("url of the files", "a+");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'am searching for an Tool in !PHP! that will return maybe an Array or just something, with which i can lookup if there are any Changes.
Its beeing used to get Data from MySQL turning it into a JSON File and if the JSON File differs from the MySQL Output, then the file should be new generated.
So i just need to type a Function or something else, if there's been changes made, (Yes/No) do X.
Do you guys know any Tool that would work with my Idea?
Thanks for Helping me out.
Your question is vague but, if I understood right, here is an idea:
<?php
class Tool {
var $db; // you will have to provide a valid db connection
function __construct (& $db) {
$this->db = $db;
}
public function execute ($query, $file){
$arr = $this->fetch($query);
$dbJSON = json_encode($arr);
$fileJSON = file_get_contents($file);
if( md5($dbJSON) != md5($fileJSON) ){
$this->write($dbJSON, $file);
}
}
protected function fetch($sql) {
$arr = array();
$result = $this->db->query($sql);
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
$arr[] = $row;
}
// Free result set
$result->close();
}
return $arr;
}
protected function write ($content, $file) {
$fp = fopen('w+', $file);
fwrite($fp, $content);
fclose($fp);
}
}
?>
and a use case:
<?php
$db = new mysqli('localhost','user','pass','database');
// Check for errors
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}else{
$tool = new Tool($db);
$tool->execute('SELECT * FROM users','/com/user.json');
}
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
someone could please tell why this code does not work? it does not create data.txt. and the file does not save anything.
<?php
$txt = "data.txt";
if(isset($_POST['info1']) && isset($_POST['info2']) && isset($_POST['info3']) && isset($_POST['info4']) && isset($_POST['info5']) && isset($_POST['info6'])) {
// check if both fields are set
//open file in read mode to get number of lines
$handle = fopen($txt, 'r');
//check file opened
if($handle) {
//get number of lines
$count = 1;
while(fgets($handle) !== false) {
$count++;
}
fclose($handle);
//open in append mode.
$handle = fopen($txt, 'a');
//prepare data to be writen
$txt = $count . ' ' . $_POST['info1'].'/'.$_POST['info2'].'/'.$_POST['info3'].'/'.$_POST['info4'].'/'.$_POST['info5'].'/'.$_POST['info6']. "\r\n";
//write data to file
fwrite($handle,$txt);
fclose($handle);
}
}
?>
In fopen function mode param with value r doesn't make file being created.
See fopen documentation.
Also the issue could be write permissions for user that runs that PHP script.
These is only my guesses. Anyway you should configure your PHP (in dev environment) to show you every error/warning in order to debug such issues. You can do that in php.ini file or straight in PHP file with this code:
error_reporting(E_ALL);
ini_set('display_errors', 1);
You are first trying to open the file read only, and if that succeeds you close it and open it again with write access.
This will only work if the file already exists.
If the file doesn't exist, your text will not get written to it.
Move the closing bracket if if ($handle) { in front of the second fopen call.
if($handle) {
//get number of lines
$count = 1;
while(fgets($handle) !== false) {
$count++;
}
fclose($handle);
}
//open in append mode.
$handle = fopen($txt, 'a');
//prepare data to be writen
$txt = $count . ' ' . $_POST['info1'].'/'.$_POST['info2'].'/'.$_POST['info3'].'/'.$_POST['info4'].'/'.$_POST['info5'].'/'.$_POST['info6']. "\r\n";
//write data to file
fwrite($handle,$txt);
fclose($handle);
by the way, you can reduce this to three lines of code.
$lines = file($txt);
$count = sizeof($lines);
file_put_contents($txt, $count . ' ' . $_POST['info1'].'/'.$_POST['info2'].'/'.$_POST['info3'].'/'.$_POST['info4'].'/'.$_POST['info5'].'/'.$_POST['info6']. "\r\n", FILE_APPEND);
to check for multiple variables in isset create only one isset to check all veriables
change this
if(isset($_POST['info1']) && isset($_POST['info2']) && isset($_POST['info3']) && isset($_POST['info4']) && isset($_POST['info5']) && isset($_POST['info6']))
to
if(isset($_POST['info1'],$_POST['info2'],$_POST['info3'],$_POST['info4'],$_POST['info5'],$_POST['info6']))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to generate a csv file, using data from a database. How does one do this in PHP CodeIgniter framework?
Take a look at this answer of mine and you will know exactly how you can do it.
Reports in Codeigniter
You need to use Codeigniter Database Utility Class
And need to return query instead of result_array or result
function query_to_csv( $query, $filename, $attachment = false, $headers = true) {
if($attachment) {
// send response headers to the browser
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);
$fp = fopen('php://output', 'w');
} else {
$fp = fopen($filename, 'w');
}
$result = mysql_query($query);
if($headers) {
// output header row (if at least one row exists)
$row = mysql_fetch_assoc($result);
if($row) {
fputcsv($fp, array_keys($row));
// reset pointer back to beginning
mysql_data_seek($result, 0);
}
}
while($row = mysql_fetch_assoc($result)) {
fputcsv($fp, $row);
}
fclose($fp);
exit;
}