how upload files via website using FTP method

File Transfer Protocol (FTP) is a common method used for transferring files between a local computer and a remote server. In the context of web development, FTP can be particularly useful when you need to upload files, such as images, documents, or other assets, from your local machine to a remote server. In this tutorial, we'll explore how to upload files to an FTP server using PHP, a popular scripting language for web development.

-> Connect to the FTP Server: To start, you need to establish a connection to the FTP server using the ftp_connect() function. This function returns an FTP connection resource that you'll use for further operations.

-> Login to the FTP Server: Once connected, you need to log in to the FTP server using the ftp_login() function. This function requires the FTP connection resource, username, and password.

-> Set Passive Mode: If you encounter connectivity issues, you might need to set passive mode using the ftp_pasv() function.

-> Upload a File: Now you're ready to upload a file from your local machine to the FTP server using the ftp_put() function. This function requires the FTP connection resource, the remote filename, and the local filename.

-> Close the FTP Connection: After uploading the file, it's a good practice to close the FTP connection using the ftp_close() function

1.) create uploadfile.php ( you can use different filename)

<?php

// get FTP credentials

$host = $_POST['host'];
$user = $_POST['user'];
$pass = $_POST['pass'];
$destDir = $_POST['dir'];
$workDir = "./temp"; // this is the temp folder to save temp file, will be deleted once upload complete

// get temporary file name for the uploaded file
$tmpName = basename($_FILES['file']['tmp_name']);

// copy uploaded file into current directory
move_uploaded_file($_FILES['file']['tmp_name'], $workDir."/".$tmpName) or die("Cannot move uploaded file to working directory");

// open connection
$conn = ftp_connect($host) or die ("Cannot initiate connection to host");

// send access parameters
ftp_login($conn, $user, $pass) or die("Cannot login");
//enable passive mode
setPasvmode (true,$conn);

// perform file upload
$upload = ftp_put($conn, $destDir."/".$_FILES['file']['name'], $workDir."/".$tmpName, FTP_BINARY);

// check upload status
if (!$upload) {

echo "Upload Failed! ". $_FILES["file"]["error"];

} else {

echo "Upload Complete! File name is ".$_FILES['file']['name'].", Destination path is ".$destDir;

}

// close the FTP stream
ftp_close($conn);

// delete temp file in $workDir folder
unlink($workDir."/".$tmpName) or die("Cannot delete uploaded file from working directory — manual deletion recommended");


function setPasvmode ($pasvmode,$conn)
{

return ftp_pasv($conn, $pasvmode);
}
?>

.

2.) Create HTML form to upload files(uploadform.html)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>upload file through FTP</title>
</head>
<body>
<h2>upload file via FTP</h2>

<form enctype="multipart/form-data" method="post" action="uploadFile.php">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
Host: <input type="text" name="host" /><p />

Username: <input type="text" name="user" /><p />

Password: <input type="password" name="pass" /><p />

Destination directory: <input type="text" name="dir" />*based on root folder of FTP account*<p />

File: <input type="file" name="file" /><p />

Action: <input type="submit" name="submit" value="Upload File" />
</form>

</body>
</html>
  • FTP, PHP, script FTP, FTP function
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

SQL server- network related or instance-specific error happened while connection establishing

  Instance-specific or network related error happening while connecting to SQL server   Please...

OleDB connection string examples

OleDB connection string examples MS Access (Jet) "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA...

Do you allow custom COM components?

Do you allow custom COM components? We are not offering to install the custom COM components...

Error when accessing a WCF services

Error when accessing a WCF services Error when accessing a WCF service: "IIS specified...

How to cache static contents to client with UseMaxAge?

You can consider to caches static contents to the client with UseMaxAge if your website has too...

Powered by WHMCompleteSolution