#native_company# #native_desc#
#native_cta#

Virtual FTP for a WebSite. ( Not actually FTP )

By Mathew Eis
on August 29, 2001

Version: 1.0

Type: Full Script

Category: File Management

License: GNU General Public License

Description: I originally made this program when I was
running through a bunch of free webhosters.
I got tired of relearning their interface so I made this one. I now have a ‘real’ website, and no longer need ‘virtual’ FTP. I couldn’t bear to just dump it, so here it is. Now I’ll probably have to make a bunch of versions 😉

There is also an online web editor that I
made to go with it… I’ll add it in a few weeks when I get some spare time.

<?php

// Virtual FTP Program for PHP4 +
// By Mathew Eis <[email protected]>

// I originally made this program when I was
// running through a bunch of free webhosters
// I got tired of relearning their interface
// so I made this one. I now have a 'real'
// website, and no longer need 'virtual' FTP.
// I couldn't bear to just dump it, so here it
// is. Now I'll probably have to make a bunch
// of new versions ;)

// There is also an online web editor that I
// made to go with it... I'll add it in a few weeks

// Function to get the listing of a directory
// Syntax: get_dir_list("/usr/local/bin");
// Gets:
//    Path to directory
//    Trailing slash shouldn't matter
// Returns:
//    Array of files & folders in the directory

function get_dir_list($directory)
{
	$files=array();
	$numfiles = 0;

	$handle=opendir($directory);

	while($file = readdir($handle))
	{
		if($file != ".")
		{
			if(($directory != ".")||($file != ".."))
			{
			$files[$numfiles] = $file;
			$numfiles++;
			}
		}
	}

	reset($files);

	return $files;
}

// This function was used to decide which icon to show for a file.
// I used Apache's default icons... 

function get_icon($file,$filename)
{
	@$ext = substr($file,strrpos($file,"."),strlen($file));
	$icon_path = "/icons/"; // Web path to icon images
	$image = "generic.gif"; // This is the icon if we don't know what to use

	if($filename == "..")
	{
		$image = "back.gif";
	}
	elseif(is_dir($file))
	{
		$image = "dir.gif";
	}
	elseif(($ext == ".jpg")||($ext == ".jpeg")||($ext == ".gif"))
	{
		$image = "image.gif";
	}
	elseif(($ext == ".txt")||($ext == ".text"))
	{
		$image = "text.gif";
	}
	elseif(($ext == ".html")||($ext == ".htm")||($ext == ".shtml")||($ext == ".php")||($ext == ".php3"))
	{
		$image = "html.gif";
	}

	return $icon_path.$image;
}

/*
Here was a place to check authorization to the FTP

if(!$hasaccess)
{
	print "You do not have access";
}
else
{
*/

	// Check the cwd...
	$cwd = (isset($cwd) ? $cwd : ".");	
	$cwd = ($cwd == "" ? "." : $cwd);

	$action = (isset($action) ? $action : "");

	// We want to get a file
	if($action == "get")
	{
		$size = filesize($file);
		$handle = fopen($file,"r");
		$contents = fread($handle,$size);
		fclose($handle);

		// Mime types needed work... I didn't have the time for it.
		header("Content-Type: unknown");
		print $contents;
	}
	else
	{

	// Print your header file here
	// include("header.php");

	// Make a directory
	if($action == "mkdir")
	{
		if($cwd == ".")
		{
		$thedir = $file;
		}
		else
		{
		$thedir = $cwd."/".$file;
		}
		mkdir($thedir,0777);
	}

	// Remove a file
	if($action == "rm")
	{
		unlink($file);
	}

	// Remove a directory
	if($action == "rmdir")
	{
		rmdir($file);
	}

	// Upload a file
	// On slow servers, the file is not copied fast enough
	// to be stat' in the next list command. It shows up in
	// The listing, but you can't get the size. There must
	// be a workaround, but I didn't put that much time into
	// it. It worked, that was enough.
	if($action == "put")
	{
		if(is_uploaded_file($file))
		{
			if(is_file($cwd."/".$file_name))
			{
			unlink($cwd."/".$file_name);
			}
			copy($file,$cwd."/".$file_name);
			chmod($cwd."/".$file_name,0777);
		}
	}
	?>
	<center>
	<table cellpadding=5 cellspacing=0 border=1 width=450>
		<tr><th align="left"><font size="+1">Virtual FTP Program</font></td></tr>
		<tr>
			<td>
				<table border=0 cellpadding=0 cellspacing=0>
				<tr>
				<td align="left">
				<form method="post" action="virtualftp.php">
				<input type="hidden" name="action" value="cd">
				Current Directory:
				<br>
				<select name="cwd">
				<?php
				$dirpath = split("/",$cwd);
				$numpaths = sizeof($dirpath);
				$dirpaths = array();
				$textpaths = array();
				for($cpath = 0; $cpath < $numpaths; $cpath++)
				{
					if($cpath == 0)
					{
					$dirpaths[$numpaths-$cpath-1] = $dirpath[$cpath];
					}
					else
					{
					$dirpaths[$numpaths-$cpath-1] = $dirpaths[$numpaths-$cpath-2]."/".$dirpath[$cpath];
					}
					if($dirpath[$cpath] != ".")
					{
					$textpaths[$numpaths-$cpath-1] = $dirpath[$cpath];
					}
					else
					{
					$textpaths[$numpaths-$cpath-1] = "/";
					}
				}
				for($cpath = 0; $cpath < $numpaths; $cpath++)
				{
					print "<option value="".$dirpaths[$cpath]."">".$textpaths[$cpath]."</option>n";
				}
				if($cwd != ".")
				{
					print "<option value=".">/</option>n";
				}
				?>
				</select>
				<input type="submit" value="Change Directory">
				</form>
				</td>
				<td align="left">
				<form method="post" action="virtualftp.php">
				<input type="hidden" name="action" value="mkdir">
				<?php
				print "<input type="hidden" name="cwd" value="".$cwd."">n";
				?>
				Create Directory:
				<br>
				<input type="text" name="file" size=15>
				<input type="submit" value="Create">
				</form>
				</td>
				</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table cellpadding=2 cellspacing=0 border="1" width="100%">
				  <tr>
						<th align="left">Filename</th>
						<th align="left">Size</th>
						<th align="left">Modified</th>
						<th align="left">Delete</th>
					</tr>
					<?php
						$files = get_dir_list($cwd);
						$numfiles = sizeof($files);

						sort($files);
			
						for($fileat = 0; $fileat < $numfiles; $fileat++)
						{
							$thefile = $files[$fileat];
							$filename = $thefile;
							if($cwd == ".")
							{
								$filepath = $thefile;
							}
							elseif($thefile == "..")
							{
								$filepath = substr($cwd,0,strrpos($cwd,"/"));
							}
							else
							{
								$filepath = $cwd."/".$thefile;
							}

							$image = get_icon($filepath,$filename);
	
							if(($filename == "..")||(is_dir($filepath)))
							{
								if($filename != "..")
								{
								$date = date("m/d/Y",filectime($filepath));
								}
								else
								{
								$date ="&nbsp;";
								}
								print "<tr>n";
								print "<td align="left">";
								print "<a href="virtualftp.php?cwd=".urlencode($filepath)."&action=cd">";
								print "<img src="".$image."" width=20 height=22 border=0>".$filename;
								print "</a></td>n";
								print "<td align="left">&nbsp;</td>n";
								print "<td align="left">".$date."</td>n";
								if($filename != "..")
								{
								print "<td align="left"><a href="virtualftp.php?cwd=".urlencode($cwd)."&action=rmdir&file=".urlencode($filepath).""><img src="icons/delete.gif" width=20 height=20 border=0></a></td>n";
								}
								else
								{
								print "<td>&nbsp;</td>";
								}
								print "</tr>n";
							}
							else
							{
								$size = filesize($filepath);
								$date = date("m/d/Y",filectime($filepath));
								print "<tr>n";
								print "<td align="left">";
								print "<a href="virtualftp.php?cwd=".urlencode($cwd)."&action=get&file=".urlencode($filepath)."">";
								print "<img src="".$image."" width=20 height=22 border=0>".$thefile;
								print "</a></td>n";
								print "<td align="left">".$size."</td>n";
								print "<td align="left">".$date."</td>n";
								print "<td align="left"><a href="virtualftp.php?cwd=".urlencode($cwd)."&action=rm&file=".urlencode($filepath).""><img src="icons/delete.gif" width=20 height=20 border=0></a></td>n";
								print "</tr>n";
							}
						}
					?>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				Upload a file into this directory:
				<br>
				<form method="post" enctype="multipart/form-data" action="virtualftp.php">
				<input type="hidden" name="action" value="put">
				<?php
				print "<input type="hidden" name="cwd" value="".$cwd."">n";
				?>
				<input type="file" name="file" size=20> <input type="submit" value="Put File">
				</form>
			</td>
		</tr>
	</table>
	</center>
	<?php
	// }  Other end of access block
}
?>