请选择 进入手机版 | 继续访问电脑版

5D同程

 找回密码
 立即注册
搜索
热搜: 活动 交友
查看: 308|回复: 0

上传文件类

[复制链接]

4

主题

0

回帖

34

积分

管理员

积分
34
发表于 2024-10-26 19:45:48 | 显示全部楼层 |阅读模式
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Layui Upload Example</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui-src/dist/css/layui.css"  media="all">
</head>
<body>

<button type="button" class="layui-btn" id="test-upload-img">选择图片</button>
<button type="button" class="layui-btn" id="test-upload-file">选择文件</button>

<script src="https://cdn.jsdelivr.net/npm/layui-src/dist/layui.all.js"></script>
<script>
//JavaScript代码区域
</script>
</body>
</html>

layui.use('upload', function(){
  var upload = layui.upload;

  //图片上传
  upload.render({
    elem: '#test-upload-img'
    ,url: 'upload_img.php' //改成你的图片上传接口
    ,accept: 'images'
    ,done: function(res){
      console.log(res);
    }
  });

  //文件上传
  upload.render({
    elem: '#test-upload-file'
    ,url: 'upload_file.php' //改成你的文件上传接口
    ,accept: 'file'
    ,done: function(res){
      console.log(res);
    }
  });
});

<?php
// upload_img.php
$upload_path = 'path/to/your/upload/directory/'; // 上传文件保存的路径
$file = $_FILES['file'];
$filename = $upload_path . basename($file['name']);
move_uploaded_file($file['tmp_name'], $filename);
echo json_encode(array('status' => 'success', 'filename' => $filename));
?>

<?php
// upload_file.php
// 和上面的代码基本一致,只是上传路径和返回的数据不同
$upload_path = 'path/to/your/upload/directory/';
$file = $_FILES['file'];
$filename = $upload_path . basename($file['name']);
move_uploaded_file($file['tmp_name'], $filename);
echo json_encode(array('status' => 'success', 'filename' => $filename));
?>

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|5D同程

GMT+8, 2024-12-10 18:35 , Processed in 0.078125 second(s), 27 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表