有个需求是从php端上传zip文件到python端并且解压到指定目录,以下是解决方法

1、python端,使用的web.py

def POST(self):
post_data = web.input(myfile={}) # 文件夹存在则删除,保证每次都是解压最新文件
dir_path = post_data['dir_path']
isExists = os.path.exists(dir_path)
try:
if not isExists:
os.makedirs(dir_path, 0o777)
else:
os.system('sudo rm -rf ' + dir_path)
os.makedirs(dir_path, 0o777)
except:
return False # 保存文件
if 'myfile' in post_data: # to check if the file-object is created
filepath=post_data.myfile.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.
filename=filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension)
fout = open(dir_path +'/'+ filename,'w') # creates the file where the uploaded file should be stored
fout.write(post_data.myfile.file.read()) # writes the uploaded file to the newly created file.
fout.close() # closes the file, upload complete. # 解压zip文件
try:
zip_ref = zipfile.ZipFile(dir_path + '/' + filename, 'r')
zip_ref.extractall(dir_path )
zip_ref.close()
os.system('sudo rm -rf ' + dir_path + '/' + filename)
except:
return False return True

2、php端curl方法

<?php
$url = "/xx/yy"; $post_data = ['myfile' => new \CURLFILE($file_path), 'dir_path' => $dir_path]; $ch = curl_init();
curl_setopt($ch , CURLOPT_URL , $url);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch , CURLOPT_POST, 1);
curl_setopt($ch , CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);

这里需要特别说明下,以下这种写法

$post_data = array(
// 需要注意的是,在路径前必须带上@,不然只会当做是简单的键值对
'pic' => '@'.realpath($path)
'name' => 'issac'
);

只有在php5.5以下版本有效,换言之现在根本没有用,而且现在网上充斥的全是这种过时的失效版本,@字符什么,现在根本没有用了,很多资料并没有注明,害我调试了好久。

所以为了兼容不同版本,可以参考这个方法

function upload_file($url,$filename,$path,$type){
//php 5.5以上的用法
if (class_exists('\CURLFile')) {
$data = array('file' => new \CURLFile(realpath($path),$type,$filename));
} else {
$data = array(
'file'=>'@'.realpath($path).";type=".$type.";filename=".$filename
);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return_data = curl_exec($ch);
curl_close($ch);
echo $return_data;
}

参考资料

http://webpy.org/cookbook/storeupload/

https://www.jianshu.com/p/63b32ceea742

https://my.oschina.net/forMemory/blog/374451

最新文章

  1. CentOS7中升级Docker版本
  2. 记一次FTP上传文件总是超时的解决过程
  3. MATLAB符号运算 分类: 图像处理 2015-07-31 22:53 3人阅读 评论(0) 收藏
  4. C语言练习代码
  5. javascript删除目标标签
  6. SQL服务器名称的更改
  7. Linq101-Projection
  8. 将使用netTcp绑定的WCF服务寄宿到IIS7上全记录 (这文章也不错)
  9. 21. 无法执行该操作,因为链接服务器”Server_202”的 OLE DB 访问接口 “SQLNCLI10″ 无法启动分布式事务”
  10. Java集群--大型网站是怎样解决多用户高并发访问的
  11. C语言函数名与函数指针详解
  12. Pyhton编程(一)之第一个Pyhton程序
  13. SpringMVC的流程分析(一)—— 整体流程概括
  14. [JSOI2009]密码 [AC自动机]
  15. C++ Error C2664:无法将参数 1 从“const char [9]”转换为“LPCWSTR”解决方案
  16. 如何删除github里面的项目
  17. [todo] 3rd
  18. IdentityServer4授权和认证集成Identity和profile
  19. Android Studio NDK开发环境搭建
  20. [译]ABP vNext介绍

热门文章

  1. MVC框架的实现
  2. 使用sqlyog连接ubuntu mysql server错误解决方案
  3. So you want to write a desktop app in Python
  4. MVC:控制器名与被调用模型名称发生冲突的解决方案
  5. PDO链式操作——针对关键字出现问题的解决方案
  6. OpenGL学习 Our First OpenGL Program
  7. ABAP Netweaver和Hybris Enterprise Commerce Platform的登录认证
  8. 2019.03.14 ZJOI2019模拟赛 解题报告
  9. Windows 7下的ARP
  10. app上线