在ftp_put之后没有工作取消链接

时间:2017-10-17 18:40:21

标签: php unlink

我在这个类中有两个取消链接,第一个是在move_uploaded_file之后的moveSub()中并且它正常工作

但是第二个在ftp_put和ftp_close之后的moveFTP()方法中,这一个不起作用,我不知道为什么?

我尝试在另一种方法中使用unlink()但仍无效!

我认为这是因为ftp_put

我该如何解决?

namespace calsses;
class uploadfile{


 protected $destination;

protected $msg = [];
protected $goodTypes = ['srt', 'application/octet-stream'];
protected $maxSize = 700 * 1024;
protected $newName;
protected $zipName;
protected $repalce = [' ', '-'];
protected $renameDuplicates;

//FTP
protected $ftp_server = "server";
protected $ftp_username = "username";
protected $ftp_userpass = "password";
protected $ftp_dir = "/src/";


function __construct($uploadFolder)
{
    if (!is_writable($uploadFolder) || !is_dir($uploadFolder)) {
        throw new \Exception('no folder');
    }
    if ($uploadFolder[strlen($uploadFolder) - 1] != '/') {
        $uploadFolder .= '/';
    }
    $this->destination = $uploadFolder;

    //FTP
}


public function upload()
{
    $sub = current($_FILES);
    if ($this->check($sub)) {
        if ($this->moveSub($sub)) {
            if ($this->makeZip()) {
                $this->moveFTP();
            }
        }
    }
}

protected function check($sub)
{
    if ($sub['error'] != 0) {
        $this->getMessges($sub);
        return false;
    }
    if (!$this->checkSize($sub)) {
        return false;
    }
    if (!$this->checkType($sub)) {
        return false;
    } else {
        return true;
    }
}

protected function getMessges($file)
{
    switch ($file['error']) {
        case 1:
        case 2:
            $this->msg[] = $file['name'] . ' is too big max:';
            break;
        case 3:
            $this->msg[] = $file['name'] . ' partily uploaded';
            break;
        case 4:
            $this->msg[] = $file['name'] . ' no file selected';
            break;
        default:
            $this->msg[] = $file['name'] . ' there is a problem wit your file';
            break;
    }
}

public function returnMsg()
{
    return $this->msg;
}

protected function checkType($sub)
{
    if (!in_array($sub['type'], $this->goodTypes)) {
        $this->msg[] = 'bad type';
        return false;
    } else {
        return true;
    }
}

protected function checkSize($sub)
{
    $serverMaxSize = 1000 * 1024;
    if ($sub['size'] > $serverMaxSize) {
        return $this->msg[] = 'max file size';
        return false;
    }
    if ($sub['size'] == 0) {
        return $this->msg[] = '0 size';
        return false;
    }
    if ($sub['size'] > $this->maxSize) {
        return $this->msg[] = 'max file size error';
        return false;
    } else {
        return true;
    }
}

protected function newName($sub)
{
    $this->newName = null;
    $justname = pathinfo($sub['name'], PATHINFO_FILENAME);
    $noSpace = str_replace(' ', '.', $justname);
    $newName = $noSpace . '_' . time() . date('Ymd') . '.' . 'srt';
    $this->newName = $newName;

    $exists = scandir($this->destination);
    if (in_array($this->newName, $exists)) {
        $i = 1;
        $newName = explode('.', $newName);
        $newName = array_slice($newName, 0, -1);
        $newName = implode($newName, '.');
        do {
            $this->newName = $newName . '.' . $i++ . '.srt';
        } while (in_array($this->newName, $exists));
    }

    return $this->newName;
}


protected function moveSub($sub)
{
    $upload = move_uploaded_file($sub['tmp_name'], $this->destination . $this->newName($sub));
    if ($upload) {
        return $this->msg[] = 'uploaded ' . $this->newName;
        return true;
    } else {
        return $this->msg[] = 'problem with uploading file ' . $this->newName;
        return false;
    }

}

protected function zipName()
{
    $this->zipName = null;
    $this->zipName = time();
    $this->zipName .= '.zip';
    return $this->zipName;


}

protected function makeZip()
{
    $zip = new \ZipArchive();
    $zip->open($this->destination . $this->zipName(), \ZipArchive::CREATE);
    $zip->addFile($this->destination . $this->newName, $this->newName);
    $zip->setArchiveComment('Subzila');
    $zip->close();
    unlink($this->destination . $this->newName);//this unlik work

    if (file_exists($this->destination . $this->zipName)) {
        return true;
    } else {
        return $this->msg[] = 'problem with zipping';
        return false;
    }
}

function delete() {
    unlink($this->destination . $this->zipName);
}

protected function moveFTP()
{
    $ftpCon = ftp_connect($this->ftp_server);
    $ftpLogin = ftp_login($ftpCon, $this->ftp_username, $this->ftp_userpass);
    ftp_pasv($ftpCon, true);
    $ftpPut = ftp_put($ftpCon, $this->ftp_dir . $this->zipName, $this->destination . $this->zipName, FTP_ASCII);
    if ($ftpPut) {
        return true;
    } else {
        return $this->msg[] = 'problem with uploadding';
        return false;
    }
    ftp_close($ftpCon);
    unlink($this->destination . $this->zipName);//this unlink wont work

}

}

1 个答案:

答案 0 :(得分:0)

在解决代码中的问题后回答您的问题: 像这样编辑你的函数moveFTP()

protected function moveFTP()
{
    $ftpCon = ftp_connect($this->ftp_server);
    $ftpLogin = ftp_login($ftpCon, $this->ftp_username, $this->ftp_userpass);
    ftp_pasv($ftpCon, true);
    // first edit here, use FTP_BINARY as transfer-mode to keep binary files OK
    $ftpPut = ftp_put($ftpCon, $this->ftp_dir . $this->zipName, $this->destination . $this->zipName, FTP_BINARY);
    // second edit(s) here, change your if/else to execute ftp_close() and unlink() before returning the function.
    // e.g. like so (I don't know your preferences, so maybe adapt this code to your needs):
    if ($ftpPut) {
        ftp_close($ftpCon);
        unlink($this->destination . $this->zipName);
        return true;
    } else {
        ftp_close($ftpCon);
        $this->msg[] = 'problem with uploading';
        return false;
    }
}