PHP: upload file from one server to another server

前端 未结 6 1151
野性不改
野性不改 2020-12-08 12:08

I\'m developing a website in a server and the storage is in another server and I have to deal with that somehow. I nerve experience this case and I found that the solution i

6条回答
  •  情深已故
    2020-12-08 12:29

    We are using same use case like you. we have two server : Application server and Storage server.Where application servers contains methods and UI part and storage server is where we upload files.Following is what we are using for file upload , that is working properly :

    1) You have to add one php file that contains following methods on your storage server :

    
    

    2) On your application server.

    Create a method that will pass $_FILES data to storage server.

     $data = array(
        'file' => new CURLFile($_FILES['file']['tmp_name'],$_FILES['file']['type'], $_FILES['file']['name']),
        'destination' => 'destination path in which file will be uploaded',
        'calling_method' => 'upload_file',
        'file_name' => 'file name, you want to give when upload will completed'
    ); 
    
    **Note :CURLFile class will work if you have PHP version >= 5**
    
     $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, PATH_FOR_THAT_PHP_FILE_ON_STORAGE_SERVER_FILE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60000);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
    
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
    
        $msg = FALSE;
    } else {
        $msg = $response;
    }
    
    curl_close($ch);
    echo $msg;
    

    In this way using CURL it will call storage server file method and upload files and in same way you can call method to delete file.

    Hope this will help.

提交回复
热议问题