我可以创建一个PHP脚本来充当FTP

时间:2014-02-20 09:57:38

标签: php ftp delete-file

我有一个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>

1 个答案:

答案 0 :(得分:0)

我并不是要在这里插入我自己的工作,但我在一个叫做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.