文件正在上传但没有得到回复

时间:2016-10-26 12:47:36

标签: php json encryption

我写了一个脚本来上传pdf文件,该文件加密文件的名称并将其上传到服务器。脚本工作正常,但没有得到json响应可以帮助我。

这是我的代码

<?php
//Turn off all error reporting
//error_reporting(0);
ini_set ("display_errors", "1");

 error_reporting(E_ALL);

 define("ENCRYPTION_KEY", "!@#$%^&*");
  /**
  * Returns an encrypted & utf8-encoded
   */
function encrypt($pure_string, $encryption_key) {
$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key,  utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
return $encrypted_string;
}

 /**
  * Returns decrypted original string
  */
  function decrypt($encrypted_string, $encryption_key) {
   $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv);
return $decrypted_string;
}


 $fieldname = 'pdfFile';
 $targetdir = 'upload/';
 $desiredtype = 'application/pdf';
 $return=array(
'status'    =>    'false',
'message'    =>    'Pdf file not uploaded!'
 );

 if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_FILES[ $fieldname ] )){
try{

    $obj = (object)$_FILES[ $fieldname ];
    $name = $obj->name;
    $size = $obj->size;
    $tmp  = $obj->tmp_name;
    $type = $obj->type;
    $error= $obj->error;






    if( is_uploaded_file( $tmp ) && $error == UPLOAD_ERR_OK && $type == $desiredtype ){

        $encrypted_name = encrypt($name, ENCRYPTION_KEY);

        $destination = $targetdir . $encrypted_name;
        $url = "webservice.pixcelservice.com";
        if( file_exists( $destination ) ){

            $return['status']='false';
            $return['message']='File already exists!';
            $return['line']=__LINE__;

            clearstatcache();

        } else {

           if($res = move_uploaded_file( $tmp, $destination )){

            chmod($destination, 0755);  
            $return['status']=$res ? 'true' : 'false';
            $return['message']=$res ? 'Pdf file uploaded successfully!' : 'Moving the file failed!';
            $return['path']="$url/$destination";

            }

     }

    } else {
        $return['status']='false';
        $return['message']='File upload error!';
        $return['line']=__LINE__;
    }
}catch( Exception $e ){
        $return['status']='false';
        $return['message']=$e->getMessage();
        $return['line']=__LINE__;
}

header('Content-type: application/json');
exit( json_encode( $return ) );

 } else {
 exit( header( 'HTTP/1.1 405 Method Not Allowed',true, 405 ) );
 }
  ?>

0 个答案:

没有答案
相关问题