how to send / get files via web-services in php

后端 未结 4 2018
你的背包
你的背包 2021-01-02 16:02

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

thanks

4条回答
  •  孤城傲影
    2021-01-02 16:31

    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.

    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.

    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);
    

    the webservice to get a file

    $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: sorry for some reason stackoverflow doesn't like to make a link of $_FILES ... so I have linked the superglobals page instead

提交回复
热议问题