致命错误:未捕获的异常'异常'有消息'无法打开输入文件'

时间:2015-10-27 14:27:26

标签: php mysql encryption cryptography

我有一个班级名aes.php(谷歌搜索时找到)。本课程使用 Rijndael-128 。 这里是aes.php

<?php

    /*
        Version: 1.0
    */
    class aes_encryption{

        const CIPHER = MCRYPT_RIJNDAEL_128; // Rijndael-128 is AES
        const MODE   = MCRYPT_MODE_ECB;

        public $key = 'abcdefghij123456'; // needs to be 32 bytes for aes
        public $iv = '1234efgh'; // needs to be 16 bytes for aes

        public function encrypt($plaintext){            
            $ciphertext = mcrypt_encrypt(self::CIPHER, $this->key, $plaintext, self::MODE, $this->iv);
            return base64_encode($ciphertext);
        }

        public function decrypt($ciphertext){
            $ciphertext = base64_decode($ciphertext);
            $plaintext = mcrypt_decrypt(self::CIPHER, $this->key, $ciphertext, self::MODE, $this->iv);
            return rtrim($plaintext, "\0");
        }



public function encrypt_file($input_file, $output_file){
            $input_file_handle = @fopen($input_file, "r");
            $output_file_handle = @fopen($output_file, 'wb');

            if(!$input_file_handle){ throw new Exception("Could not open input file"); }
            if(!$output_file_handle){ throw new Exception("Could not open output file"); }

            while(!feof($input_file_handle)){
                $buffer = base64_encode(fread($input_file_handle, 4096));                     
                $encrypted_string = $this->encrypt($buffer);
                //echo strlen($encrypted_string).'<br>';
                fwrite($output_file_handle, $encrypted_string);
            }

            fclose($input_file_handle);
            fclose($output_file_handle);

            return true;
        }

        public function decrypt_file($input_file, $output_file){
            $input_file_handle = @fopen($input_file, "r");
            $output_file_handle = @fopen($output_file, 'wb');

            if(!$input_file_handle){ throw new Exception("Could not open input file"); }
            if(!$output_file_handle){ throw new Exception("Could not open output file"); }

            while(!feof($input_file_handle)){
                //4096 bytes plaintext become 7296 bytes of encrypted base64 text
                $buffer = fread($input_file_handle, 7296);
                $decrypted_string = base64_decode($this->decrypt($buffer));
                //echo strlen($buffer).'<br>';
                fwrite($output_file_handle, $decrypted_string);
            }

            fclose($input_file_handle);
            fclose($output_file_handle);

            return true;
        }

    }//class aes_encryption

?>

和类名upload.php,此类是在上传时上传文件和自动加密文件。

<?php
include_once 'dbconfig.php';    
include 'aes.php';

session_start();
    if($_SESSION['user'] and ($_SESSION['nik'])){
    }
    else{ 
       header("location:index.php");
    }

if(isset($_POST['btn-upload']))

        $file       =rand(1000,100000)."-".$_FILES['file']['name'];
        $file_loc   =$_FILES['file']['tmp_name'];
        $file_size  =$_FILES['file']['size'];
        $dekripsi   =$_POST['dekripsi'];
        $file_type  =$_FILES['file']['type'];
        $file_nik   = $_SESSION ['nik'];
        $file_namalengkap = $_SESSION ['user'];
        $folder="uploads/";
        $new_size = $file_size/1024;  
        $new_file_name = strtolower($file);
        $final_file=str_replace(' ','-',$new_file_name);

        if (move_uploaded_file($file_loc,$folder.$final_file)){

            $sql="INSERT INTO tbl_uploads(file,namalengkap,nik,dekripsi,type,size) VALUES('$final_file','".$_SESSION ['user']."','".$_SESSION ['nik']."','$dekripsi','$file_type','$new_size')";
            mysql_query($sql);
            echo mysql_error();
            $crypt = new aes_encryption();

            $final_file = $file_loc; 

            $crypt->encrypt_file($final_file, $final_file.'.enc');


            ?>
                <script>
                    alert('successfully uploaded');
                    window.location.href='home.php?success';
                </script>
            <?php

        }
        else
        {
            ?>
                <script>
                    alert('gagal upload');
                    window.location.href='home.php?fail';
                </script>
            <?php
        }


?>

我试图运行,但它显示错误,

  

致命错误:未捕获的异常&#39;异常&#39;有消息&#39;不能   打开输入文件&#39;在C:\ xampp \ htdocs \ aes \ aes.php:29堆栈跟踪:#0   C:\ XAMPP \ htdocs中\ AES \ uploadaes.php(40):   aes_encryption-&GT; encrypt_file(&#39; C:\ XAMPP \ TMP \ pH值...&#39 ;,   &#39; C:\ xampp \ tmp \ ph ...&#39;)#1 {main}引入   第29行的C:\ xampp \ htdocs \ aes \ aes.php

我认为问题是$final_file = $file_loc ;无法获得价值,我不知道为什么它无法获得$file_loc的价值,但如果你有意见问题在哪里,你能告诉我吗? / p>

1 个答案:

答案 0 :(得分:0)

您已将文件从$file_loc移至$folder.$final_file,因此这似乎是您问题的根源。您应该按原样保留$ final_file,并使用:

$crypt->encryt_file($folder.$final_file, $folder.$final_file.'.enc');

您还需要对MySQL查询语句的输入进行某种检查。另请注意,mysql_query已弃用,建议您选择使用更新的库,例如pdomysqli