Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100 off


PHP Tutorials and Scripts   




Title: Determining file size in FTP    Marked Cool    (Review this resource)
Author: davidlo
Posted On: 2004-12-20
Category: Home > PHP Functions

Popularity: 1 points out of 10    

Description: Returns size of specified file in FTP

Total Hits: 917     Total Votes: 0     Total Points: 0 (0 reviews)        [ Download ]   

Page Navigation:  [1]


FTP File Size




Introduction:

ftp_filesize() works via two ways-the ftp_raw() function found in PHP 5 or by retrieving a list of files from the directory determined by the $path parameter. The PHP 5 method is very simple, consisting of a SIZE command sent to the FTP server. The less elegant method retrieves the entire raw listing of the directory, parses through it using the ftpProcessEntry function until the specified filename is found, and returns the size attribute from there.

Source Code:
<?php
function stripspaces($str)
{
	while(substr($str, 0, 1)==" ")
		$str=substr($str, 1);
	return $str;
}

function ftpProcessEntry($line)
{
	list($prop['perm'],$next)=split(" ",$line,2);
	list($prop['num'],$next)=split(" ",stripspaces($next),2);
	list($prop['owner'],$next)=split(" ",stripspaces($next),2);
	list($prop['group'],$next)=split(" ",stripspaces($next),2);
	list($prop['size'],$next)=split(" ",stripspaces($next),2);
	list($prop['month'],$next)=split(" ",stripspaces($next),2);
	list($prop['day'],$next)=split(" ",stripspaces($next),2);
	list($prop['time'],$prop['filename'])=split(" ",stripspaces($next),2);
	return $prop;
}

function ftp_filesize($conn_id, $path)
{
	//some files use PHP 5 syntax, must include correct files for PHP 4
	if(version_compare(PHP_VERSION, "5.0.0")>=0) 
	{
		$contents=ftp_raw($conn_id, "SIZE $path");
		return substr($contents[0], strpos($contents[0], " "));
	}
	else
	{
		$dir=substr($path, 0, strrpos($path, "/")+1);
		$filename=substr($path, strrpos($path, "/")+1);
		$contents=ftp_rawlist($conn_id, $dir);
		foreach($contents as $entry)
		{
			$prop=ftpProcessEntry($entry);
			if($prop['filename']=="." || $prop['filename']=="..")
				continue;
			if($prop['filename']==$filename)
				return $prop['size'];
		}
	}
}
?>


Page Navigation:  [1]



© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.