randomly sorting files in php - Page 3

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Re: getting perl and php to talk to each other

On 03/07/2013 03:11 PM, M. Strobel wrote:
Quoted text here. Click to load it

Why, yeees. Or even reading the actual words of
the actual original question, actually. Actually *read*,
I mean.

 >>>>> No command 'php-config' found, did you mean:
 >>>>> Command 'php-config5' from package 'php5-dev' (main)

Alastair    <-- never owned a red van

Re: getting perl and php to talk to each other

On 3/7/2013 3:43 PM, The Natural Philosopher wrote:
Quoted text here. Click to load it

No, but trying to get two different languages working together takes  
more expertise than just installing LAMP.  Of course, it's nothing a  
troll like you would understand.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

where to put the binaries was Re: getting perl and php to talk to each other

On 03/07/2013 12:43 PM, The Natural Philosopher wrote:

Quoted text here. Click to load it

NP, I've been reading you for a while now.  Did you give me the name of  
a newsgroup where they consider webstuff ontopic?

I'm rather convinced that scripting languages are my meal ticket this  
month, so much so that I'm making it happen with my rudimentary  
webtools.  This is what I call output:


This is a powerful medium, where one can intermix binaries and text.  My  
problem now is that it's not a discrete medium without me attending  
endlessly to obfuscations with files that I would much just rather have  
as themselves, and the only way you can see them is if php lets you with  
an authentication level equal to my level of give a crap.

It's been a while since I did a source listing, so I would like to do  
so, now that I can ask better questions.

In my line of work, I find myself taking a lot of pictures, because it  
shows what I do for my clients.  My clients are honest to God Amis whose  
privacy is a matter for me to stake my integrity on, and I have enough  
of them that I know that their preferences to privacy vary, depending  
mostly on age, btw.  Little bit on class.  whatevs.

The rub is that I need to be able to show what I do to market the things  
that I do.  These are the scripts I'm using to pull it off now:

I run a bash script to handle the images that get chosen from my camera:

$ cat nikon_bash3.sh

set -e
cd $
mkdir -p $DIR
cp *.JPG $DIR
cd $DIR
ls -l
mogrify -resize 800x600! *
rename 'y/A-Z/a-z/' *.JPG
ls -l
cd ..
cd ..
sudo fredify2.sh

I have several versions of this.  Very vanilla.

$ cd cleaning
$ cat food_ftp1.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
use HTML::Entities;
use File::Basename;
use Cwd;

# diamond bracket in paragraph mode declared at top scope
$/ = "";

## usage needs 1 arg from argv my_ftp

#identity and config
my $ident = 'my_ftp.txt';
my ( $config, $domain );
$config = do($ident);
unless ($config) {
     die("read error: $!")  if $!;
     die("parse error: $@") if $@;
$domain = $config->{ $ARGV[0] };
die("unknown domain: $ARGV[0]") unless $domain;

# get name of directory
my $dir = cwd;
print "dir is $dir \n";
my $word = basename($dir);
print "word is $word \n";

# get files
my $path  = "images1/";
my @files = <$path*>;
print " files are @files\n";

#dial up the server
my $ftp = Net::FTP->new( $domain->, Debug => 1, Passive => 1 )
   or die "Can't connect: $@\n";
$ftp->login( $domain->, $domain-> )
   or die "Couldn't login\n";

# get files from remote root that end in html:
my @remote_files = $ftp->ls();

# make unique .html choice
my @matching = map /$_(\d+)\.html/, @remote_files;
print "matching is @matching\n";
push( @matching, 0 );
@matching = sort { $a <=> $b } @matching;
my $winner  = pop @matching;
my $newnum1 = $winner + 1;
my $word2   = "$_$newnum1";
print " new word is  $word2\n";
my $html_file = "$.html";
print "html file is  $html_file\n";

#make directory in images to correspond

   or die "Cannot change working directory ", $ftp->message;
   or warn "Cannot create directory ", $ftp->message;

# create file for html stubouts
open( my $fh, '>', $html_file )
   or die("Can't open $html_file for writing: $!");

my $header = "template_stuff/header1.txt";

# write in the header
open( my $gh, '<', $header ) or die("Can't open: $!");

while (<$gh>) {
     print $fh $_;

my $h1 = 'Resolving conflicts';
my $h2 = 'and moving forward';
print $fh "<h1>$h1</h1>\n";
print $fh "<h2>$h2</h2>\n";

#create template outside loop

my $template = <<'TEMPLATE';
<img src="/images/%s"/>


open my $CAPTIONS, "<", "food1.txt" or die "file not there\n";

$ftp->cwd("/images/$/") or die "cwd failed $@\n";

# main control
for my $name (@files) {
     print "name is $name\n";
     my  $remote_file = basename($name);
     print "remote is $remote_file\n";
     $ftp->put( $name, $remote_file ) or die "put failed $!\n";

     # captions
     my $caption = <$CAPTIONS>;
     $caption = encode_entities($caption);
     printf $fh $template, "$/" . $remote_file, $caption;

# write in the footer
my $footer = "template_stuff/footer1.txt";
open( my $hh, '<', $footer )
   or die("Can't open: $!");

while (<$hh>) {
     print $fh $_;

close $fh;
$ftp->cdup()          or die "cdup failed $@\n";
$ftp->cdup()          or die "cdup failed $@\n";
$ftp->put($html_file) or die "put failed $@\n";


This has so far been my home-cooked perl version of static web dev, but  
I want to see if php can obviate all the go***a%n filing-about that you  
have to do to maintain the privacies of the the binaries shown.  These  
images are my shack so I don't care so much anymore.  I also use this  
tool chain to lay down the law with people a bit; it's a very direct medium:


Q1)  How do I design a website where the binaries aren't there for the  
taking unless php thinks you have done some act of authentication to see  

Q2)  What's a good first book for php?

Re: where to put the binaries was Re: getting perl and php to talk to each other

On 14/03/13 10:11, Cal Dershowitz wrote:

Quoted text here. Click to load it
Two standard ways.

1/. put them all in different directories and pop a .htaccess and  
password file in each one so that APACHE denies access without a login.

This is pure Apache and nothing to do with PHP at all. If apache is  
coinfigured to respect this method teh general form is


AuthType Basic
AuthName "My Private Picture access"
AuthUserFile /var/www.mysite.com/this_directory/.htpasswd
Require valid-user

That tells apache to deny unauthorised access, and look in

for name:password pairs.

This contains a user name and a hashed password and is created using teh  
htpasswd utility that comes with apache.


If you have access to the total apache installation, this is trivial,  
less so if you are merely uploading files to someone else's

2/. use cookies (sessions) in (php) via a login page: That essentially  
sets a php variable to reflect the users session. So you if the user  
attempts to access the page, and he is not logged in, the php should  
present him with a login page, and name and password (are filled in and  
submitted to the same page, these are then used to set the cookie, and  
all subsequent invocation's of that page with the cookie set will allow  
access to the directory via the PHP script.

You set and read cookies either using sessions, or doirectly


Cookies are an array of name=>value pairs in PHP global space.
"K9PA70634H" is just an arbitrary string.
AS is what's in $cookie...in my application every time a user logs in I  
look in a database to see if he has the right password, and if he has,  
generate a random cookie and store that in the database. If he comes  
back with the cookie still set, then that's a match and access is  
seamless. If the cookie has timed out or its different from that in the  
database, he gets to login again.

i.e. a not especially relevant code snippet..user may have set name and  
password in $_POST['name'] and $_POST['password']: $_POST['persistent']  
i sset if he wants to stay logged in forever..

    $result=mysql_query(sprintf("select id, password, email from people  
where familiar='%s'",$_POST['name']));
    if( $result && mysql_numrows($result)>0) // user at least exists
        if($_POST['password'] != mysql_result($result,0,'password'))
            $error="Incorrect password";
        else  // by golly we have a match!
             // generate a cookie and store it and echo it back to the users browser.
             mysql_query(sprintf("update people set cookie='%s', timeout='%s'  
where id = '%d'",$cookie, $_POST['persistent'],$id));
             // pretend we already HAVE a valid cookie from the browser..so that  
when getlogname is called it all 'works' and a new cookie will get issued
        $error="No such name is registered";

So that's how the name password gets stuck in the DB and a cookie is  

This function checks the cookie is valid, and CHANGES IT EVERY TIME THE  

Additional security so that even if - say some hacker knows what the  
cookie was last time, it wont work if the user has accessed the page again

function getlogname()
    global $logname; // system wide for the duration
    $id=0; // set default of not found
            $query=sprintf("select id , familiar, timeout from people where  
cookie ='%s'",$cookie);
            if ($result && mysql_num_rows($result)==1)
                $id=mysql_result($result,0, 'id');
                $logname=mysql_result($result,0, 'familiar');
                $persistent=mysql_result($result,0, 'timeout');
                $cookie=randomstring(10); // generate new cookie so stolen cookies  
wont work..this also means using a new machine will wipe out stored cookies
                    setcookie("K9PA70634H",$cookie,time() + (86400 * 365)); // give em  
a year before it expires.
                    setcookie("K9PA70634H",$cookie); // issue session only cookie
                // now update access time and ip address
                $query=sprintf("update people set last_ip='%s',cookie='%s',  
last_accessed='%s' where id='%d'",$ipaddr=$_SERVER['REMOTE_ADDR']  
,$cookie, date("Y-m-d H:i:s"), $id);
    return $id;

Getlogname in my case is called every page that has access to 'sensitive  
data' and checks that either the cookie is set, or invites the user to  
log in before proceeding.

If you went that sort of route you would probably hold a set of  
directories or pictures that user was allowed to access in the database.

Going the PHP route and using cookies and or databases is probably the  
slicker neater and vastly more complex way to do what the .htaccess  
files will do anyway.

Quoted text here. Click to load it
PHP is so much a stock procedural language with a bolt on OO part that  
its hardly worth having a 'first' book in PHP. Essentially if you have  
programmed in anything else, PHP is pretty much like C or Pascal in  
terms of its structure. The differences are subtle, in the (lack of)  
typing and in what libraries are available, and here the online manual  
is the best resource of all, especially user contributed solutions and  


(in-ep-toc?-ra-cy) ? a system of government where the least capable to  
lead are elected by the least capable of producing, and where the  
members of society least likely to sustain themselves or succeed, are  
rewarded with goods and services paid for by the confiscated wealth of a  
diminishing number of producers.

Re: where to put the binaries was Re: getting perl and php to talk to each other

On 3/14/2013 6:11 AM, Cal Dershowitz wrote:
Quoted text here. Click to load it

comp.lang.php is on topic for PHP, which drives a lot of websites.  But  
we already know you're an idiot.

Quoted text here. Click to load it

No, scripting languages seen to be beyond your level of comprehension.  
You're in the same league as TNP.

Quoted text here. Click to load it

Obfuscation is not security!

<snip unrelated crap from an idiot>

Quoted text here. Click to load it

Sure you can do it with PHP.  But you can do it with perl, also, without  
all the crap.  You just need to understand some basic concepts.

Quoted text here. Click to load it

Security measures will prevent users from accessing the files.  But  
you've already said you don't want help, so why do you continue posting?

Quoted text here. Click to load it

Dick and Jane Create a Web Site would be about your speed.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: randomly sorting files in php

Quoted text here. Click to load it

Do you know what order the mp3 player uses to decide the order of
play? If for instance it plays in alphabetical order of file name,
then the only way to get them played randomly is to rename them. On
the other hand it could be based on the order in which they are copied
to the device.

Re: randomly sorting files in php

On 03/04/2013 02:45 AM, Captain Paralytic wrote:
Quoted text here. Click to load it

there's a USER.PL there, empty.

playlist perl Idunno.

Site Timeline