SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    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.

    PHP Code:
    $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.
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi !

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

    Thanks
    ZH


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •