Jump to content

cbox


[PHP] Remote FTP File Upload


Recommended Posts

Posted

Missing the HTML Form.

 

<?php
//by charbsk

// FTP Configuration
$FTP_User = "$username";
$FTP_Pass = "$password";
$FTP_Host = "###";
$FTP_Root = "$dir";

// If the form was submitted
if (isset($action) && $action == "submit")
{
  
if ($dir == "-1")
{
	echo "Error: Please choose a File Type to upload";
	exit;
}

// Connect to the ftp address
$Connect = ftp_connect($FTP_Host);

if (!$Connect)
{
	echo "Error: Could not connect to ftp server
";
	exit;
}

	echo "Connected to $FTP_Host
";

// Login
$login = ftp_login($Connect, $FTP_User, $FTP_Pass);

if (!$login)
{
	echo "Error: Could not log on as $FTP_User
";
	ftp_quit($Connect);
	exit;
}

echo "Logged in as $FTP_User
";
  
//Turns passive mode on
$passive = ftp_pasv ($Connect, true );
  
/*
if (!passive){
	echo "Failed to enter passive mode.
";
}
else {
	echo "Entered passive mode.
";
}*/

if (ftp_chdir($Connect, "$FTP_Root"))
{
echo "Current directory is now: " . ftp_pwd($Connect) . "
";
}
else echo "Cannot change directory";

// Set the filename to be uploaded
$Filename = $_FILES['File_1']['name'];
$myFile = $_FILES['File_1'];
$destination_file = $FTP_ROOT.$_FILES['File_1']['name'];

// Set the local resource (the file that will be uploaded)
$file = $myFile['tmp_name'];

$contents = ftp_nlist($Connect, ".");
  
if (is_array($contents))
{
	if (in_array("$Filename", $contents))
	{
	echo "The file <font color='red'>$Filename</font> already exists on server
";
	ftp_close($Connect);
	exit;
	}
 }
	$upload = ftp_put($Connect, $destination_file, $file, FTP_BINARY);

	if (!$upload)
	{
		// Show success message
		echo "There was a problem uploading $Filename<BR>";
	}
	else
	{
		// Else show error message
		echo "Successfully uploaded $Filename<BR>";
		   
	}
	   
	$contents = ftp_nlist($Connect, ".");
	   
	if (in_array("$Filename", $contents))
	{
			echo "The File $Filename exists
";
	}
	else
	{
		echo "The File $Filename does not exist
";
	}
  
ftp_close($Connect);
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...