Skip to main content

How to upload a file to remote server in php using curl

To upload  files using CURL please check your curl on remote server must be enabled.


All uploading functionality will be done at local server and using base64_encode we will copy our  local uploaded file to remove server.



Create two files :
  1. local.php  -  for local server to upload file in local server.
  2. server.php - for live remote server to copy local uploaded file.

1. Script for local.php


<form enctype="multipart/form-data" method='post'>
  <input name="file" type="file" value="choose">
  <input type="submit" value="Upload">
</form>
 
<?php
if(isset($_POST["submit"])){
    // Move file to a temp location
    $uploadDir = 'your local upload directory path here';
    $uploadFile = $uploadDir . basename($_FILES['file']['name']);
    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)){
        
        // set array to send data to remote server
        $remoteData = array(
            'fileName' => $uploadFile,
            'fileData' => base64_encode($uploadFile)
        );

        // start curl set up for remote file upload
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, 'http://your-server/server.php');
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $remoteData);
        $response = curl_exec($curl);
        curl_close($curl);
        echo $response;   // set response to server.php file 
    } else {
        echo "Your file not uploaded to server.";
    }
} ?> 


2.  Script for server.php
 
<?php
// check if post data is available or not
if (isset($_POST['fileName']) && $_POST['fileData']){
    // save uploaded file
    $uploadDir = 'your path to save file';
    file_put_contents(
        $uploadDir. $_POST['fileName'],
        base64_decode($_POST['fileData'])
    );// done  
   echo "Success";  
} else { 
   echo "Error : File not uploaded to remote server."; 
} ?>

Comments

Popular posts from this blog

Getting Started With Python - for PHP developers

This blog is not specially for PHP developers. So don't worry if you are not from PHP Development Background. Recommended Reading - IBM - Python basics for PHP developers After reading above article i am assuming you already knows A) What is Python. B) How Python is different from PHP (If you are from PHP Background) C) Basic understanding for variables in Python. This Tutorial will cover Web Application Development using Python and  MySQL . Prerequisite  1. Install  MySQL   for database storage.  2. Install Python .  3. Install Django .    why Django ?   Yess right question.  Django is open source web framework which is most popular to create a web application in python language. No doubt you can start development by core python. But if you are going to develop a secure standard application than Django is a boiler plate to get started quickly. Except  Django   there are also some python w...

Blog CMS Admin Panel Using Laravel within 10 minutes

Hey !! Welcome Back. I am using latest version    Laravel 5.4   for  this post. Before continue please check your are updated with your PHP version required for the running environment of  Laravel. And you have installed the  Composer  in your system. You can check & verify  your running environment from  here . If you are up to date with the minimum requirement than lets continue. Open your terminal and type enter after below command : composer create - project -- prefer - dist laravel / laravel blog   Now wait for a moment. If you got any error during installation than terminal will warn you. Please  Google   the issue and resolve it.  If you are still facing any issue for the same just comment below. If you installed the laravel  successfully than in your system than lets move to next step. There will be an new folder " blog "  where you executed the above composer command.  Just...