PHP的 - 上传文件到另一台服务器没有卷曲 [英] PHP - Upload file to another server without curl

查看:124
本文介绍了PHP的 - 上传文件到另一台服务器没有卷曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我可以上传文件到另一台服务器,而不卷曲..
因为不是每个服务器都有卷曲...
谢谢...

http://php.net $ man
$ b

  function do_post_request($ url,/ manual / en / function.stream-context-create.php ) $ postdata,$ files = null)
{
$ data =;
$ boundary =---------------------。substr(md5(rand(0,32000)),0,10);

//收集Postdata
foreach($ postdata as $ key => $ val)
{
$ data。= - $ boundary \\\
;
$ data。=Content-Disposition:form-data; name = \。$ key。\\\\
\\\
。$ val。\\\
;
}

$ data。= - $ boundary \\\
;

//收集Filedata
foreach($文件为$ key => $文件)
{
$ fileContents = file_get_contents($ file ['tmp_name'] );
$ b $ data。=Content-Disposition:form-data; name = \{$ key} \; filename = \{$ file ['name']} \ \\\
;
$ data。=Content-Type:image / jpeg\\\
;
$ data。=Content-Transfer-Encoding:binary \\\
\\\
;
$ data。= $ fileContents。\\\
;
$ data。= - $ boundary - \\\
;

$ b $ params = array('http'=> array(
'method'=>'POST',
'header'=> 'Content-Type:multipart / form-data; boundary ='。$ boundary,
'content'=> $ data
));

$ ctx = stream_context_create($ params);
$ fp = fopen($ url,'rb',false,$ ctx);

if(!$ fp){
抛出新的异常(带有$ url的问题,$ php_errormsg);
}

$ response = @stream_get_contents($ fp);
if($ response === false){
抛出新的异常(从$ url读取数据的问题,$ php_errormsg);
}
返回$ response;


//设置数据(在这个例子中是帖子)

//样本数据
$ postdata =数组(
' name'=> $ _POST ['name'],
'age'=> $ _POST ['age'],
'sex'=> $ _POST ['sex']
);

//示例图片
$文件['image'] = $ _FILES ['image'];

do_post_request(http://example.com,$ postdata,$ files);


I have one question, can i upload file to another server without curl.. Because not every server have CURL ... Thanks...

解决方案

Yes it is possible using pure PHP fopen together with stream_context_create. The following example comes from the online PHP manual (http://php.net/manual/en/function.stream-context-create.php):

function do_post_request($url, $postdata, $files = null) 
{ 
    $data = ""; 
    $boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10); 

    //Collect Postdata 
    foreach($postdata as $key => $val) 
    { 
        $data .= "--$boundary\n"; 
        $data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n"; 
    } 

    $data .= "--$boundary\n"; 

    //Collect Filedata 
    foreach($files as $key => $file) 
    { 
        $fileContents = file_get_contents($file['tmp_name']); 

        $data .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$file['name']}\"\n"; 
        $data .= "Content-Type: image/jpeg\n"; 
        $data .= "Content-Transfer-Encoding: binary\n\n"; 
        $data .= $fileContents."\n"; 
        $data .= "--$boundary--\n"; 
    } 

    $params = array('http' => array( 
           'method' => 'POST', 
           'header' => 'Content-Type: multipart/form-data; boundary='.$boundary, 
           'content' => $data 
        )); 

   $ctx = stream_context_create($params); 
   $fp = fopen($url, 'rb', false, $ctx); 

   if (!$fp) { 
      throw new Exception("Problem with $url, $php_errormsg"); 
   } 

   $response = @stream_get_contents($fp); 
   if ($response === false) { 
      throw new Exception("Problem reading data from $url, $php_errormsg"); 
   } 
   return $response; 
} 

//set data (in this example from post) 

//sample data 
$postdata = array( 
    'name' => $_POST['name'], 
    'age' => $_POST['age'], 
    'sex' => $_POST['sex'] 
); 

//sample image 
$files['image'] = $_FILES['image']; 

do_post_request("http://example.com", $postdata, $files); 

这篇关于PHP的 - 上传文件到另一台服务器没有卷曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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