Jump to content

convert python to php . . ?


2tonejoe

Recommended Posts

i can't get python to run with apache and need this to work badly. .

 

#!/usr/bin/env python

import os

a = "32496"
b = a[2:5]

dirlist = os.listdir("/repo/files/names/ids/" + b + "/")
found = 0

for i in dirlist:
    if "32496" in i:
        os.system("cp -Rf /repo/files/names/ids/496/32496* /new/folder/process-these/")
        found = found + 1
    else:
        pass

if found != 0:
    echo "I found and copied: " + str(found) + " records."
else:
    echo "I didn't find that one. Sorry"

Link to comment
https://forums.phpfreaks.com/topic/126914-convert-python-to-php/
Share on other sites

i am running linux.

 

the code takes the name of a file with no extension and reads the last 3 digits of the input and looks for files named like it in a directory structure. If it finds them it copies them to a different folder.

 

for instance . . .

  • a web form POSTs a user input of "1234567"
  • the scripts looks for any file starting off with the name "1234567" inside of "/repo/files/names/ids/567/"
  • for every file inside the folder that starts with "1234567" it copies to "/new/folder/process-these/" using a simple linux copy tool and an asterisk *

 

does that help at all?

This should do what the Pyhon script did:

<?php
$a = '32496';
$b = substr($a,-3,3);
$i = 0;
foreach (glob('/repo/files/names/ids/' . $b . '/' . $a . '*') as $file) {
      copy($file,'/new/folder/process-these/' . basename($file));
      $i++;
}
if ($i > 0)
    echo "I found and copied: " + $i + " records.<br>";
else
    echo "I didn't find that one. Sorry";
?>

 

Note: untested

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.