|
<!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));
?>
|
|