Return to Snippet

Revision: 16764
at August 15, 2009 03:47 by renatoelias


Initial Code
< ?php


function formataLista($type,$name,$dir){
	$type = substr($type,0,1);
	if ($type == "d") {
		return "<a href='?dir=$dir$name'>$name";
	} else {
		return "<a href='?dir=".substr($dir,0,strlen($dir)-1)."&amp;get=$dir$name&amp;name=$name' target='_blank'>$name</a>";
	}
}

// set up basic connection

$ftp_server = "ftp.tata.com.br";
$ftp_user_name = "ftp";
$ftp_user_pass = "ftp";

$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
if ((!$conn_id) || (!$login_result)) {
        echo "FTP connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name";
        exit;
}

// specif the folder to usage
//$user = "/piralatopes";

$dirCurrent = $_GET['dir']."/";

if ($dirCurrent == "" or $dirCurrent == "//") { 
	$dirCurrent = "/";
}


$dir = $user.str_replace("../","",$dirCurrent);

$tmpSearch = substr($dirCurrent,0,strlen($dirCurrent)-1);
$dirPast = substr($dirCurrent,0,strrpos($tmpSearch, "/"));
$tmpSearch = "";

if (ftp_chdir($conn_id, $dir)) {

	if($_GET['get'] != "" and $_GET['name'] != "") {
			$destination_file .= ".".$user;
    		$destination_file .= basename($_GET['get']);
		   if (ftp_get($conn_id, $destination_file, basename($_GET['get']), FTP_BINARY)) { 
				header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
				header ("Content-Length: " . filesize($destination_file));
				header ("Content-Disposition: attachment; filename=".$_GET['name']);
				readfile($destination_file);
				unlink($destination_file);
		   }
		exit();
	}


?>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
	<title>FTP</title>
		<style type="text/css" media="screen">
			body {
				font-family: "Trebuchet MS";
			}
		</style>
	</head>
	<body>

	<h2>Arquivos na Pasta: < ?= $dirCurrent?></h2>
< ?

	if($_FILES['upload_file']['name'] != '') {


		$source_file = $_FILES['upload_file']['tmp_name'];
		$destination_file = $_FILES['upload_file']['name'];	
		// upload the file
		$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
		
		// check upload status
		if (!$upload) {
		        echo "FTP upload has failed!<br />";
		    } else {
		        echo "Uploaded $source_file to $ftp_server as $destination_file<br />";
		    }
	
	}

	if ($_POST['new_dir'] != "") {
		$newDir = $dir.$_POST['new_dir'];
		if (@ftp_mkdir($conn_id, $newDir)) {
			 echo "successfully created $newDir<br />";
		} else {
		 	echo "There was a problem while creating $newDir<br />";
		}
	}
	

	$listHard = ftp_rawlist($conn_id, $dir);
	$listEasy = ftp_nlist($conn_id, $dir);

	echo "<ul>";
	if ($dirCurrent != "/" and $dirCurrent != "") {
		echo "<li><a href='?dir=$dirPast'>..</a></li>";
	}
	$i = 0;
	foreach($listHard as $list) {
		echo "<li>".formataLista($list,$listEasy[$i],$dirCurrent)."</li>";
		$i++;
	}
	echo "</ul>";

?>

	<h3>Enviar Arquivos</h3>	

	<form action="?dir=<?= $dirCurrent?>" enctype="multipart/form-data" method="post">
		<input type="file" name="upload_file"  id="upload_file"/>
		<p><input type="submit" value="Enviar &rarr;"/></p>
	</form>

	<h3>Criar Pasta</h3>	

	<form action="?dir=<?= $dirCurrent?>" method="post">
		<input type="text" name="new_dir"  id="new_dir"/>
		<input type="submit" value="Enviar &rarr;"/>
	</form>

</body>
< ?
	
} else {
	header("location:?dir=/");
}

// close the FTP stream
ftp_close($conn_id);
?>

Initial URL
http://renatoelias.art.br/blog/2007/04/12/sistema-simples-de-ftp-em-php-simple-system-of-ftp-in-php/

Initial Description


Initial Title
PHP FTP SIMPLE SYSTEM

Initial Tags
php, simple

Initial Language
PHP