2017-05-08 123 views
-2

開發一個管理頁面,其中管理員上傳PDF和用戶下載該PDF。用戶下載PDF後,PDF要問密碼,如何爲PDF自動在PHP設置密碼和PDF不應由Admin密碼保護在PHP中的pdf

設置這是我的管理頁面代碼

move_uploaded_file($tmp_name, $location); 
if (copy($location, $location1)) { 

    $query = mysqli_query($conn, "SELECT * FROM `at_uploads` WHERE name='$name1'"); 
    if (mysqli_num_rows($query) > 0) { 

     echo("<SCRIPT LANGUAGE='JavaScript'> 
        window.alert('File already exists with that name') 
        window.location.href='upload'; 
         </SCRIPT>"); 
    } 
} 

這是我的用戶下載頁面

if(isset($_GET['dow'])) { 

$filepath1 = $_GET['dow']; 

$query = mysqli_query($conn, "SELECT * FROM `at_uploads` WHERE filepath1 ='$filepath1'"); 
while (($row = mysqli_fetch_array($query))) { 

    $name=$row['name']; 
    $location = "C:/wamp/www/school/admin/files/" . $name; 
    header('Content-Type: application/pdf'); 

    header('Content-Disposition: attachment; filename="' . basename($location) . '"'); 
    header('Content-Length: ' . filesize($location)); 
    readfile($location); 
     } 
    } 

我不知道在哪裏設置pdf的密碼

+0

您需要更具體地瞭解您的需求。如果你可以提供你的源代碼以及你犯了什麼錯誤,那麼這會更快。即您的錯誤日誌 – Akintunde007

+0

使用哪個庫 –

+0

但是,如果管理員無法設置密碼,用戶應該如何知道密碼?或者讓用戶回憶單獨的pdf和密碼,例如文件直接下載和郵件密碼? –

回答

0

您正在使用哪個庫?如果您正在使用FPdi,請使用下面的功能。

<?php 

function pdfEncrypt ($origFile, $password, $destFile){ 
//include the FPDI protection http://www.setasign.de/products/pdf-php-solutions/fpdi-protection-128/ 
require_once('fpdi/FPDI_Protection.php'); 

$pdf =& new FPDI_Protection(); 
// set the format of the destinaton file, in our case 6×9 inch 
$pdf->FPDF('P', 'in', array('6','9')); 

//calculate the number of pages from the original document 
$pagecount = $pdf->setSourceFile($origFile); 

// copy all pages from the old unprotected pdf in the new one 
for ($loop = 1; $loop <= $pagecount; $loop++) { 
    $tplidx = $pdf->importPage($loop); 
    $pdf->addPage(); 
    $pdf->useTemplate($tplidx); 
} 

// protect the new pdf file, and allow no printing, copy etc and leave only reading allowed 
$pdf->SetProtection(array(),$password); 
$pdf->Output($destFile, 'F'); 

return $destFile; 
} 

//password for the pdf file 
$password = '[email protected]'; 

//name of the original file (unprotected) 
$origFile = 'yourfilename.pdf'; 

//name of the destination file (password protected and printing rights removed) 
$destFile ='yourfilename.pdf'; 

//encrypt the book and create the protected file 
pdfEncrypt($origFile, $password, $destFile); 
?> 
+0

我試過這不工作 – karthik