Frejoh466
Posts: 32
Joined: Sat Jun 29, 2013 5:09 pm

Run .py script from PHP and include a folder.

Sun Jul 28, 2013 4:33 pm

So I've been trying to get two things working today, and I've spent roughly 9hours today to get this to work, but no luck.

First.
I'm trying to run .py script. With PHP it seems that the exec() would be best, but I can't get it to work. I have tried,

Code: Select all

<?php exec('sudo python /var/www/takepic.py'); ?>
Also tried different combinations, like exec('takepic.py') and so on, but nothing is happening. So I started to look in the PHP manual and found some that might would work, but didn't get them to work ether (tried with jQuery, but just got it to load the plain text in the .py script).

Second.
I'm trying to load the latest picture that was taken in the /picture/ folder, they are named by the date that they where taken. And then have a next/prev/numbers button to brows them (I later found that the best way to do this would be by using jQuery, but need to read up on how to use it). But I was able to write this,

Code: Select all

<?php
    $images = glob('*.{jpg,jpeg}', GLOB_BRACE);
    $num_of_files = 1;
    foreach($images as $image)
    {
         $num_of_files--;
         if($num_of_files > -1)
           echo "<b>".$image."</b><br>Created on ".date('D, d M y H:i:s', filemtime($image)) ."<br><img src="."'".$image."'"."><br><br>" ;
         else
           break;
    }
?>
It works, but I want it to load the files in /picture/ and not in the root folder. I tried some combination with, scandir, chdir and include. But didn't get that to work.

I also wrote this,

Code: Select all

<body class="left-sidebar">
	<div id="wrapper">
		<div id="content">
			<div id="content-inner">						
				<div class="pager">
					<div class="pages">
						<a href="#" class="active">1</a>
						<a href="#">2</a>
						<a href="#">3</a>
						<a href="#">4</a>
						<span>&hellip;</span>
						<a href="#">20</a>
					</div>
				<a href="#" class="button next">Next Page</a>
		             </div>					
		        <article class="is-post is-post-excerpt">
		</article>
	</div>
</div>
But I guess I need to re-write this in jQuery to get it to work.

txt3rob
Posts: 368
Joined: Sat Aug 11, 2012 3:45 pm
Location: Liverpool

Re: Run .py script from PHP and include a folder.

Sun Jul 28, 2013 7:39 pm

have you checked permissions what is apache running as
https://www.github.com/random-robbie - Infosec tools

Corez
Posts: 39
Joined: Fri Feb 03, 2012 12:59 am

Re: Run .py script from PHP and include a folder.

Mon Jul 29, 2013 3:46 pm

Frejoh466 wrote:So I've been trying to get two things working today, and I've spent roughly 9hours today to get this to work, but no luck.

First.
I'm trying to run .py script. With PHP it seems that the exec() would be best, but I can't get it to work. I have tried,

Code: Select all

<?php exec('sudo python /var/www/takepic.py'); ?>
Also tried different combinations, like exec('takepic.py') and so on, but nothing is happening. So I started to look in the PHP manual and found some that might would work, but didn't get them to work ether (tried with jQuery, but just got it to load the plain text in the .py script).
You will need to give Apache sudo access to the python script. I believe adding the following code to the bottom of the /etc/sudoers file, although I am not familiar with the sudoers syntax so this may be wrong:

Code: Select all

www-data ALL=/var/www/takepic.py NOPASSWD
www-data is the user the Apache uses to run in
Frejoh466 wrote:Second.
I'm trying to load the latest picture that was taken in the /picture/ folder, they are named by the date that they where taken. And then have a next/prev/numbers button to brows them (I later found that the best way to do this would be by using jQuery, but need to read up on how to use it). But I was able to write this,

Code: Select all

<?php
    $images = glob('*.{jpg,jpeg}', GLOB_BRACE);
    $num_of_files = 1;
    foreach($images as $image)
    {
         $num_of_files--;
         if($num_of_files > -1)
           echo "<b>".$image."</b><br>Created on ".date('D, d M y H:i:s', filemtime($image)) ."<br><img src="."'".$image."'"."><br><br>" ;
         else
           break;
    }
?>
It works, but I want it to load the files in /picture/ and not in the root folder. I tried some combination with, scandir, chdir and include. But didn't get that to work.
You need to put the path into the glob function so it would look something like this:

Code: Select all

<?php
    $images = glob('/picture/*.{jpg,jpeg}', GLOB_BRACE);
    $num_of_files = 1;
    foreach($images as $image)
    {
         $num_of_files--;
         if($num_of_files > -1)
           echo "<b>".$image."</b><br>Created on ".date('D, d M y H:i:s', filemtime($image)) ."<br><img src="."'".$image."'"."><br><br>" ;
         else
           break;
    }
?>
Frejoh466 wrote:I also wrote this,

Code: Select all

<body class="left-sidebar">
	<div id="wrapper">
		<div id="content">
			<div id="content-inner">						
				<div class="pager">
					<div class="pages">
						<a href="#" class="active">1</a>
						<a href="#">2</a>
						<a href="#">3</a>
						<a href="#">4</a>
						<span>&hellip;</span>
						<a href="#">20</a>
					</div>
				<a href="#" class="button next">Next Page</a>
		             </div>					
		        <article class="is-post is-post-excerpt">
		</article>
	</div>
</div>
But I guess I need to re-write this in jQuery to get it to work.
If you choose to use jQuery you would be looking at a plugin called a Slider (there are lots of them), in which case jQuery will (probably) make the navigation for you. If you would like to learn how to make your own jQuery slider NetTuts has a great course on jQuery which includes making a slider. It is well worth a look

Frejoh466
Posts: 32
Joined: Sat Jun 29, 2013 5:09 pm

Re: Run .py script from PHP and include a folder.

Thu Aug 08, 2013 11:45 am

edit the sudoer file with,

Code: Select all

sudo pkexec visudo
Then adding,

Code: Select all

www-data ALL=(/var/www/takepic.py) NOPASSWD: ALL
and now I can run the .py file in /var/www/.


But,

Code: Select all

<?php
    $images = glob('/picture/*.{jpg,jpeg}', GLOB_BRACE);
    $num_of_files = 1;
    foreach($images as $image)
    {
         $num_of_files--;
         if($num_of_files > -1)
           echo "<b>".$image."</b><br>Created on ".date('D, d M y H:i:s', filemtime($image)) ."<br><img src="."'".$image."'"."><br><br>" ;
         else
           break;
    }
?>
Did not work,
if I change it to

Code: Select all

 $images = glob('/var/www/picture/*.{jpg,jpeg}', GLOB_BRACE);
it tells me the latest file,
/var/www/picture/20130808133658.jpg
Created on Thu, 08 Aug 13 13:36:58
But does not load the image.

Thanks for the help and for the link to the jQuery tutorial.

Corez
Posts: 39
Joined: Fri Feb 03, 2012 12:59 am

Re: Run .py script from PHP and include a folder.

Thu Aug 15, 2013 7:59 pm

Try using a relative path in the glob function so it would be 'pictures/*.{jpg,jpeg}'

NalloK
Posts: 1
Joined: Sat Aug 17, 2013 6:56 am

Re: Run .py script from PHP and include a folder.

Sat Aug 17, 2013 7:07 am

I believe you need to include the file extension in:

Code: Select all

if($num_of_files > -1)
           echo "<b>".$image."</b><br>Created on ".date('D, d M y H:i:s', filemtime($image)) ."<br><img src="."'".$image."'"."><br><br>" ;
         else
           break;
Where it should be:

Code: Select all

<img src="."'".$image.".jpg'".">
I have tried to get php to do this before and believe that this is the solution. I'm not sure if there is a better way or how exactly to get php to get the extension. And this will only work with .jpg not .JPG so that is another problem. If you find a cleaner way to do this please post, as well as if this at least loads an image.

Hope it helps!

Frejoh466
Posts: 32
Joined: Sat Jun 29, 2013 5:09 pm

Re: Run .py script from PHP and include a folder.

Sun Sep 15, 2013 4:57 pm

So I fixed the code (after reinstall the OS, 6 times due to corruption, and changing to a new OS). I got it to work with

Code: Select all

<?php
	date_default_timezone_set('Europe/London');
    $images = glob('picture/*.{jpg,jpeg}', GLOB_BRACE);
    $num_of_files = 1;
    foreach($images as $image)
    {
         $num_of_files--;
         if($num_of_files > -1)
           echo "<b>".$image."</b><br>Created on ".date('D, d M H:i', filemtime($image)) ."<br><img src="."'".$image."'"."><br><br>" ;
         else
           break;
    }
?>


You might see that I added "date_default_timezone_set('Europe/London');" why? because it decided to use UTC instead of CEST, and the OS is using CEST time, and the .py script that take the picture also use the OS time and it didn't get effected, so it's still using CEST.

But, now PHP decided to use descending instead of ascending, so the code doesn't work because it's loading the oldest image instead of the newest. Why? because PHP does whatever PHP wants to.

So, does anyone know how to change PHP from using descending to ascending?

Corez
Posts: 39
Joined: Fri Feb 03, 2012 12:59 am

Re: Run .py script from PHP and include a folder.

Sat Oct 12, 2013 8:53 pm

As an alternative to ednl's answer you could look at using the scandir function it does support sorting

Return to “Other programming languages”