如何通过php中的Web服务发送/获取文件 [英] how to send / get files via web-services in php

查看:80
本文介绍了如何通过php中的Web服务发送/获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能吗? 发送文件的正确方法是什么?

is this possible ? what is the correct way to send files ?

谢谢

推荐答案

如果您希望Web服务上载/下载文件,则不需要.无论如何,您都可以使用curl( http://fr.php.net/curl )上载/下载文件来自其他Web服务器.

I don't if you want your webservice to upload/download files. Anyway you can use curl(http://fr.php.net/curl ) to upload/download file from other webserver.

要从用户获取一些文件到Web服务,与从表单获取文件几乎相同,请使用superglobal变量:$ _ FILES(

To get some file uploaded to the webservice from the user it's pretty much the same as gettings it from a form, please use the superglobal variable:$_FILES (doc) to get upload files.

用于从php上传到网络服务

for uploading from php to a webservice

$fullflepath = 'C:\temp\test.jpg';
$upload_url = 'http://www.example.com/uploadtarget.php';
$params = array(
 'photo'=>"@$fullfilepath",
 'title'=>$title
);  

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);
curl_close($ch);

获取文件的网络服务

$uploads_dir = '/uploads';
foreach ($_FILES["photo"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["photo"]["tmp_name"][$key];
        $name = $_FILES["photo"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}

PS:抱歉,由于某些原因,stackoverflow不希望链接到$_FILES ...,所以我改为链接了superglobals页面

PS: sorry for some reason stackoverflow doesn't like to make a link of $_FILES ... so I have linked the superglobals page instead

这篇关于如何通过php中的Web服务发送/获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆