PHP ftp file upload and download code

The following is the code for file upload and download by using PHP ftp function.

The interface will be like this:

php file upload using ftp

File transfer is expensive. So using ftp to transfer file and using web service to store related information may not be the most efficient way.

The following is the complete code. For editing reason, I separate php and html code, but you should put them in the same file, that should work properly.

if($_POST && $_POST['action'] == 'upload'){
	if($_POST['file_title'] && $_POST['define_type_id'] && $_POST['connection_type_id']){
	$ftp_server = $_POST['host'];
	$ftp_user = $_POST['user'];
	$ftp_pass = $_POST['pass'];
 
	// set up a connection or die
	$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
	//echo $conn_id;
	// try to login
	$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
 
	$file_id = md5($_FILES['file']['name'].time());
	$str = $_FILES['file']['name'];
	$pos = strrpos($str, '.');
	$len = strlen($str);
	$real_name = substr($str, 0, $pos);
	$file_ext = substr($str, $pos+1, $len);
 
	if (ftp_put($conn_id, $file_id.".".$file_ext, $_FILES['file']['tmp_name'], FTP_BINARY)) {
		//echo "successfully uploaded {$_FILES['file']['name']}\n";
 
		$client = new SoapClient ( "http://localhost:8080/FileManagement/FileManagementService?WSDL" );
 
		$file_title = $_POST['file_title'];
		$connection_id = $_POST['connection_type_id'];
		$file_type = $_POST['define_type_id'];
 
		$result = $client->Upload(array("file_id" => $file_id,
		"file_title"=> $file_title,
		"connection_id" => $connection_id,
		"define_type_id" => $file_type,
		"real_name" => $real_name,
		"file_ext" => $file_ext
		));
 
		echo "New file uploaded!";
 
	} else {
		echo "There was a problem while uploading\n";
	}
 
	ftp_close($conn_id);
	}else{
		echo "please input every field";
	}
}elseif ($_GET && $_GET['action'] == 'delete'){
	echo "deleting";
	$client->DeleteFile(array("file_id" => $_GET['file_id']));
 
}

Below is the html code:

<h2>Please provide the following information:</h2>
<form enctype="multipart/form-data" method="post">
<input name="MAX_FILE_SIZE" type="hidden" value="5000000" />Host 
<input name="host" type="text" />
 
Username 
<input name="user" type="text" />
 
Password 
<input name="pass" type="password" />
 
Destination directory 
<input name="dir" type="text" />
 
File 
<input name="file" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>
Category >> PHP  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. Rick Hellewell on 2015-2-10

    You probably should have some input sanitation in there somewhere, or at least mention the need for it.

  2. mohaafid on 2015-8-11

    i am student now and its difucult to run this code plz post database fields. i dont understood the field name in this code.

Leave a comment

*