【问题标题】:Upload file with html, and zipping or rar the file with password in PHP?使用 html 上传文件,并在 PHP 中使用密码压缩或 rar 文件?
【发布时间】:2018-04-06 04:36:19
【问题描述】:

我不知道如何使用密码在 PHP 中压缩文件。密码将是时间和文件名。

这是我到目前为止所做的。

上传的 HTML 代码。

<form enctype="multipart/form-data" action="http://localhost/CSS/addfile.php" method="POST">
<div id="label">
<label>Upload File</label>
</div>
    <input name="doc" type="file" placeholder="Upload File Here" accept="files/topsecret/*" required>
<input type="submit" value="Upload" name="submit">
</form>

PHP 代码

function GetImageExtension($filetype)
{
    if(empty($filetype)) return false;
    switch($filetype)
    {
        case 'files/topsecret/bmp': return '.bmp';
        case 'files/topsecret/gif': return '.gif';
        case 'files/topsecret/jpeg': return '.jpg';
        case 'files/topsecret/png': return '.png';
        case 'files/topsecret/txt': return '.txt';
        case 'files/topsecret/doc': return '.doc';
        case 'files/topsecret/docx': return '.docx';
        case 'files/topsecret/pdf': return '.pdf';
        default: return false;
    }
}

$upFile = $_FILES['doc']['name'];
$tmp_name = $_FILES['doc']['tmp_name'];
$ftype = $_FILES['doc']['type'];
$fileExt = GetImageExtension($ftype);

$filename = $upFile.$fileExt;
$target_path="files/topsecret/".$filename;
move_uploaded_file($tmp_name,$target_path);

date_default_timezone_set('Asia/Kuala_Lumpur');
$timefile = date("F j, Y g:ia");
$size = filesize($target_path);
$size = number_format($size / 1024, 2) . ' KB'; 

try{
    $sql = "INSERT INTO file(File_path,Date,Size,Name) VALUES ('".$target_path."','".$timefile."','".$size."','".$filename."')";

    if ($connection->query($sql)){

        echo"<script type= 'text/javascript'>alert('Upload Successfully');</script>";
        header("refresh:2;index.php");
    }else{
        echo "<script type= 'text/javascript'>alert('Upload Not Successfully Inserted.');</script>";
    }

我研究了一下,发现了一些 php 函数,但不知道如何使用它。 喜欢。 ZipArchive::setEncryptionName ...但不能使用它,因为我在 xampp 中使用的是 PHP 7.1.8 版。

请帮助我解释如何做到这一点,尽可能简单。我需要使用 zip 或 rar 使用密码加密上传的文件。计划将文件名和时间一起使用哈希,然后将其设置为密码。

非常感谢。

【问题讨论】:

  • 您完全按照示例php.net/manual/en/ziparchive.setencryptionname.php中显示的方式使用它
  • 我试过了,因为我的版本是 7.1.8,所以我无法使用它,因为我使用的是 xampp。未捕获的错误:调用未定义的方法 ZipArchive::setEncryptionName()

标签: php html encryption zip rar


【解决方案1】:

首先,try 块需要一个 catch。

其次,你应该不需要GetImageExtension函数,$_FILES在上传的数组中有扩展名,你只需要print_r($_FILES);就能验证。

遗憾的是,根据我的阅读,您还不能加密文件,您需要等待php 7.2 被释放才能使用$zip-&gt;setEncryptionName;

我在写了一些代码后发现了这一点,我认为它可能会有所帮助,但这就是我发布此答案的原因。

您可以查看:http://php.net/manual/en/filters.encryption.php,这是集成到下面代码中的好选择,我现在没有时间,但是按照他们的示例进行操作相当容易。

if(isset($_POST['submit'])){
    upload($_FILES);
}

class Connection {
    protected $db = null;

    public function db(){
        if($this->db === null){
            try {
                $this->db = new PDO('mysql:host=localhost;dbname=name; charset=utf8', user, password);
            } catch (PDOException $e) {
                echo 'Connection failed: ' . $e->getMessage();
            }
        }
        return $this->db;
    }
}

function upload($file_data){

    // calling this statically, don't suggest it
    $conn = Connetion::db();

    $name = $file_data['doc']['name'];
    $tmp_name = $file_data['doc']['tmp_name'];
    $extension = explode('/', $file_data['doc']['type']); // $extension[1] returns file type.
    $image_size = getimagesize($tmp_name);

    $file_name = $name . '.' . $extension[1];
    $target_path = "";

    if($image_size !== false){

        $zip = new ZipArchive();
        if ($zip->open('uploaded_file.zip', ZipArchive::CREATE) === TRUE) {
            $zip->addFromString("text.txt", "#1 This is a test string added as testfilephp.txt.\n");
            $zip->setEncryptionName('text.txt', ZipArchive::EM_AES_256, 'secret'); // here we'd set the password 
            $zip->close();
            $zip_created = true;
        } else {
            $zip_created = false;
        }   

        // if zip was created and uploaded the file, then we upload it to the database
        if($zip_created == true){
            $sth = $conn->prepare('INSERT INTO file (file_path, `date`, size, name) VALUES (:target_path, :time_file, :size, :file_name)');
            $sth->bindValue(':target_path', $target_path, PDO::PARAM_STR);
            $sth->bindValue(':time_file', date('m-d-Y H:i:s'), PDO::PARAM_STR);
            $sth->bindValue(':target_path', $target_path, PDO::PARAM_STR);
            $sth->bindValue(':file_name', $file_name, PDO::PARAM_STR);
            $sth->execute();
        } else  {
            // here we can upload the error to the database or do nothing
        }
    }
}

?>

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="doc">
    <input type="submit" value="Upload" name="submit">
</form>

【讨论】:

    猜你喜欢
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 2015-10-10
    • 2010-12-28
    • 2015-03-25
    • 2013-09-27
    • 2013-05-12
    相关资源
    最近更新 更多