Skip to content

Instantly share code, notes, and snippets.

@erhaem
Last active December 11, 2023 07:08
Show Gist options
  • Select an option

  • Save erhaem/6838df6978776f5c53b629b2e39ad5e0 to your computer and use it in GitHub Desktop.

Select an option

Save erhaem/6838df6978776f5c53b629b2e39ad5e0 to your computer and use it in GitHub Desktop.
FTP File Upload

FTP File Upload

This really helped me when I encountered the 'cannot be uploaded, Error message' issue with CyberPanel. I had no root access, so nothing I can do with the configuration. Tried FileZilla, net2ftp but still no luck, I didn't even see the port (21) is blocked. If you have the same issue, give this a try.

Upload (or create the file manually) ftp.php to public_html. Then head to yoursite.com/ftp.php

Hope it helps! :)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FTP File Upload</title>
</head>
<body>
<h2>File Upload via FTP</h2>
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Select File:</label>
<input type="file" name="file" id="file" required>
<br>
<input type="submit" value="Upload">
</form>
</body>
</html>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$ftpServer = '127.0.0.1';
$ftpUsername = 'YOUR FTP USERNAME';
$ftpPassword = 'YOUR FTP PASSWORD';
$ftpConnection = ftp_connect($ftpServer);
if (!$ftpConnection) {
die('Could not connect to FTP server');
}
$loginResult = ftp_login($ftpConnection, $ftpUsername, $ftpPassword);
if (!$loginResult) {
die('FTP login failed');
}
// ftp_chdir($ftpConnection, 'public_html');
// $contents = ftp_nlist($ftpConnection, ".");
// // output $contents
// var_dump($contents);
$uploadedFile = $_FILES['file']['tmp_name'];
if (!file_exists($uploadedFile)) {
die('Local file does not exist: ' . $uploadedFile);
}
$remoteFileName = '/public_html/' . $_FILES['file']['name'];
$uploadResult = ftp_put($ftpConnection, $remoteFileName, $uploadedFile, FTP_BINARY);
if ($uploadResult) {
echo '<font color="green">File uploaded successfully.</font>';
} else {
echo '<font color="red">File upload failed. </font><pre>'. print_r(error_get_last(), true).'</pre>';
}
ftp_close($ftpConnection);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment