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' => '請求數(shù)據(jù)失敗',
'result' => ['msg' => '請求參數(shù)或者請求方式錯誤'],
];
echo json_encode($mess);
exit;
}
if($_FILES['ava']['error'] != 0) {
$mess = [
'code' => 0,
'message' => '請求數(shù)據(jù)失敗',
'result' => ['msg' => '文件上傳失敗',],
];
}
$name = $_FILES['ava']['name'];
$size = $_FILES['ava']['size'];
if(!strlen($name)) {
$mess = [
'code' => 0,
'message' => '請求數(shù)據(jù)失敗',
'result' => ['msg' => '文件名不存在'],
];
echo json_encode($mess);
exit;
}
$ext = $this -> getExtension($name);
if(!in_array($ext,$valid_formats)) {
$mess = [
'code' => 0,
'message' => '請求數(shù)據(jù)失敗',
'result' => ['msg' => "請上傳jpg','png','gif','bmp','jpeg'格式圖片(拓展名大寫也是可以的)"],
];
echo json_encode($mess);
exit;
}
if($size > (3 * 1024 * 1024)) {
$mess = [
'code' => 0,
'message' => '請求數(shù)據(jù)失敗',
'result' => ['msg' => '圖片大小不應(yīng)超過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' => '文件夾創(chuàng)建失敗',
];
echo json_encode($mess);
exit;
}
}
if(!move_uploaded_file($tmp,$path.$actualName)) {
$mess = [
'code' => 0,
'message' => '請求數(shù)據(jù)失敗',
'result' => ['msg' => '上傳文件失敗',],
];
echo json_encode($mess);
exit;
}
// 組裝圖片數(shù)據(jù)
$data['url'] = $this -> visitpath.'post/'.$actualName;
$data['pid'] = $pid;
$data['atime'] = time();
$res = model('postgallery') -> insert($data);
if(!$res) {
$mess = [
'code' => 0,
'message' => '請求數(shù)據(jù)失敗',
'result' => ['msg' => '圖片上傳失敗','data' => $data],
];
echo json_encode($mess);
exit;
}
$data['result']['data']['test'] = $n;
$mess = [
'code' => 1,
'message' => '請求數(shù)據(jù)成功',
'result' => ['msg' => '圖片成功',],
];
echo json_encode($mess);
exit;
The result is like this:
小伙看你根骨奇佳,潛力無限,來學PHP伐。
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 在同一秒內(nèi),相同后綴的不同圖片上傳
that it will be the same path
Looking 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