Upload Files in php using REST

I think you should be looking for $_FILES['file_contents'] and not $_POST['file']. – user1190992


php5.5+

$filePath = $_FILES['file_upl']['tmp_name'];
$type=$_FILES['file_upl']['type'];
$fileName = $_FILES['file_upl']['name'];
    
$data = array('file_upl' => curl_file_create($filePath, $type, $fileName));

$ch = curl_init();     
curl_setopt($ch, CURLOPT_URL, 'http://localhost/api/upload.php');
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);

Try this.

index.php

<?php
    echo "<pre>";
    print_r($_FILES);
    error_reporting(9);
    if($_REQUEST['action'] == 'submit') {
        $ch = curl_init();
        $filePath = $_FILES['file_upl']['tmp_name'];
        $fileName = $_FILES['file_upl']['name'];
        $data = array('name' => 'Foo', 'file' => "@$filePath", 'fileName' =>$fileName);             
        curl_setopt($ch, CURLOPT_URL, 'http://www.restServiceHost.com/file3/upload.php');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_exec($ch);
        curl_close($ch);
    }
?>

<form name="file_up" action="" method="POST" enctype="multipart/form-data">
Upload your file here
<input type="file" name="file_upl" id="file_upl"/>
<input type="submit" name="action" value="submit"/>
</form>

and upload.php in http://www.restServiceHost.com/file3

<?php
    echo "<pre>";
    echo 'in upload.php<br/>';
    print_r($_FILES);
    print_r($_REQUEST);
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_REQUEST["fileName"]);
?>