2017-10-25 3 views
1

암호로 PHP에서 파일을 압축하는 방법을 알 수 없습니다. 암호는 시간과 파일 이름이됩니다.html을 사용하여 파일을 업로드하고 PHP에서 비밀번호가있는 파일을 압축하거나 rar 하시겠습니까?

이것은 내가 지금까지 해 온 것입니다.

업로드 용 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를 사용하여 업로드 된 파일을 암호로 암호화해야합니다. 파일 이름과 시간을 함께 해시 한 다음 암호로 설정하십시오.

고마워요.

+0

당신은 내 버전은 7.1.8 원인으로 나는 그것을 사용하지 못할,이 예제 http://php.net/manual/en/ziparchive.setencryptionname.php – Craig

+0

나는 그것을 시도에 표시된 정확히 사용 방법 xampp을 사용하고 있습니다. 잡히지 않은 오류 : 정의되지 않은 메서드 호출 ZipArchive :: setEncryptionName() –

답변

0

먼저 try 블록에는 catch가 필요합니다.

둘째, GetImageExtension 함수가 필요하지 않습니다. $ _FILES에 업로드 된 배열의 확장자가 있습니다. 확인이 가능하려면 print_r($_FILES); 만 있으면됩니다.

슬프게도 내가 읽은 파일에서 아직 파일을 암호화 할 수 없으므로 php 7.2이 출시되어 $zip->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> 
관련 문제