Jump to content

Recommended Posts

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();
				?>

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.

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.