php uploads,uploads.php

header("Access-Control-Allow-Origin: *");

header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS");

@set_time_limit(60);

$rootDir = './uploads';

if(!file_exists($rootDir)) {

@mkdir($rootDir);

}

$targetDir = $rootDir.'/'.date('ymd');

if (!file_exists($targetDir)) {

@mkdir($targetDir);

}

if ( empty($_FILES) ) {

echo json_encode(['status' => 0, 'msg' => '没找到文件']);

exit;

}

if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {

echo json_encode(['status' => 0, 'msg' => '上传文件出错']);

exit;

}

if ( $_FILES['file']['size'] > 2*1024*1024 ) {

echo json_encode(['status' => 0, 'msg' => '文件不能超过2MB']);

exit;

}

$fileName = $_FILES["file"]["name"];

$suffix = strtolower(array_pop(explode('.',$_FILES["file"]["name"])));

if ( !in_array($suffix, ['jpg', 'jpeg', 'bmp', 'gif', 'png', 'zip']) ) {

echo json_encode(['status' => 0, 'msg' => '只能上传图片或ZIP包']);

exit;

}

$newFile = $targetDir.DIRECTORY_SEPARATOR.md5(uniqid()).'.'.$suffix;

move_uploaded_file($_FILES["file"]["tmp_name"], $newFile);

$arr = explode('/', $newFile);

$name = $arr[count($arr)-2]."/".$arr[count($arr)-1];

$json = ['status' => 1, "url"=>"http://". $_SERVER['HTTP_HOST'] ."/uploads/".$name];

echo json_encode($json);

exit;

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值