php trigger

New topics about Symfony 2 should go here

Moderators: tiagojsag, dcobalt

php trigger

Postby msalsas » Sat Jun 29, 2013 5:09 pm

Hello! I'm doing an application to upload images to the server and manipulate them. My web is already done, but I have a problem.
I'm saving in the server two images per person. I have no problem to remove them when the client do some action. However, I don't know how to remove the images when the client close the browser. I've seen that it can be done using javascript, but I want to do this in the server. Is ther any way to run a php script (that would remove the images), when a session has finished? If it could be possible, I'd pass the id_session as a variable to the script to remove the concrete images.
-----------------------------------------------------
| http://msalsas.com | http://itransformer.es |
-----------------------------------------------------
msalsas
Member
 
Posts: 53
Joined: Fri Jun 21, 2013 2:28 pm

Re: php trigger

Postby tiagojsag » Sat Jun 29, 2013 5:15 pm

Hi,

AFAIK the only simple way to do that is with a cron job +console command.
Tiago Garcia
@tiagojsag

Core Web Developer @ Shopware
http://www.shopware.de
User avatar
tiagojsag
Faithful Member
 
Posts: 885
Joined: Wed Aug 10, 2011 4:58 pm

Re: php trigger

Postby msalsas » Sun Jun 30, 2013 1:24 pm

First of all, I'm sorry because this is not really a symfony topic. I didn't think before.

Well, I'm trying to do the next:

Create a new crontab which execute a script every 5 min that will see the active sessions. Then, it will remove the images which belongs to the non-active session clients.
The first part of the script is the next code:

Code: Select all
function getUsersOnline() {
$handle = opendir(session_save_path());
if ($handle == false) return -1;

while (($file = readdir($handle)) != false) {
if (preg_match("/^sess/", $file)) {
if(time()- fileatime(session_save_path() . ‘/’ . $file) < 1200) { // 1200 secs = 20 minutes session
//then get the active id_session and remove the images
//...
}

}
}
closedir($handle);
}

getUsersOnline();


I get the /var/lib/php5 path from:
Code: Select all
$handle = opendir(session_save_path());


...but there are no files inside. Even if I'm with an active session.
Where then are saved the session files?, and how I get the active id-sessions?
I've tryed to change the session path by:
Code: Select all
session_save_path(realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session'));

but doesn't work. The output of
Code: Select all
$handle = opendir(session_save_path());

is the same: /var/lib/php5
:?~:-?~:???:
-----------------------------------------------------
| http://msalsas.com | http://itransformer.es |
-----------------------------------------------------
msalsas
Member
 
Posts: 53
Joined: Fri Jun 21, 2013 2:28 pm

Re: php trigger

Postby tiagojsag » Sun Jun 30, 2013 1:30 pm

Crons use php cli config, not apache/your web server config. You need to take that into account.
Tiago Garcia
@tiagojsag

Core Web Developer @ Shopware
http://www.shopware.de
User avatar
tiagojsag
Faithful Member
 
Posts: 885
Joined: Wed Aug 10, 2011 4:58 pm

Re: php trigger

Postby msalsas » Tue Jul 02, 2013 2:28 pm

Ok. I've done it in the home page controller. When any user goes to the home page, this will see the active sessions and will remove the old images:

Code: Select all
   
//src/XXX/HomeBundle/Controller/HomeController.php   
                        function rmOldImg() {
         //path images
         $ruta_imagenes = '../web/public/img/usuarios/';
      

         $handle_img = opendir($ruta_imagenes);
         if ($handle_img == false) return -1;
         
         
         while (($ifile = readdir($handle_img)) != false) {         
         if($ifile!='.' && $ifile!='..')   {
         $preg = 0;         
         $handle = opendir(session_save_path());
         if ($handle == false) return -1;   
            while (($file = readdir($handle)) != false) {
         
               if (preg_match("/^sess/", $file)) {
               if(time()- fileatime(session_save_path() . '/' . $file) < 1200) { // 1200 secs = 20 minutes session
                  $idSess = substr($file,5);                  
                  if (preg_match("/^".$idSess.".+/", $ifile)) $preg = 1;
                  
               }
               }
            }
            closedir($handle);   
            
            if($preg == 0){               
               unlink($ruta_imagenes.$ifile);
            }
         }   
         }
         
         closedir($handle_img);
      }
         
      rmOldImg();


And this works!! :D~:-D~:grin:

But I want to execute it like a crontab job. I've create a new crontab job by:

Code: Select all
crontab -e


and writed the next code:

Code: Select all
*/1 * * * *   /home/manolo/MiServer/itransformer/cronscript/borrar_img.php


The script code is:

Code: Select all
<?php

$ruta_proy = '/home/manolo/MiServer/itransformer';
//path images
$ruta_imagenes = $ruta_proy. '/web/public/img/usuarios';


$handle_img = opendir($ruta_imagenes);
if ($handle_img == false) return -1;


while (($ifile = readdir($handle_img)) != false) {         
if($ifile!='.' && $ifile!='..')   {
$preg = 0;         
$handle = opendir($ruta_proy . '/app/cache/dev/sessions');
if ($handle == false) return -1;   
   while (($file = readdir($handle)) != false) {

      if (preg_match("/^sess/", $file)) {
      if(time()- fileatime(($ruta_proy . '/app/cache/dev/sessions/') . $file) < 12) { // 12 secs = 0'2 minutes session
         $idSess = substr($file,5);                  
         if (preg_match("/^".$idSess.".+/", $ifile)) $preg = 1;
         
      }
      }
   }
   closedir($handle);   
   
   if($preg == 0){               
      unlink($ruta_imagenes.'/'.$ifile);
   }
}   
}

closedir($handle_img);
}
         


But doesn't work :frown:

How can I see if crontab is doing anything? Is ther any thing else to do?

Thank you
-----------------------------------------------------
| http://msalsas.com | http://itransformer.es |
-----------------------------------------------------
msalsas
Member
 
Posts: 53
Joined: Fri Jun 21, 2013 2:28 pm

Re: php trigger

Postby tiagojsag » Tue Jul 02, 2013 3:02 pm

Hi,

Don't know how to debug cron, but google should.

cheers
Tiago Garcia
@tiagojsag

Core Web Developer @ Shopware
http://www.shopware.de
User avatar
tiagojsag
Faithful Member
 
Posts: 885
Joined: Wed Aug 10, 2011 4:58 pm

Re: php trigger

Postby msalsas » Wed Jul 03, 2013 9:28 am

:D~:-D~:grin: Yeah! It works. :nod:

Just changing the cron tab job to:

Code: Select all
*/1 * * * *   php /home/manolo/MiServer/itransformer/cronscript/borrar_img.php


I thought that it was not necessary to write "php" before "script.php".
Anyway, it works correctly.

Thanks for all :proud: :thumbup:
-----------------------------------------------------
| http://msalsas.com | http://itransformer.es |
-----------------------------------------------------
msalsas
Member
 
Posts: 53
Joined: Fri Jun 21, 2013 2:28 pm

Re: php trigger

Postby tiagojsag » Wed Jul 03, 2013 9:34 am

Hi,

You should write the full path to the php bin (type 'which php' in shell to find it out) to avoid problems

cheers
Tiago Garcia
@tiagojsag

Core Web Developer @ Shopware
http://www.shopware.de
User avatar
tiagojsag
Faithful Member
 
Posts: 885
Joined: Wed Aug 10, 2011 4:58 pm

Re: php trigger

Postby msalsas » Wed Jul 03, 2013 12:30 pm

Right!

Code: Select all
MAILTO="myemail@gmail.com"
*/20 * * * *   /usr/bin/php /home/manolo/MiServer/itransformer/cronscript/borrar_img.php


Only one more question. Why does not send me a email when the command fails? It says:

MAIL (mailed 1 byte of output; but got status 0x00ff, #012) :uhoh:
-----------------------------------------------------
| http://msalsas.com | http://itransformer.es |
-----------------------------------------------------
msalsas
Member
 
Posts: 53
Joined: Fri Jun 21, 2013 2:28 pm


Return to General Symfony 2 discussion

Who is online

Users browsing this forum: Bing [Bot] and 4 guests