php 上传文件到服务器

/客户端代码

function upload()

{                      

 $file = $this->upload->data();

$file_name = $file['file_name'];
$filepath = getcwd().'/assets/android/'.$file_name;
读取文件内容
$handle = fopen($filename, "r");//读取二进制文件时,需要将第二个参数设置成'rb'
$contents = fread($handle, filesize ($filepath));//通过filesize获得文件大小,将整个文件一下子读到一个字符串中
fclose($handle);
删除本地刚上传此文件
unlink($filepath);

//客户端

文件内容转换成base64

$this->data['filecontent']=base64_encode($contents);

SERVER_BASE_URL="http://192.168.1.104:8080/ucenter";

$url_active = SERVER_BASE_URL."/index.php?/api/apns/upload";
$response = $this->curl_post ( $url_active, $this->data );

}

function curl_post($url, $vars) {
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, 1 );
    curl_setopt($ch, CURLOPT_HEADER, 0 ) ;
    curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
    $response = curl_exec($ch);
    curl_close($ch);
   
    if ($response)
    {
    return $response;
    }
    else
    {
    return false;
    }
    }

服务器

function upload()
    {
  
    $filecontent = $_POST['filecontent'];
   
    $resource = base64_decode ( $filecontent );
    if (! $resource) {
    $ret = array (
    'flag' => 0,
    'msg' => "failed"
    );
    echo json_encode($ret);
    return ;
    }
   
    // //写文件
    $path = getcwd()."/assets/certificates/".$file_name;
   
    $fp = fopen($path, "w+" );
   
    if ($fp) {
    fputs ( $fp, $resource);
    fclose ( $fp );
    $ret = array (
    'flag' => 1,
    'msg' => "succes"
    );
   
    } else {
    fclose ( $fp );
    $ret = array (
    'flag' => 0,
    'msg' => "failed"
    );
    }
    echo json_encode($ret);
    }



<!-- upload certificate Begin -->
<?php if(isset($flag)):?>
<article class="module width_full">
<header>
<h3><?php echo lang('v_ios_upload_certificate') ?></h3>
</header>

<div id="content" class="span10">
<!-- content starts -->
<div class="row-fluid sortable ui-sortable">
<div class="box span12">


<div class="box-content">
<?php echo form_open_multipart('plugin/iospush/iosactivate/upload/'.$register_id.'/'.$appname);?>
<div class="module_content">
<table class="table table-striped table-bordered bootstrap-datatable">
<tbody>
<tr>
<td><?php echo form_label(lang('v_ios_certificate_file')); ?>&nbsp;&nbsp;</td>
                                    <td>
                                        <?php echo form_upload('userfile');?>
                   </td>
</tr>
<tr></tr>
<tr></tr>
<tr>
<td><?php echo form_label(lang('v_ios_certificate_pwd')); ?>&nbsp;&nbsp;</td>
                                    <td>
                                        <?php 
echo form_password($crt_passwd);
                                        ?>
<span class="help-inline"><font color='error'>
                                    <?php echo form_error($crt_passwd['name']); ?>
                                    <?php echo isset($errors[$crt_passwd['name']]) ? $errors[$crt_passwd['name']] : '';?>
                                    </font></span>
                   </td>
</tr>
</tbody>
</table>
<br />
<div class="form-actions">
<button class="btn btn-primary" type="submit" id="uploadsubmit" 
style="width:70px;height:25px;">
<?php echo lang('v_ios_upload')?></button>
</div>

   </div>    
</form>
</div>
</div>
</div>
</div> <!-- content ends -->
</article> <!-- upload certificate Begin -->
<?php endif;?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值