php网站上传到服务器上,如何把文件提交给接口上传到服务器(php版)

2.html端: index.html

name:

gender:male female

photo:

$("[link=sub]").click(function (){

var data = new FormData($('#uploadForm')[0]);

$.ajax({

url: 'a.php',

type: 'POST',

data: data,

dataType: 'json',

cache: false,

processData: false,

contentType: false

}).done(function(ret){

if(ret['isSuccess']){

var result = '';

result += 'name=' + ret['name'] + '
';

result += 'gender=' + ret['gender'] + '
';

                result += ''%20+%20ret%5B'photo'%5D%C2%A0%20+%20'';

$('#result').html(result);

}else{

alert('提交失敗');

}

});

return false;

});

//type=file 一旦change就上传图片

$("[name=photo]").change(function (){

var data = new FormData($('#uploadForm')[0]);

$.ajax({

url: 'a.php',

type: 'POST',

data: data,

dataType: 'json',

cache: false,

processData: false,

contentType: false

}).done(function(ret){

if(ret['isSuccess']){

var result = '';

result += 'name=' + ret['name'] + '
';

result += 'gender=' + ret['gender'] + '
';

                result += ''%20+%20ret%5B'photo'%5D%C2%A0%20+%20'';

$('#result').html(result);

}else{

alert('提交失敗');

}

});

return false;

});

3.html提交给自己的程序a.php (如果用js提给接口会存在跨域问题)

//文件路径

$file="@".$_FILES['photo']['tmp_name'];

//获取文件扩展名

$file_name_arr=pathinfo($_FILES['photo']['name']);

$file_extension=$file_name_arr['extension'];

$name=$_POST['name'];

$gender=$_POST['gender'];

$ch = curl_init();

$post_data = array(

'name' => $name,

'gender' => $gender,

'file' => $file,

'file_extension'=>$file_extension,

);

curl_setopt($ch, CURLOPT_HEADER, false);

//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);

curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);      //得到返回值且不显示在页面上

curl_setopt($ch, CURLOPT_URL, 'https://www.wlphp.com/myblog_demo/api/uploadfile/index.php');   //可以理解为接口地址

$info= curl_exec($ch);    //可以理解为接口返回值

curl_close($ch);

//完全输出接口返回值

echo $info;

4.php接口:

error_reporting(0);

$file_extension=$_POST['file_extension'];

$filename = "./upload/".time().time().".".$file_extension;

if(move_uploaded_file($_FILES['file']['tmp_name'], $filename)){

$response['isSuccess'] = true;

$response['name'] = $_POST['name'];

$response['gender'] = $_POST['gender'];

$response['photo'] = dirname('http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]).$filename;

}else{

$response['isSuccess'] = false;

}

echo json_encode($response);

版权声明:若无特殊注明,本文皆为《菜鸟站长》原创,转载请保留文章出处。

本文链接:如何把文件提交给接口上传到服务器(php版) - http://wlphp.com/?post=62

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
dropzone.js 上传的文件默认是保存在浏览器的缓存中,你可以通过设置 `autoProcessQueue` 属性为 `true`,即自动提交队列,将文件传输服务器后端进行保存。 具体步骤如下: 1. 在 HTML 中设置一个 `form` 表单,设置 `enctype="multipart/form-data"`。 2. 创建一个 Dropzone 实例,并配置相关属性。例如: ```javascript var myDropzone = new Dropzone("#my-dropzone", { url: "/file/upload", // 文件上传的后台接口 autoProcessQueue: true, // 是否自动将文件传输服务器 paramName: "file", // 文件参数名,后端可以通过这个参数名获取文件 maxFilesize: 2, // 最大文件大小限制 addRemoveLinks: true, // 添加移除文件链接 acceptedFiles: "image/*,application/pdf", // 允许上传的文件类型 dictDefaultMessage: "将文件拖到此处上传或者点击上传", // 默认提示信息 dictFallbackMessage: "您的浏览器不支持拖放文件上传", // 不支持拖放上传的提示信息 dictFileTooBig: "文件过大 ({{filesize}}MiB),最大上传大小: {{maxFilesize}}MiB.", // 文件过大的提示信息 dictInvalidFileType: "无法上传此类型的文件", // 不允许上传的文件类型的提示信息 dictResponseError: "服务器响应错误 {{statusCode}}.", // 服务器响应错误的提示信息 dictCancelUpload: "取消上传", // 取消上传按钮的提示信息 dictCancelUploadConfirmation: "您确定要取消上传吗?", // 确认取消上传的提示信息 dictRemoveFile: "移除文件", // 移除文件按钮的提示信息 dictMaxFilesExceeded: "只能上传 {{maxFiles}} 个文件", // 超过最大文件数量限制的提示信息 headers: {"Authorization": "Bearer " + token} // 设置请求头,例如 token }); ``` 3. 在后端接口中,可以通过常规方式(如 PHP 中的 `move_uploaded_file` 函数)将文件保存到服务器中指定的路径。 例如: ```php <?php $targetDir = "/var/www/html/uploads/"; // 文件保存的目标路径 if (!empty($_FILES)) { $tempFile = $_FILES["file"]["tmp_name"]; $targetFile = $targetDir . $_FILES["file"]["name"]; move_uploaded_file($tempFile, $targetFile); // 将文件从临时目录移动到目标路径 echo "success"; } ?> ``` 4. 在前端中,可以监听 Dropzone 的 `success` 或 `error` 事件,以判断文件上传是否成功。例如: ```javascript myDropzone.on("success", function(file, response) { console.log(response); // 打印上传成功后的响应结果 }); myDropzone.on("error", function(file, errorMessage) { console.log(errorMessage); // 打印上传失败的错误信息 }); ``` 以上是一个基本的使用 Dropzone.js 上传文件并保存到服务器的流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值