简体   繁体   English

使用 PHP curl 将文件发送到 WebDav 服务器

[英]Sending file to WebDav server using PHP curl

I am trying to send file to the webDav server using curl.我正在尝试使用 curl 将文件发送到 webDav 服务器。 My code is working fine but the problem i am facing is curl -T is deleting all the existing file in network.我的代码工作正常,但我面临的问题是 curl -T 正在删除网络中的所有现有文件。 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提前致谢

You can use this code to send file using curl.您可以使用此代码使用 curl 发送文件。 Make sure you use CURLFile class.确保使用 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"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM