doujiu3768 2014-01-19 16:22
浏览 10

too long

I am making a zend project that has a file upload functionality and the client wanted to encrypt and upload the files without their extension. I did that perfectly by using the following code :

$upload = new Zend_File_Transfer_Adapter_Http ();
$salt = $upload->getHash('md5', 'file_name');
$upload->addFilter('Rename', array('target' => APPLICATION_PATH . '/../../uploads/presentations/'.$salt));
$upload->addFilter('Encrypt', array('adapter' => 'mcrypt', 'key' => $constants->encryption_key));
$upload->receive ();

This is working fine now I'm just downloading the file with the following code

$filename = 'example_presentation_file.ppt'; //original name in db. by the way this coming from database
$hash = '85be69db87a43265a44be1482cc6d819'; //changed file name when uploaded with this
$mime = 'application/octet-stream';
$file_path = APPLICATION_PATH . '/../../uploads/presentations/'.$hash;
header('Content-Type: "'.$mime.'"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
$size = filesize($file_path);

if(isset($_SERVER['HTTP_RANGE']))
{
    list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
    list($range) = explode(",",$range,2);
    list($range, $range_end) = explode("-", $range);
    $range = intval($range);
    if(!$range_end) {
        $range_end = $size-1;
    }
    else {
        $range_end = intval($range_end);
    }
    $new_length = $range_end-$range+1;
    header("HTTP/1.1 206 Partial Content");
    header("Content-Length: $new_length");
    header("Content-Range: bytes $range-$range_end/$size");
}
else {
    $new_length = $size;
    header("Content-Length: ".$size);
}
@ob_end_clean();
$speed = false;
$chunksize = ($speed?($speed * 1024):(1024 * 1024)) ;
$bytes_send = 0;
$buffer = "";
if ($file = fopen($file_path, 'r')) {
    if(isset($_SERVER['HTTP_RANGE'])) {
        fseek($file, $range);
    }
    while(!feof($file) && (!connection_aborted()) && ($bytes_send<$new_length)) {
        $buffer = fread($file, $chunksize);
        print($buffer);
        flush();
        if($speed) {    
            if($type!=2) sleep(1);
        }
        $bytes_send += strlen($buffer);
    }
    fclose($file);
}

This code is working very fine and downloading the file but when I open that file in powerpoint, it doesn't open it and displays an error.

The point is that, If i disable the file encryption, I opens very good but it is necessary. I just wanted to know that how to decrypt the file in run time (just when user downloads it not forever).

Thanks.

  • 写回答

1条回答 默认 最新

  • duanqu9279 2014-01-20 01:27
    关注

    I suggest the following workflow:

    1. Temporary decrypt the encrypted file and save it somewhere
    2. Send this decrypted file to the client
    3. Remove decrypted file from the file system

    Example:

    <?php
    
    /* ... */
    
    $options = array(
    'adapter' => 'mcrypt', 
    'key'     =>  $constants->encryption_key);
    
    
    $decrypt = new Zend_Filter_File_Decrypt($options);
    
    // temp. decrypt file and save it on the disc
    $decrypted_file_path = APPLICATION_PATH . '/../../uploads/presentations/'. $filename;
    $decrypt->setFilename($decrypted_file_path);
    
    // encrypted file location
    $decrypt->filter($file_path);
    
    /* your code: Send $decrypted_file_path to the client */
    
    // remove decrypted file
    unlink($decrypted_file_path);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度