codeigniter ftp get filesize - codeigniter

How can i get remote file's file size with codeigniter?
after making a connection , i want to get properties.xml file's size. in ordinery ftp i would get it with no problem but since i start using codeigniter it is kinda difficult.
this is in procedurel php ;
$remote_filesize = ftp_size($conn_id, "properties.xml");
but how can i do it in codeigniter?
if($this->ftp->connect($config))
{
/*here some magic code*/
}

this a small example
Controller_ftp.php
class Controller_ftp extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->library('ftp');
}
publilc function get_ftp_info(){
$config['hostname'] = 'ftp.example.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug'] = TRUE;
foreach($this->ftp->list_files('/public_html/') as $item){
$size = filesize($item);
echo "name => $item - size => $size - etc <br>";
clearstatcache();
}
}
}
If you want to use $this->ftp->file_size($file), you need to extend ftp library and create the function retunring filesize($item);

ok i went to library->ftp.php and basically added a function
public function get_conn_id()
{ return $this->conn_id;}
and later all i need to do was calling the function like this
$conn_id = $this->ftp->get_conn_id();
$remote_filesize = ftp_size($conn_id, "properties.xml");

Related

Method behavior with codeigniter parameters

Needing help understanding codeigniter 3 behavior
I'm not understanding the controller behavior very well and I ask for help
class Simulado extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('funcoes_helper');
$this->load->helper('file');
$this->load->library('session');
$this->load->library('controle_acesso');
$this->load->model('resumoapp_model', 'resumo');
$this->load->model('homeapp_model', 'homeapp');
$this->load->model('simulado_model', 'simulado');
//permissão para acessar app
$this->controle_acesso->acesso();
$this->output->enable_profiler(TRUE);
}
public function index() {
$dados = '0'//todas as materias;
$bdinsert = $this->simulado->set_bd($dados);
$this->load->view('prova',$dados)
}
public function materia () {
$dados = 1; //idamateria
$bdinsert = $this->simulado->set_bd($dados);
$this->load->view('prova',$dados)
}
}
model
public function set_bd ($dados) {
$data = [
'id_mat' = $dados
];
$this->db->insert('simulados', $data);
}
routes.php
$route['default_controller'] = 'home';
$route['404_override'] = 'my404';
$route['translate_uri_dashes'] = FALSE;
$route['app'] = "/app/home";
$route['site'] = "/home";
when accessed through the link http://localhost/escola/app/simulado, the system behaves in the desired way, it writes the value zero once in the database, for example.
accessing the url we have the method of the class, simulated, http://localhost/escola/app/simulado/materia/1/exatas-matematica calls the method in the correct way, but when writing to the database it writes the code of the matter.
I did a test and put the same code of the article method in the index and called the url, and it worked perfectly, writing only once the code of the article in the database.
I need to know why it is recording 3 times in the database when accessing the url is a method of the class and has parameters.

Laravel bitly error The response does not contain a shortened link

I use pacakage :: https://github.com/Shivella/laravel-bitly
I try to create bitly link 5000 link but It's always error :The response does not contain a shortened link
public function test(Request $request) {
if ($request->isMethod('post')) {
$barcode = Barcode1::whereNull('shortlink')->get();
foreach ($barcode as $b) {
$url = app('bitly')->getUrl('https://www.test.com/shrotern/'.$b->encode);
$update = Barcode1::find($b->id);
$update->shortlink = $url;
$update->save();
}
}
return view('test');
}
How can I fix this
It's work fine on a real domain or you can create a virtual host.

Loading view inside a Library, issues with cached vars

I am trying to implement a widgets library using load->view. I know I can use include to call directly the file and avoid the vars cache issues but just wondering why it does not work.
Here is how I have structured my code:
My Controller:
class Page extends MY_Controller {
public $data = array();
public function __construct() {
parent::__construct();
...
$this->load->library('widgetmanager');
}
public function index($slug = '') {
echo $this->widgetmanager->show(2);
echo $this->widgetmanager->show(1);
}
}
My Library
class WidgetManager
{
private $CI;
public function __construct()
{
$this->CI = & get_instance();
}
public function show($widget_id) {
$data = array();
$widget_id = (int)$widget_id;
$this->CI->db->select('*');
$this->CI->db->from('widget');
$this->CI->db->where('id', $widget_id);
$query = $this->CI->db->get();
$item = $query->row_array();
$data['widget_title'] = $item['title'];
$data['widget_content'] = $item['content'];
$widget = $this->CI->load->view('widget/'.$item['source'], $data, TRUE);
$data['widget_title'] = '';
$data['widget_content'] = '';
$this->CI->load->view('widget/'.$item['source'], $data);
return $widget;
}
}
widget 1: Calls widget/content
widget 2: Calls widget/banner
What is happening is, the vars set on the first widget call (they are same name as second widget call), get cached, meaning values from the first call are passed to same call. It is weird because are different views.
I have tried:
Using clear_vars(): $this->CI->load->clear_vars(), before and after doing load->view on the library.
Calling load->view with empty array, null, etc
Tried to add a prefix with the widget slug to the vars (that works, but I have to send in some way the prefix to the view, so it is not possible due cache issue)
Any ideas?
Here is what should work.
(I took the liberty of simplifying your database call making it require much less processing.)
public function show($widget_id)
{
$data = array();
$widget_id = (int) $widget_id;
$item = $this->CI->db
->get_where('widget', array('id' => $widget_id))
->row_array();
$data['widget_title'] = $item['title'];
$data['widget_content'] = $item['content'];
$widget = $this->CI->load->view('widget/'.$item['source'], $data, TRUE);
//clear the cached variables so the next call to 'show()' is clean
$this->CI->load->clear_vars();
return $widget;
}
On further consideration The call $this->CI->load->clear_vars(); is probably pointless because each time WidgetManager::show() is called the $data var is recreated with exactly the same keys. When the $data var is passed to load->view the new values of $data['widget_title'] and $data['widget_content'] will replace the values in the cached vars using those keys.

Login Throttling Function with Active Record

I have a function that I found that was wrote to use PDO, I modified it to use codeigniters active record db class. Everything work EXCEPT when I place the code within a function like so:
function login_attempt_count() {
$seconds = 10;
// First we delete old attempts from the table
$oldest1 = strtotime(date('Y-m-d H:i:s').' - '.$seconds.' seconds');
$oldest2 = date('Y-m-d H:i:s',$oldest1);
$del_data = $oldest2;
$this->db->where('when <', $del_data);
$this->db->delete('Login_Attempts');
// Next we insert this attempt into the table
$data = array(
'ip' => $_SERVER['REMOTE_ADDR'],
'when' => date("Y-m-d H:i:s"));
$this->db->insert('Login_Attempts', $data);
// Finally we count the number of recent attempts from this ip address
$count = 'SELECT count(*) as number FROM Login_Attempts WHERE ip = ?';
$num = $this->db->query($count, $_SERVER['REMOTE_ADDR']);
if ($num->num_rows() > 0){
foreach($num->result() as $attempt){
$attempts = $attempt->number;
return $attempts;
}
}
}
using it like this:
$max_attempts = 3;
if(login_attempt_count() <= $max_attempts) {
echo 'login';
}else{
echo 'To many attempts';
}
or this:
$a = login_attempt_count();
Causes the rest of the page to not load. So this indicates an error.
Again if I use the code within the function, outside the function, it works as expected.
Or if there is a completely better and more secure method that I should be using I am open for suggestions. Thanks!
Ok here I am answering my own question. ;) The function works fine! However, testing it in a view causes an error? After placing the function within my model and calling the function within my controller works great. Using it like so:
class Auth extends CI_Controller{
function login(){
$max_attempts = 3;
if($this->auth_model->login_attempt_count() <= $max_attempts) {
//Do something
}else{
//Something else
}
}//End Function
}//End Class

Codeigniter passing "Name/Name" as one parameter in a url

class Barcode extends CI_Controller
{
function index($barcode, $text='', $format="PNG", $quality=100, $width=160, $height=80, $type=1)
}
I have the following function and I want to pass the following in the URL:
http://localhost/index.php/barcode/index/1/Test%2FTest/PNG/100/256/80/1
But I get a 404 when trying to do this.
Don't you need to get all the parts of the GET request via $this->uri->segment('{n}') first?
I might be wrong, but I don't think the way you are doing it is right. If I was to code this I would do the following:
function index()
{
$barcode = $this->uri->segment('3');
$text = $this->uri->segment('4');
$format = $this->uri->segment('5');
$quality = $this->uri->segment('6');
$width = $this->uri->segment('7');
$height = $this->uri->segment('8');
$type = $this->uri->segment('9');
// continue with your code
}

Resources