View Full Version : modify to list a directory in ftp instead of glob

DJ Hands3
03-05-2011, 09:56 AM
1) Project Details: (be as specific as possible):

Hi, At the moment I have a simple php script that will read a directory and only show the files I want based on a search I pass through from a query.

At the moment it uses glob, but I have seen in php it can be done in ftp as I need to read the directory from another site I run, and file get contents removes some css formatting which I need.

I have attempted myself to modify but am getting stuck filtering the results based on a search query. Below is 2 php scripts the existing using glob and a another version using ftp up to a limited point.

All I need is the results from the search in the ftp modified version to pass the filterd results to a vairable so I can finish it off, the original version contains most the coding it's just getting it to work over ftp not through glob.

Original directory list:

//change the query .mp3 to .part should be .rar but the 2 dot's cause problems
$strip = str_replace(array('.mp3'), '.part', $_GET['q']); rename($_GET['q'], $strip);

//First expected part1.rar file to be found in directory
$strip_primary = str_replace(array('.mp3'), '.part1.zip', $_GET['q']); rename($_GET['q'], $strip);

//stream relay files for djhandsfree.co.uk

$streamaudiolink = $_GET['q'];

$random_intro = array("kie1.mp3",
$sizeof = count($random_intro);
$random = (rand()%$sizeof);

$random_advert = array("dnr_vinyl_advert.mp3",
$sizeof = count($random_advert);
$random = (rand()%$sizeof);

$dir = 'files/';
//exclude the directory being searched for these files
$exclude = array('.','..','.htaccess', '.mp3');
$q = (isset($strip))? strtolower($strip) : '';

$res = opendir($dir);

$filename = $dir.$strip_primary;

if (file_exists($filename))
$msg = 'Download file parts: save all the extracted files into the same folder and unzip!<br>';
} else {
$msg = 'The file is not yet available for download and is still being processed <a href="listen.php?file='.urlencode($streamaudiolink).'&intro='.$random_intro[$random].'&advert='.$random_advert[$random].'&mix='.urlencode($file_name).'" title="Stream this file now! '.$file_name.'">click here to alternatively stream the file</a><br>';
while(false!== ($file = readdir($res)))
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude))
$thelist .= '<a href="http://www.djhandsfree.co.uk/barrier.php?file='.$file.'" title="Click to download '.$file.'">'.$file.' <img src="img/rar.jpg" alt="" title="Click to download '.$file.'" width="16" height="16" border="0" /></a><br>';

<P><? // display the rar parts for download if found
echo $msg.$thelist

New ftp version trying to be modified

<?php ini_set('display_errors',1);

# the directory where ftp_rawlist starts
$startdir = "httpdocs/files";

# optional Datatypefilter (leave blank if not needed)
$suffix = "zip";
# ftp-login
$ftp_server = "ftp.somesite.com";
$ftp_user = "username";
$ftp_pw = "userpass";
$ftp_mode = "";
$conn_id = ftp_connect($ftp_server);
ftp_login($conn_id, $ftp_user, $ftp_pw) OR die("<br>ftp-login failed");
ftp_pasv($conn_id, true);

# create filelist (recursiv)
$files = array(); # must be defined here
$files = raw_list("$startdir");

# print result
$i = 0; $count = count($files);
while ($i < $count):
echo '<a href="'.$files[$i].'">'.$files[$i].'</a><br>';

# rawlist in recursive form (without parameter true!!!)
function raw_list($folder)
Global $conn_id;
Global $suffix;
Global $files;
$suffixes = explode(",", $suffix);
$list = ftp_rawlist($conn_id, $folder);
$anzlist = count($list);
$i = 0;
while ($i < $anzlist):
$split = preg_split("/[\s]+/", $list[$i], 9, PREG_SPLIT_NO_EMPTY);
$ItemName = $split[8];
$endung = strtolower(substr(strrchr($ItemName,"."),1));
$path = "$folder/$ItemName";
if (substr($list[$i],0,1) === "d" AND substr($ItemName,0,1) != "."):
# array_push($files, $path); # write directory in array if desired
elseif (substr($ItemName,0,2) != "._" AND in_array($endung,$suffixes)):
array_push($files, $path);
return $files;

2) Payment Amount: Please quote me - good job + bonus :D

3) Payment method/ details (Paypal, check? Timeline?): Paypal

4) Additional Info (about project or potential bidders): N/A

DJ Hands3
03-09-2011, 01:07 AM