ฉันสามารถสร้างสคริปต์ php เพื่อทำหน้าที่เป็น FTP ได้หรือไม่

ฉันมีสคริปต์ php ที่ใช้ในการอัปโหลดไฟล์ zip ไปยัง ftp และแตกไฟล์โดยอัตโนมัติ

ฉันสงสัยว่ามีสคริปต์ php ที่ทำเสร็จแล้วเพื่อลบโฟลเดอร์และไฟล์บน ftp ของฉันหรือไม่

เหตุผลที่ฉันถามคือเพราะฉันประหยัดเวลาได้มากในการอัปโหลดและแตกไฟล์ zip แทนที่จะแตกไฟล์ในเครื่องแล้วอัปโหลดไฟล์

ตอนนี้ปัญหาของฉันคือต้องใช้เวลาค่อนข้างมากในการลบโฟลเดอร์และไฟล์โดยใช้ Filezilla และฉันต้องการเร่งความเร็วให้เร็วขึ้น

ใครมีวิธีแก้ปัญหาการทำงาน?

แก้ไข: นี่คือรหัสแตกไฟล์ของฉันที่ฉันใช้:

    <?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 แหล่งที่มา
comment
ดังนั้นคุณกำลังเชื่อมต่อกับเซิร์ฟเวอร์ FTP และอัปโหลดไฟล์ zip ผ่าน PHP Script หากวิธีนี้เหมาะกับคุณ ทำไมไม่ลบไฟล์บนเซิร์ฟเวอร์ FTP ผ่านทาง PHP ด้วยล่ะ php.net/manual/en/function.ftp-delete.php (อย่าทำทันทีหลังจากคลายซิป ให้ทำบน cron สำหรับไฟล์ zip ที่อัพโหลด เช่น เก่ากว่า 1 ชั่วโมงใช่หรือไม่)   -  person Latheesan    schedule 20.02.2014
comment
ฉันเพิ่งพบสคริปต์สำหรับการแตกไฟล์ กำลังมองหาสคริปต์ php ที่เสร็จแล้วสำหรับการลบ ฉันไม่รู้อะไรเกี่ยวกับการเขียนโปรแกรม ...   -  person Behedwin    schedule 20.02.2014
comment
คุณช่วยโพสต์รหัสของคุณได้ไหม เพื่อให้เราดูว่าคุณกำลังทำอะไรอยู่...?   -  person Latheesan    schedule 20.02.2014
comment
ฉันได้แก้ไขโพสต์แรกของฉันด้วยรหัส นั่นคือรหัสที่ฉันใช้แตกไฟล์ .zip มันดำเนินไปเร็วมากเมื่อเทียบกับการแตกไฟล์ในเครื่องแล้วอัพโหลดไฟล์/โฟลเดอร์ ตอนนี้ฉันต้องการสคริปต์ที่คล้ายกันซึ่งฉันสามารถลบไฟล์/โฟลเดอร์บนเซิร์ฟเวอร์ FTP ของฉันได้ หากเป็นไปได้หรืออาจมีวิธีแก้ปัญหาที่ดีกว่านี้?   -  person Behedwin    schedule 20.02.2014


คำตอบ (1)


ฉันไม่ได้ตั้งใจจะเชื่อมต่องานของตัวเองที่นี่ แต่มีสคริปต์ที่ฉันขายบนเว็บไซต์ชื่อ Code Canyon ชื่อ MightyFTP ที่รวมฟังก์ชัน php ftp ขั้นตอนที่ซับซ้อนให้กลายเป็น OO API ที่ง่ายกว่ามาก

เท่าที่ฉันทราบการลบโฟลเดอร์จำเป็นต้องมีการลบแบบเรียกซ้ำผ่าน FTP API ปัจจุบัน ดังนั้นจึงมีงานเล็กน้อยที่เกี่ยวข้องตามปกติ

ลิงก์ไปยังสคริปต์อยู่ที่นี่ "http://codecanyon.net/item/mightyftp/8276375 "

หากคุณมีโค้ดของฉันและต้องการลบโฟลเดอร์ ftp โค้ดที่ต้องทำก็จะตรงไปตรงมามากดังที่แสดงด้านล่าง

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