Bisakah saya membuat skrip php untuk bertindak sebagai FTP

Saya memiliki skrip php yang saya gunakan untuk mengunggah file zip ke ftp saya dan secara otomatis mengekstraknya.

Saya ingin tahu apakah sudah ada skrip php untuk menghapus folder dan file di ftp saya.

Alasan saya bertanya adalah karena saya menghemat banyak waktu dalam melakukan proses unggah dan unzip zip daripada membuka zip secara lokal dan kemudian mengunggah file.

Jadi sekarang masalah saya adalah membutuhkan waktu yang cukup lama untuk menghapus folder dan file menggunakan Filezilla dan saya ingin mempercepatnya.

Adakah yang punya solusi yang berfungsi?

Sunting: Ini kode unzip yang saya gunakan:

    <?php
/* Simple script to upload a zip file to the webserver and have it unzipped
  Saves tons of time, think only of uploading Wordpress to the server
  Thanks to c.bavota (www.bavotasan.com)
  I have modified the script a little to make it more convenient
  Modified by: Johan van de Merwe (12.02.2013)
*/

function rmdir_recursive($dir) {
foreach(scandir($dir) as $file) {
if ('.' === $file || '..' === $file) continue;
if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
else unlink("$dir/$file");
}

rmdir($dir);
}

if($_FILES["zip_file"]["name"]) {
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];

$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}

$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
$message = "The file you are trying to upload is not a .zip file. Please try again.";
}

/* PHP current path */
$path = dirname(__FILE__).'/'; // absolute path to the directory where zipper.php is in
$filenoext = basename ($filename, '.zip'); // absolute path to the directory where zipper.php is in (lowercase)
$filenoext = basename ($filenoext, '.ZIP'); // absolute path to the directory where zipper.php is in (when uppercase)

$targetdir = $path . $filenoext; // target directory
$targetzip = $path . $filename; // target zip file

/* create directory if not exists', otherwise overwrite */
/* target directory is same as filename without extension */

if (is_dir($targetdir)) rmdir_recursive ( $targetdir);


mkdir($targetdir, 0777);


/* here it is really happening */

if(move_uploaded_file($source, $targetzip)) {
$zip = new ZipArchive();
$x = $zip->open($targetzip); // open the zip file to extract
if ($x === true) {
$zip->extractTo($targetdir); // place in the directory with same name
$zip->close();

unlink($targetzip);
}
$message = "Your .zip file was uploaded and unpacked.";
} else {    
$message = "There was a problem with the upload. Please try again.";
}
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Unzip a zip file to the webserver</title>
</head>

<body>
<?php if($message) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
<label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>

person Behedwin    schedule 20.02.2014    source sumber
comment
Jadi Anda terhubung ke server FTP dan mengunggah file zip melalui skrip PHP. Jika itu berhasil untuk Anda, mengapa tidak menghapus file di server FTP melalui PHP juga? php.net/manual/en/function.ftp-delete.php (jangan langsung lakukan setelah membuka ritsleting, lakukan di cron untuk file zip apa pun yang diunggah, katakanlah lebih dari 1 jam?)   -  person Latheesan    schedule 20.02.2014
comment
Saya baru saja menemukan skrip untuk unzip. Mencari skrip php yang sudah jadi untuk dihapus. Saya tidak tahu apa-apa tentang pemrograman...   -  person Behedwin    schedule 20.02.2014
comment
Bisakah Anda memposting kode Anda, sehingga kami dapat melihat apa yang Anda lakukan...?   -  person Latheesan    schedule 20.02.2014
comment
Saya telah mengedit posting pertama saya dengan kode. Itu adalah kode yang saya gunakan untuk meng-unzip file .zip. Itu berjalan sangat cepat dibandingkan dengan unzip secara lokal dan kemudian mengunggah file/folder. Sekarang saya menginginkan skrip serupa di mana saya dapat menghapus file/folder di server FTP saya. Jika memungkinkan, atau mungkin ada solusi yang lebih baik?   -  person Behedwin    schedule 20.02.2014


Jawaban (1)


Saya tidak bermaksud untuk memasukkan pekerjaan saya sendiri di sini tetapi ada skrip yang saya jual di situs web bernama Code Canyon bernama MightyFTP yang membungkus fungsionalitas prosedural php ftp yang kompleks ke dalam OO API yang lebih sederhana.

Sejauh yang saya ketahui, menghapus folder memerlukan penghapusan rekursif melalui API FTP saat ini sehingga biasanya ada sedikit pekerjaan yang terlibat.

Tautan ke skrip ada di sini "http://codecanyon.net/item/mightyftp/8276375 "

Jika Anda memiliki kode saya dan ingin menghapus folder ftp, kode untuk melakukannya sangatlah mudah seperti yang ditunjukkan di bawah ini.

require_once("MightyFTP/FTPClient.php");

$myFtpClient = new MightyFTP\FTPClient("yourftpserver", new MightyFTP\FTPCredentials("yourftpusername", "yourftppassword"));

$ftpDirectoryToDelete = $myFtpClient->getFile("/PathToDirectory/");

$parentDir = $ftpDirectoryToDelete->delete(); //The directory and all its children have been deleted. It will then return the parent of the deleted file/folder.

$ftpDirectoryToDelete->rename(""); //Will throw an exception because you will not be able to run an operation on a file/directory that no longer exists.
person Brendan    schedule 18.11.2014