Skip to content
Advertisement

Sending file to WebDav server using PHP curl

I am trying to send file to the webDav server using curl. My code is working fine but the problem i am facing is curl -T is deleting all the existing file in network. Is there any other flag which can only send one file and donot touch other files in a network.

exec('curl -T "' . $src . $file . '" ' . $full_url . str_replace(' ', '', $upDir) . str_replace(' ', '', $file));

Thanks in advance

Advertisement

Answer

You can use this code to send file using curl. Make sure you use CURLFile class.

$file = $request->file('file');

    $file_name = $file->getClientOriginalName();
    $file_ext = $file->getClientOriginalExtension();
    $file_path = $file->getRealPath();

    $fileName = request('fileName');

    $curl_file =  new CURLFile($file_path, $file_ext, $file_name);
    $post_data = array(
      'files' => $curl_file,
      'name' => $fileName
    );

    $target_url = env('URL', null) . "/library";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $target_url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content_Type: application/json',
      'Authorization: Bearer ' .  $_SESSION["token"],
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

    $response = curl_exec($ch);
    curl_close($ch);

    $contents = $response;
    $content = json_decode($contents, true);
    
    return response()->json(["status" => "ok"]);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement