php - 使用 PHP curl 将文件发送到 WebDav 服务器
问题描述
我正在尝试使用 curl 将文件发送到 webDav 服务器。我的代码工作正常,但我面临的问题是 curl -T 正在删除网络中的所有现有文件。是否有任何其他标志只能发送一个文件而不能触及网络中的其他文件。
exec('curl -T "' . $src . $file . '" ' . $full_url . str_replace(' ', '', $upDir) . str_replace(' ', '', $file));
提前致谢
解决方案
您可以使用此代码使用 curl 发送文件。确保使用 CURLFile 类。
$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"]);
推荐阅读
- visual-studio - 用于 Javascript/AJAX 的 Visual Studio Code 镜头
- javascript - Discord Bot 多次响应事件
- php - 大小动态数组PHP,POST方式,用Jquery
- node.js - Jest 进程在所有测试执行之前退出,没有任何反馈
- java - Char变量减去char变量在java中解析为Int
- laravel - 登录 Laravel 8 应用程序时出现“未找到列” - 假设缺少“id”列?
- jmeter - 如何解决 Jmeter 和软件之间的问题
- reactjs - 使用 Apollo 客户端处理前端的渐进式披露
- javascript - 如果 count == 0 AND otherCount == 0
- heroku - Heroku 计划选择哪个