biwerw Posted July 27, 2009 Share Posted July 27, 2009 I have used this script in the past and it has worked on a local server. How would I set the below script to connect to and display files from a separate ftp? <?php // TASK: display a directory listing with thumbnails for images and human-readable filesizes // handy humansize function: // input is number of bytes, output is a "human-readable" filesize string function humansize($size) { // Setup some common file size measurements. $kb = 1024; // Kilobyte $mb = 1024 * $kb; // Megabyte $gb = 1024 * $mb; // Gigabyte $tb = 1024 * $gb; // Terabyte if($size < $kb) return $size."B"; else if($size < $mb) return round($size/$kb,0)."KB"; else if($size < $gb) return round($size/$mb,0)."MB"; else if($size < $tb) return round($size/$gb,0)."GB"; else return round($size/$tb,2)."TB"; } // get local directory path $path = "lgl-down"; ?> <!--<br /> <h3>Files in <?php print $path; ?>:</h3>--> <br /> <ul class="file-list"> <?php $d = dir($path); $icon = ''; while (false !== ($entry = $d->read())) { if ( substr($entry, 0, 1)=='.' ) continue; // get size $size = filesize($path.'/'.$entry); $humansize = humansize($size); // find filename extension $dotpos = strrpos($entry, '.'); if ($dotpos) { $ext = substr($entry, $dotpos+1); if ($ext === 'jpeg' || $ext === 'jpg' || $ext === 'gif' || $ext === 'png' || $ext === 'pdf' || $ext === 'psd' || $ext === 'ai') { $icon = "<img src='$path/$entry' style='width:48px; height:auto; vertical-align:text-top; margin:0 4px 8px 0;' alt='' title='$entry' />"; } } print "<li>$icon <a href='$path/$entry'>$entry</a> <span class='file-size'>($humansize)</span></li>\n"; $icon= ''; } $d->close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/167668-edit-script-to-display-contents-of-an-ftp-directory/ Share on other sites More sharing options...
flyhoney Posted July 27, 2009 Share Posted July 27, 2009 You will have to use PHP's FTP functions: http://us3.php.net/manual/en/ftp.examples-basic.php Quote Link to comment https://forums.phpfreaks.com/topic/167668-edit-script-to-display-contents-of-an-ftp-directory/#findComment-884330 Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 If the files are on the same server as where you're running the script you do not need to use the FTP functions of PHP. You just have to set the correct path which is where it goes wrong I suspect? Quote Link to comment https://forums.phpfreaks.com/topic/167668-edit-script-to-display-contents-of-an-ftp-directory/#findComment-884335 Share on other sites More sharing options...
biwerw Posted July 28, 2009 Author Share Posted July 28, 2009 If the files are on the same server as where you're running the script you do not need to use the FTP functions of PHP. You just have to set the correct path which is where it goes wrong I suspect? The files need to be on an ftp. Quote Link to comment https://forums.phpfreaks.com/topic/167668-edit-script-to-display-contents-of-an-ftp-directory/#findComment-884904 Share on other sites More sharing options...
biwerw Posted July 28, 2009 Author Share Posted July 28, 2009 Below is an updated script I have been working with but receive the following error after hanging for about a minute. Fatal error: Maximum execution time of 30 seconds exceeded in E:\www\mymarketinglab.com\securelogin\general\download.php on line 49 <?php // set up basic connection $conn_id = ftp_connect('12.123.123.123'); $ftp_user_name="username"; //change to ftp username $ftp_user_pass="password"; //change to ftp password // login with username and password $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); // get contents of the current directory $contents = ftp_nlist($conn_id, "/download-general"); // replace dot with your dir // TASK: display a directory listing with thumbnails for images and human-readable filesizes // handy humansize function: // input is number of bytes, output is a "human-readable" filesize string function humansize($size) { // Setup some common file size measurements. $kb = 1024; // Kilobyte $mb = 1024 * $kb; // Megabyte $gb = 1024 * $mb; // Gigabyte $tb = 1024 * $gb; // Terabyte if($size < $kb) return $size."B"; else if($size < $mb) return round($size/$kb,0)."KB"; else if($size < $gb) return round($size/$mb,0)."MB"; else if($size < $tb) return round($size/$gb,0)."GB"; else return round($size/$tb,2)."TB"; } // output $contents print_r($contents); ?> <br /> <ul class="file-list"> <?php $d = dir($path); $icon = ''; while (false !== ($entry = $d->read())) { if ( substr($entry, 0, 1)=='.' ) continue; // get size $size = filesize($path.'/'.$entry); $humansize = humansize($size); // find filename extension $dotpos = strrpos($entry, '.'); if ($dotpos) { $ext = substr($entry, $dotpos+1); if ($ext === 'jpeg' || $ext === 'jpg' || $ext === 'gif' || $ext === 'png' || $ext === 'pdf' || $ext === 'psd' || $ext === 'ai') { $icon = "<img src='$path/$entry' style='width:48px; height:auto; vertical-align:text-top; margin:0 4px 8px 0;' alt='' title='$entry' />"; } } print "<li>$icon <a href='$path/$entry'>$entry</a> <span class='file-size'>($humansize)</span></li>\n"; $icon= ''; } $d->close(); ?> </ul> Quote Link to comment https://forums.phpfreaks.com/topic/167668-edit-script-to-display-contents-of-an-ftp-directory/#findComment-884941 Share on other sites More sharing options...
biwerw Posted July 28, 2009 Author Share Posted July 28, 2009 BTW, this is line 49 $contents = ftp_nlist($conn_id, "/download-general"); // replace dot with your dir Quote Link to comment https://forums.phpfreaks.com/topic/167668-edit-script-to-display-contents-of-an-ftp-directory/#findComment-884944 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.