skip to Main Content

I have tried below code, since our PHP version is little bit older 5.4.16, so this code is not working with that version, also new CurlFile($_FILES[“upfile”][“tmp_name”], $_FILES[“upfile”][“type”], $_FILES[“upfile”][“name”]), not working with php 5.4.16. any help is appreciated

$post = "@" . $_FILES["upfile"]["tmp_name"]
    . ";type=" . $_FILES["upfile"]["type"]
    . ";filename=" . basename($_FILES["upfile"]["name"]);
    echo "<pre>";
    print_r($post);
    echo "</pre>";

    $conn = curl_init('//third party url');

// send a file
    curl_setopt($conn, CURLOPT_POST, true);
    curl_setopt(
        $conn,
        CURLOPT_POSTFIELDS,
        array(
            'file'        => '@' . realpath($_FILES["upfile"]["tmp_name"]),
        )
    );

// output the response
    curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);


// close the session

    $result      = curl_exec($conn);
    $header_info = curl_getinfo($conn, CURLINFO_HEADER_OUT);
    $header_size = curl_getinfo($conn, CURLINFO_HEADER_SIZE);
    $header      = substr($result, 0, $header_size);
    $body        = substr($result, $header_size);
    curl_close($conn);

2

Answers


  1. '@' . realpath($_FILES["upfile"]["tmp_name"])

    What is the @ sign for? For preventing errors? It won´t. It will append the at sign at the beginning of your filepath, which leads to the problem, that the file is not found. IF you want to prevent errors, you have to just do @realpath(...), but its discouraged to use this operator.

    Removing it should fix your problem.


    As Ingus mentioned, your Post data looks odd as well, you should not build the query string by yourself, instead use http_build_query, which is an inbuilt function from PHP.

    Use it like a so:

    $post = http_build_query($_POST);

    Also by “not working”, what do you mean exactly. What error are you receiving?

    Login or Signup to reply.
  2. EDIT try with this code:
    If this still does not work try to check if file is there. ..

    $url = '//third party url';
    $post = array('file'=> '@'.realpath($_FILES["upfile"]["tmp_name"]));
    
    $conn = curl_init();
    
    curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($conn, CURLOPT_URL, $url);
    curl_setopt($conn, CURLOPT_POST,1);
    curl_setopt($conn, CURLOPT_SAFE_UPLOAD, false);
    curl_setopt($conn, CURLOPT_POSTFIELDS, $post);
    curl_setopt($conn, CURLOPT_RETURNTRANSFER, true); //probably can remove
    //curl_setopt( $conn, CURLOPT_SSLVERSION, 3); //if still not working try to uncomment this
    
    $result = curl_exec($conn);
    $header      = substr($result, 0, $header_size);
    $body        = substr($result, $header_size); 
    
    curl_close($conn);
    

    Here is example of how i do it:
    My file in this example comes from database as blob.

    $query = mysqli_query($conn, "SELECT name, type, size, content FROM my_files WHERE id='".intval($id)."'");
    
    list($name, $type, $size, $content) = mysqli_fetch_array($query);
    $name=html_entity_decode($name, ENT_NOQUOTES, 'UTF-8');
    
    
    file_put_contents($_SERVER["DOCUMENT_ROOT"].'/temporarylocation/'.$name, $content);
    
    $file_name_with_full_path = realpath($_SERVER["DOCUMENT_ROOT"].'/temporarylocation/'.$name);
    $post = array('file'=> '@'.$file_name_with_full_path);
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_USERPWD, $user.":".$password);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch); 
    curl_close($ch);
    
    $resu = json_decode($response, TRUE);
    

    I hope this code helps you! Remember this is not ready to go code. Just an example. ..

    Here are CURL manual: https://www.php.net/manual/en/book.curl.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search