Skip to content

Instantly share code, notes, and snippets.

@mitrallex
Last active April 18, 2018 22:07
Show Gist options
  • Save mitrallex/c312b7bc00f8788d2772c76ffc579ea7 to your computer and use it in GitHub Desktop.
Save mitrallex/c312b7bc00f8788d2772c76ffc579ea7 to your computer and use it in GitHub Desktop.
CLeanFTP
<?php
if (!function_exists("readline")) {
/**
* Reads a single line from the user
* @param string $prompt You may specify a string with which to prompt the user
* @return string Single string from the user
*/
function readline($prompt = null) {
if ($prompt){
echo $prompt;
}
$fp = fopen("php://stdin", "r");
$line = rtrim(fgets($fp, 1024));
return $line;
}
}
/**
* Get name of file
* @return string Filename
*/
function getFileName() {
$filename = readline("\nWhat is filename?\n");
if (empty($filename)) {
echo "Filename can't be empty!\n";
getFileName();
}
return $filename;
}
/**
* Select server
* @return array Server params
*/
function getServer() {
$server_to_choose = readline("\nSelect a server:\n1-Server1\n2-Server2\n3-Server3\n");
$server = array();
switch ($server_to_choose) {
case 1:
$server = array('name'=>'name',
'ip'=>'255.255.255.255',
'login'=>'login',
'password'=>'password',
'root_dir'=>'root_dir/'
);
break;
case 2:
$server = array('name'=>'name',
'ip'=>'255.255.255.255',
'login'=>'login',
'password'=>'password',
'root_dir'=>'root_dir/'
);
break;
case 3:
$server = array('name'=>'name',
'ip'=>'255.255.255.255',
'login'=>'login',
'password'=>'password',
'root_dir'=>'root_dir/'
);
break;
default:
echo "Server not selected!\n";
getServer();
break;
}
return $server;
}
// Define main variables
$im_folders = array('images', 'images10', 'images35', 'images_t', 'images_hq');
$params = array(
'' => 'help::',
'r::' => 'root::',
's::' => 'subdir::',
'v::' => 'version::',
'f::' => 'father::'
);
// Parse options passed to the script
$options = getopt(implode('', array_keys($params)), $params);
if (isset($options['help'])) {
$help = "
Welcome to CLeanFTP!
This script will help you remove a specific image from the server over the ftp connection.
Usage: php cleanftp.php [--help] [-r|--root] [-s|--subdir=directory_name]
Options:
--help Show this message
-r --root Use root directory for search and delete image
-s --subdir Set sub directory for search and delete image
-v --version Show program version
Example:
php cleanftp.php --subdir=usd
";
die($help);
}
if (isset($options['v']) || isset($options['version'])) {
$version = "
CLeanFTP
Version 1.0.0-alpha
";
die($version);
}
if (isset($options['f']) || isset($options['father'])) {
$father = "
CLeanFTP
Made by Mitrakhovich Alex
";
die($father);
}
// Select filename and server
$filename = getFileName();
$server = getServer();
// Connect to the server and log in
$connect_id = ftp_connect($server['ip']) or die("Could not connect to {$server['name']}");
ftp_login($connect_id, $server['login'], $server['password']);
// Search and delete file in the each dir, if it exists
foreach ($im_folders as $im_folder) {
if (isset($options['r']) || isset($options['root'])) {
$file = $server['root_dir'] . $im_folder . '/' .$filename;
} elseif (isset($options['s']) || isset($options['subdir'])) {
$sub_folder = isset($options['s']) ? $options['s'] : $options['subdir'];
$file = $server['root_dir'] . $im_folder . '/' . $sub_folder . '/' .$filename;
} else {
if (strpos($filename, '_') != false) {
$sub_folder = explode('_', $filename);
$sub_folder = $sub_folder[0];
$file = $server['root_dir'] . $im_folder . '/' . $sub_folder . '/' .$filename;
} else {
die('Invalid filename! Please specify the valid filename or specify a subdirectory (run this script with --help option for more information).');
}
}
$file_exist = ftp_size($connect_id, $file);
if ($file_exist != -1) {
if (ftp_delete($connect_id, $file)) {
echo "File successefully deleted from {$im_folder}!\n";
} else {
echo "Could not delete {$file}\n";
}
} else {
echo "File doesn't exist in {$im_folder}!\n";
}
}
// Close connection
ftp_close($connect_id);
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment