1. Recently, PHP is used to connect with the IOS side. When uploading multiple pictures, the APP side calls the background single picture upload interface through a loop, and the background saves the picture and stores the path in the database. However, the final result is that as many pictures are uploaded as there are identical pictures in the database, this means that the interface can only process one picture.
The backend code is as follows (not yet optimized)
//Format the $_FILES array
$path = "./upload/post/";
$valid_formats = array('jpg','png','gif','bmp','jpeg','PNG','JPG','JPEG','GIF','BMP');
if(!isset($_POST) || !$_SERVER['REQUEST_METHOD'] == 'POST') {
$mess = [
'code' => 0,
'message' => '请求数据失败',
'result' => ['msg' => '请求参数或者请求方式错误'],
];
echo json_encode($mess);
exit;
}
if($_FILES['ava']['error'] != 0) {
$mess = [
'code' => 0,
'message' => '请求数据失败',
'result' => ['msg' => '文件上传失败',],
];
}
$name = $_FILES['ava']['name'];
$size = $_FILES['ava']['size'];
if(!strlen($name)) {
$mess = [
'code' => 0,
'message' => '请求数据失败',
'result' => ['msg' => '文件名不存在'],
];
echo json_encode($mess);
exit;
}
$ext = $this -> getExtension($name);
if(!in_array($ext,$valid_formats)) {
$mess = [
'code' => 0,
'message' => '请求数据失败',
'result' => ['msg' => "请上传jpg','png','gif','bmp','jpeg'格式图片(拓展名大写也是可以的)"],
];
echo json_encode($mess);
exit;
}
if($size > (3 * 1024 * 1024)) {
$mess = [
'code' => 0,
'message' => '请求数据失败',
'result' => ['msg' => '图片大小不应超过3M'],
];
echo json_encode($mess);
exit;
}
$actualName = md5(time().substr(str_replace(" ", '_', $ext),5)).".".$ext;
$tmp = realpath($_FILES['ava']['tmp_name']);
if(!is_dir($path)) {
if(!mkdir($path,0777,true)) {
$mess = [
'code' => 4,
'message' => '请求资源失败',
'result' => '文件夹创建失败',
];
echo json_encode($mess);
exit;
}
}
if(!move_uploaded_file($tmp,$path.$actualName)) {
$mess = [
'code' => 0,
'message' => '请求数据失败',
'result' => ['msg' => '上传文件失败',],
];
echo json_encode($mess);
exit;
}
// 组装图片数据
$data['url'] = $this -> visitpath.'post/'.$actualName;
$data['pid'] = $pid;
$data['atime'] = time();
$res = model('postgallery') -> insert($data);
if(!$res) {
$mess = [
'code' => 0,
'message' => '请求数据失败',
'result' => ['msg' => '图片上传失败','data' => $data],
];
echo json_encode($mess);
exit;
}
$data['result']['data']['test'] = $n;
$mess = [
'code' => 1,
'message' => '请求数据成功',
'result' => ['msg' => '图片成功',],
];
echo json_encode($mess);
exit;
The result is like this:
It is indeed unoptimized code. . .
The key step in the code
$actualName = md5(time().substr(str_replace(" ", '_', $ext),5)).".".$ext;
Get the path through time() + ext, then have you considered
在同一秒内,相同后缀的不同图片上传
that it will be the same pathLooking at the results of your database, this is confirmed
How to solve it
1. Accurate to milliseconds or microseconds, there is a probability of duplication
2. Add conditional rand random number to path generation, there is also a probability of duplication
3.
uniqid
Well, let’s combine 1+2+3, the probability of repetition is too low, unless you are a bat, let’s talk about it at that level