Open a file similar to $_FILE

Hello !

I have an issue, via curl I have to upload a file to a server and for that, it is required that the file is sent as a object. Its same like we upload the file and open it via “$_FILES”

So my question, please guide me how i can get this thing done:

$myfile = “/path/to/some/file/on/server”;

$filename = $FILE_OBJECT[$myfile][“name”];
$type = $FILE_OBJECT[$myfile][“type”];
$tmp_name = $FILE_OBJECT[$myfile][“tmp_name”];
$error = $FILE_OBJECT[$myfile][“error”];
$size = $FILE_OBJECT[$myfile][“size”];

Please guide me !

Thanks
ZH

Hi !

Thanks for the suggestion, I did what you suggested and it worked.

Thanks
ZH

Unless the remote server is doing something weird, the file doesn’t need to be sent as an object. You don’t need to construct an array like that on your server, it is entirely up to the remote server to deal with the file however it likes (it might not be using PHP at all).

Uploading a file with cURL is essentially the same as any other request. Create a cURL handle with curl_init(), set the appropriate options, then execute the request.


$ch = curl_init('http://remote.server/upload');
curl_setopt_array(array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS = array(
        'file_field_name' => '@/path/to/some/file/on/server',
        'another_field' => 'some value',
    ),
));
$response = curl_exec($ch);

The important part is specifying that the file_field_name (or whatever the remote server needs) field should be a file; this is done by prefixing the value with an @ followed by the path to the file on your server.