您的位置:首页 > 编程语言 > PHP开发

php 上传文件到服务器

2013-10-17 17:24 288 查看
/////客户端代码

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')); ?>  </td>

<td>

<?php echo form_upload('userfile');?>

</td>

</tr>

<tr></tr>

<tr></tr>

<tr>

<td><?php echo form_label(lang('v_ios_certificate_pwd')); ?>  </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;?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: