bhavish030
bhavish030

Reputation: 53

Pass an array from BASH to PHP

I am listing all files on my desktop using a shell script. Below is my shell script:

 #! /bin/bash

myFiles=`dir /home/chb-pc/Desktop`
#echo $myFiles;

count=0
for f in $myFiles
do 
    #echo $f 
    read myArray[$count] = f
    `expr $count + 1`
done

echo myArray

The above script will list file names of all files. I need to store the output in a variable and send it to PHP where i will loop each and every files in that variable and do some stuff.

Below is my PHP script:

<?php

$output = shell_exec("sh /home/chb-pc/Desktop/files.sh");

foreach ($output as $files) {
    echo $files . "<br>";
}
?>

I know my shell script does not seem right, but i tried various syntax and can't find the right one.

Upvotes: 1

Views: 390

Answers (4)

Gordon Davisson
Gordon Davisson

Reputation: 125838

EDIT: as several others have pointed out, it's possible (and generally better) to get the list of filenames directly in php, without involving a shell script at all. But if you do need to use a shell script for some reason...

Since unix filenames can contain any character except for "/" and ASCII NUL (character #0) (and file paths can contain "/"), the standardish way to pass lists of filenames is with NUL delimiters. Getting a list of filenames from a command like ls that doesn't use a clean format like this is hard, so it's best to get the list by just using a raw wildcard (e.g. *) and letting bash itself get the list. The one slightly tricky thing in this part is that by default, if there are no matching files, the shell will just leave "*" there rather than producing an empty list, so you want bash's nullglob option enabled, and that's a bash-only feature (so run this with bash, not just sh!).

#! /bin/bash

cd /home/chb-pc/Desktop || exit

shopt -s nullglob    # If there are no files, don't just print the "*"!
printf '%s\0' *      # Print each filename followed by a NUL

There's another slightly tricky thing about capturing this in php: the list has a NUL after each filename, including the last one, but explode assumes the delimiters are between items. As a result, it'll wind up with an empty item at the end of the array, and you need to remove that with array_pop.

<?php
$output = explode("\x00", shell_exec('bash /home/chb-pc/Desktop/files.sh'));
array_pop($output);

foreach ($output as $files) {
        echo $files . "<br>";
}
?>

Upvotes: 1

Raja G
Raja G

Reputation: 6643

You can simply use glob

$fileList = glob('temp/*');

and now you can iterate and perform your set of actions.

Upvotes: 1

lyte
lyte

Reputation: 1192

Just avoid using both, PHP is quite capable of listing a directory:

<?php
$dir = '/home/chb-pc/Desktop';
foreach (new DirectoryIterator($dir) as $fileInfo) {
    if($fileInfo->isDot()) continue;
    echo $fileInfo->getFilename() . "\n";
}

See: http://php.net/manual/en/class.directoryiterator.php

Upvotes: 4

MyLibary
MyLibary

Reputation: 1771

I think that an ideal solution would be if your shell script output a comma-separated list, and your PHP code would look like so:

<?php

$output = explode(',', shell_exec('sh /home/chb-pc/Desktop/files.sh');

foreach ($output as $files) {
    echo $files . "<br>";
}
?>

May that work for you?

Upvotes: -1

Related Questions