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;?>
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;?>
相关文章推荐
- Android端通过Okhttp与PHP服务器进行交互并实现文件上传下载(一)
- Android上传文件到Web服务器,PHP接收文件(二)
- PHP:上传文件到服务器的方法
- Android之上传多个文件至php服务器,php服务器只能接收最后一个文件问题
- Android使用HttpClient实现文件上传到PHP服务器,并监控进度条
- 【Android】Android客户端使用okhttp上传文件php服务器
- windows基于阿帕奇+PHP服务器,实现vc++文件上传功能
- php利用curl上传文件到服务器(支持文件夹创建)
- [置顶] Android上传文件到Web服务器,PHP接收文件(一)
- PHP文件上传至另一台服务器
- 在php中ftp上传文件到服务器的操作
- js html5 上传文件、多文件,服务器 端php
- python上传字符和二进制的文件到PHP服务器
- 修改PHP服务器文件上传大小限制-apache,lighttpd,nginx
- Android上传文件到Web服务器,PHP接收文件(一)
- android上传文件php服务器接收代码
- iphone上传文件到php做的服务器的有关问题
- php上传文件到服务器 (注意权限)
- php把文件上传到远程服务器上例子
- cocos2dx 实现简单的文件上传到php服务器的demo