I want to load csv file data to extract the urls from CSV and check for the title tag for all the urls and update the urls with corresponding title tags in a new csv. But while I try to add data to the csv all the urls are getting listed but only the title of the last url is displayed in the CSV. I have tried different ways to overcome this problem but unable to do so.
Here is my code:
<?php
ini_set('max_execution_time', '300'); //300 seconds = 5 minutes
ini_set('max_execution_time', '0');
include('simple_html_dom.php');
// if (isset($_POST['resurl'])) {
// $url = $_POST['resurl'];
if (($csv_file = fopen("old.csv", "r", 'a')) !== FALSE) {
$arraydata = array();
while (($read_data = fgetcsv($csv_file, 1000, ",")) !== FALSE) {
$column_count = count($read_data);
for ($c = 0; $c < $column_count; $c++) {
array_push($arraydata, $read_data[$c]);
}
}
fclose($csv_file);
}
$title = [];
foreach ($arraydata as $ad) {
$ard = [];
$ard = $ad;
$html = file_get_html($ard);
if ($html) {
$title = $html->find('title', 0)->plaintext;
// echo '<pre>';
// print_r($title);
}
}
$ncsv = fopen("updated.csv", "a");
$head = "Url,Title";
fwrite($ncsv, "\n" . $head);
foreach ($arraydata as $value) {
// $ar[]=$value;
$csvdata = "$value,$title";
fwrite($ncsv, "\n" . $csvdata);
}
fclose($ncsv);
I've changed the code so that you write the CSV file as you read the HTML pages. This saves having another loop and an extra array of titles.
I've also changed it to use fputcsv to write the data out as it sorts ot things like escaping values etc.
// Open file, using w to clear the old file down
$ncsv = fopen('updated.csv', 'w');
$head = 'Url,Title';
fwrite($ncsv, "Url,Title" . PHP_EOL . $head);
foreach ($arraydata as $ad) {
$html = file_get_html($ad);
// Fetch title, or set to blank if html is not loaded
if ($html) {
$title = $html->find('title', 0)->plaintext;
} else {
$title = '';
}
// Write record out
fputcsv($ncsv, [$value, $title]);
}
fclose($ncsv);
I was able to solve it finally.
Here is the updated code:
<?php
ini_set('max_execution_time', '300'); //300 seconds = 5 minutes
ini_set('max_execution_time', '0');
include('simple_html_dom.php');
// if (isset($_POST['resurl'])) {
// $url = $_POST['resurl'];
if (($csv_file = fopen("ntsurl.csv", "r", 'a')) !== FALSE) {
$arraydata = array();
while (($read_data = fgetcsv($csv_file, 1000, ",")) !== FALSE) {
$column_count = count($read_data);
for ($c = 0; $c < $column_count; $c++) {
array_push($arraydata, $read_data[$c]);
}
}
fclose($csv_file);
}
// print_r($arraydata);
$title=[];
$ncsv=fopen("ntsnew.csv","a");
$head="Website Url,title";
fwrite($ncsv,"\n".$head);
foreach($arraydata as $ad)
{
$ard = [];
$ard = $ad;
$html = file_get_html($ard);
if ($html) {
$title = $html->find('title', 0)->plaintext;
echo '<pre>';
print_r($title);
$csvdata="$ard,$title ";
fwrite($ncsv,"\n".$csvdata);
}
}
// fclose($ncsv);
Am trying to read multiple file and display the last line of each file but my code is not working very fine i don't know what am doing wrong.
<?php
if(isset($_GET['fid'], $_GET['timestamp']) && !empty($_GET['fid'])){
$lastmodif = isset( $_GET['timestamp']) ? $_GET['timestamp']: 0 ;
$fl_session = preg_replace("/[^0-9=]+/", "", $_GET['fid']);
if(strpos($fl_session, '==') !== false) {
$fl_session = explode('==', $fl_session);
foreach($fl_session as $fn){
$filename = dirname(__FILE__).'/logs/foo_'.$fn.'.txt';
$currentmodif = filemtime($filename);
while($currentmodif <= $lastmodif) {
usleep(10000);
clearstatcache();
$currentmodif = filemtime($filename);
}
if(file_exists($filename)){
$myfile = fopen($filename, "r") or die("Unable to load readme file!");
if($myfile){
$log = fread($myfile, filesize($filename));
fclose($myfile);
}
$log = explode("\n", trim($log));
for ($i = 0; $i < count($log); $i++) {
$log[$i] = trim($log[$i]);
$log[$i] = explode('|', $log[$i]);
}
foreach ($log as $logline) {
$response['id'] = trim($logline['0']);
$response['type'] = trim($logline['3']);
}
}
$response[$fn]['timestamp'] = $currentmodif;
//$response['timestamp'] = $currentmodif;
//var_export($response);
echo json_encode($response);
}
}
}
In my GET['fid'] parameter it contain something like this 12345==09765==87765, so i will explode it and loop on every id to locate the file name foo_12345.txt, foo_09765.txt, foo_87765.txt. This current code returned something like this from one txt file and ignored other files.
{ "id":"58287469", "type":"client", "8942180223":{
"timestamp":1505409432 } } { "id":"53094615", "type":"client",
"8942180223":{ "timestamp":1505409432 }, "4582422934":{
"timestamp":1505409420 } }
I would like to split a very large CSV file (20 000 lines) to be able to put everything in my MySQL database (after I use cronjob for load the php file every hours).
So I have found code to split my file :
$inputFile = 'Shipping.csv';
$outputFile = 'output';
$splitSize = 1000;
$in = fopen($inputFile, 'r');
$rowCount = 0;
$fileCount = 1;
while (!feof($in)) {
if (($rowCount % $splitSize) == 0) {
if ($rowCount > 0) {
fclose($out);
}
$out = fopen($outputFile . $fileCount++ . '.csv', 'w');
}
$data = fgetcsv($in);
if ($data)
fputcsv($out, $data);
$rowCount++;
}
fclose($out);
I have tried to do this but they don't work :
require_once dirname(__DIR__).'/pdo.php';
$inputFile = 'Shipping.csv';
$outputFile = 'output';
$splitSize = 1000;
//open uploaded csv file with read only mode
$in = fopen($inputFile, 'r');
//skip first line
fgetcsv($in);
$rowCount = 0;
$fileCount = 1;
while (!feof($in)) {
if (($rowCount % $splitSize) == 0) {
if ($rowCount > 0) {
fclose($out);
}
$out = fopen($outputFile . $fileCount++ . '.csv', 'w');
}
$data = fgetcsv($in);
if ($data){
fputcsv($out, $data);
}
while(($line = fgetcsv($out)) !== FALSE){
//check whether member already exists in database with same email
$prevQuery = "SELECT id FROM shipbp WHERE license = '".$line[1]."'";
$prevResult = $bdd->query($prevQuery);
if($prevResult->rowCount() > 0)
{
$bdd->query("UPDATE shipbp SET license = '".$line[0]."'");
}
else{
$bdd->query("INSERT INTO shipbp (license) VALUES ('".$line[0]."')");
}
}
$rowCount++;
}
fclose($out);
CSV file look like :
P135460,002,00003,250,AS44563,0.35,,Blabla,17/3/2017 00:00:00,SB,Blabla
How I can do ?
Thank you for your help
My PHP script is always connection (IRC Bot) but say the bot runs into an error and gets disconnected I want it to reconnect to the server.
Right now the bot is running local so I’m able to restart the bot using a bat file but if I host it somewhere how would I initiate a reconnection?
For example my code now is:
<?php
set_time_limit(0);
include 'config.php';
fputs($socket,"USER TFBot TFBot.PugBot TFBot :The TitanFall.Pug Bot\n");
fputs($socket,"NICK $bnick\n");
$commands = array (
"${cmdsym}commands",
"${cmdsym}add",
"${cmdsym}join",
"${cmdsym}last",
"${cmdsym}coms",
"${cmdsym}voip",
"${cmdsym}promote",
"${cmdsym}need",
"${cmdsym}list",
"${cmdsym}pick",
"${cmdsym}remove",
);
while (1) {
while($data=fgets($socket,128)) {
$get = explode(' ', $data);
if (stripos( $get[1], ':Closing' ) !== false) {
echo "Reconnecting in 10 seconds\n";
sleep(10);
passthru("Startbot.bat");
}
if (stripos( $get[1], 'NICK' ) !== false) {
$nick = explode(':',$get[0]);
$nick = explode('!',$nick[1]);
$nick = $nick[0]; //User who entered the command
$nuser = $nick;
$wnuser = preg_replace('/^\\:/','', $get[2]);
$tnuser = rtrim($wnuser);
if (chnickchange("tready.txt",$nuser)) {
fputs($socket,"CNOTICE $tnuser $jchan : Your name was updated from: $nuser to: $tnuser on the PUG List. - $chan.\n");
nickchange("tready.txt",$nuser,$tnuser);
nickchange("pready.txt",$nuser,$tnuser);
nickchange("sready.txt",$nuser,$tnuser);
nickchange("militia.txt",$nuser,$tnuser);
nickchange("imc.txt",$nuser,$tnuser);
nickchange("captains.txt",$nuser,$tnuser);
nickchange("pick.txt",$nuser,$tnuser);
}
}
if (stripos( $get[1], 'QUIT' ) !== false) {
$nick = explode(':',$get[0]);
$nick = explode('!',$nick[1]);
$nick = $nick[0]; //User who entered the command
$partuser = $nick;
$line = file("tready.txt", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
if (array_search($partuser, $line) !== FALSE) {
fputs($socket,"PRIVMSG $chan : $nick was removed from the PUG List. - QUIT.\n");
$DELETE = $partuser;
$datap = file("tready.txt");
$out = array();
foreach($datap as $line) {
if(trim($line) != $DELETE) {
$out[] = $line;
}
}
$fp = fopen("tready.txt", "w+");
flock($fp, LOCK_EX);
foreach($out as $line) {
fwrite($fp, $line);
}
flock($fp, LOCK_UN);
fclose($fp);
$datap = file("sready.txt");
$out = array();
foreach($datap as $line) {
if(trim($line) != $DELETE) {
$out[] = $line;
}
}
$fp = fopen("sready.txt", "w+");
flock($fp, LOCK_EX);
foreach($out as $line) {
fwrite($fp, $line);
}
flock($fp, LOCK_UN);
fclose($fp);
sleep(1);
$listnp = file("tready.txt", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
$nplayers = $pugmax - count($listnp);
if ($nplayers !== 0) {
fputs($socket,"PRIVMSG $chan : ${afcolor}(${scolor}$nplayers needed to begin${afcolor})\n");
}
}
}
if ($get[0] == "PING") {
fputs ($socket, "PONG ".$get[1]."\n");
}
if (stripos( $data, 'Nickname is already in use.' ) !== false) {
$bnick = $bnick . "|2";
fputs($socket,"NICK $bnick\n");
}
if (stripos( $data, 'End of /MOTD command.' ) !== false) {
fputs($socket,"PRIVMSG Q#CServe.quakenet.org : AUTH $authname $authpass\n");
sleep(1);
fputs($socket,"MODE $bnick +x\n");
//Enter the channel you want to use your bot on.
sleep(2);
fputs($socket,"JOIN $jchan\n");
}
if (substr_count($get[2],"#")) {
$nick = explode(':',$get[0]);
$nick = explode('!',$nick[1]);
$nick = $nick[0]; //User who entered the command
$nhost = explode('!', $get[0]); //User Hostname for private commands
$nhost = $nhost[1];
$chan = $get[2]; //the channel the bot is in
$num = 3; //If you observe the array format, actually text starts from 3rd index.
if ($num == 3) {
$split = explode(':',$get[3],2);
$text = rtrim($split[1]); //trimming is important. never forget.
// Bot Public Commands
/* if (stripos( $get[1], 'PART' ) !== false) {
$partuser = $nick;
removeuser($partuser,"tready.txt","0");
removeuser($partuser,"pready.txt","1");
removeuser($partuser,"militia.txt","1");
removeuser($partuser,"imc.txt","1");
removeuser($partuser,"captains.txt","2");
removeuser($partuser,"pick.txt","2");
} */
if (stripos( $get[1], 'PART' ) !== false) {
$partuser = $nick;
$line = file("tready.txt", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
if (array_search($partuser, $line) !== FALSE) {
fputs($socket,"NOTICE $partuser : You were removed from the PUG List. - $chan.\n");
fputs($socket,"PRIVMSG $chan : ${afcolor}$partuser ${scolor}was removed from the pug list${afcolor}. - ${scolor}Reason${afcolor}: PART.\n");
$DELETE = $partuser;
$datap = file("tready.txt");
$out = array();
foreach($datap as $line) {
if(trim($line) != $DELETE) {
$out[] = $line;
}
}
$fp = fopen("tready.txt", "w+");
flock($fp, LOCK_EX);
foreach($out as $line) {
fwrite($fp, $line);
}
flock($fp, LOCK_UN);
fclose($fp);
$datap = file("sready.txt");
$out = array();
foreach($datap as $line) {
if(trim($line) != $DELETE) {
$out[] = $line;
}
}
$fp = fopen("sready.txt", "w+");
flock($fp, LOCK_EX);
foreach($out as $line) {
fwrite($fp, $line);
}
flock($fp, LOCK_UN);
fclose($fp);
sleep(1);
$listnp = file("tready.txt", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
$nplayers = $pugmax - count($listnp);
if ($nplayers !== 0) {
fputs($socket,"PRIVMSG $chan : ${afcolor}(${scolor}$nplayers needed to begin${afcolor})\n");
}
}
}
Seems like you have passthru enabled.
A simple script would be:
<?PHP
passthru('c:/path/to/file/startbot.bat');
Which you're already doing. This will call the batch file, and restart the script.
Or,
<?PHP
passthru('c:/path/to/php/php c:/path/to/script/script.php');
How can I add "fake additional online users". Lets say I have real 30 but I need additional 30 fake. Any ideas how to tweak this up ?
function GetLoggedInCount()
{
$cachefile = './cache/playercount.txt';
$db = $this->database[ADB];
if (!file_exists($cachefile) || (time() - 300) >= filemtime($cachefile))
{
$count = 0;
$num_rows = $db->doQuery('SELECT COUNT(strCharID) as pCount FROM CURRENTUSER');
if ($num_rows == 1)
{
$row = $db->doRead();
$count = $row['pCount'];
$fp = fopen($cachefile, 'w');
fwrite($fp, $count);
fclose($fp);
}
return $count;
}
else if ((time() - 300) < filemtime($cachefile))
{
$fp = fopen($cachefile, 'r');
$content = fread($fp, filesize($cachefile));
fclose($fp);
return intval($content);
}
}