<input id="input-fcount-3" name="inputfcount3[]" multiple type="file" class="file-loading" accept="image/*">
首先,它必须验证图像计数最小02和最大05,然后文件大小最大8MB,之后图像类型“JPEG”和“PNG”仅允许,错误应为0.
然后我必须创建一个文件夹作为userName(登录用户的用户名,它在会话中可以请求)。在该文件夹中,应该创建另一个文件夹,如userName_ID_220(220为此必须从Database SQL max函数中选择最后插入的id)。在此文件夹中,图像应该像userName_id_220_0.jpg一样移动和重命名,下一个图像应该像userName_id_220_1.jpg。
我必须保存数据库内的所有图像路径,例如在pic0栏中的userName / userName_ID_001 / userName_id_220_0.jpg,其他图像路径和名称应保存在列pic1中,如userName / userName_ID_001 / userName_id_220_1.jpg,如下所示图片。
我尝试过foreach但是它为每个图像创建了每一行。所以请建议我一个解决方案。
date_default_timezone_set("Asia");
$currentDateTime = date('Y-m-d H:i:s');
$ad_topic = addslashes($_REQUEST['ad_topic']);
$distict = $_REQUEST['distict'];
$city = $_REQUEST['city'];
$price = $_REQUEST['price'];
$details = $_REQUEST['details'];
$user_name = $_REQUEST['user_name'];
$mainCat = addslashes($_REQUEST['Category']);
$subCat = addslashes($_REQUEST['subCategory']);
if (isset($_REQUEST['item_brand']) && isset($_REQUEST['condition']) ) {
$sub_sub_Cat = addslashes($_REQUEST['item_brand']);
$condition = $_REQUEST['condition'];
}else if(isset($_REQUEST['condition'])){
$condition = $_REQUEST['condition'];
}else if(isset($_REQUEST['item_brand'])){
$sub_sub_Cat = addslashes($_REQUEST['item_brand']);
}else{
}
if (empty($ad_topic) || strlen($ad_topic)<=10) {
echo "at least ad topic should be 10 characters long";
}else if(empty($distict) || $distict=='Select District'){
echo "Please Choose District";
}else if(empty($city) || $city=='Select City'){
echo "Please Choose City";
}else if(empty($price)){
echo "Please Enter Proper value for price";
}else if(empty($details) || strlen($details)<=15 ){
echo "At least details should be 15 characters long";
}else if(empty($user_name)){
echo "Something Wrong Contact Admin";
}else if (empty($mainCat) || ($mainCat=='Select Category')) {
echo "Please Choose Category";
}else if (empty($subCat) || ($subCat=='Select Sub Category')) {
echo "Please Choose Sub Category";
}else if(isset($_REQUEST['item_brand']) && $sub_sub_Cat=='Select Item / Brand'){
echo "Please Choose Item / Brand";
}else if (isset($_REQUEST['condition']) && $condition=='Select Condition') {
echo "Please Choose Condition";
}else if(!isset($_FILES) || count($_FILES['inputfcount3']['name']) < 2){
echo "Please choose at least 2 Pictures";
}else{
$totalPics = sizeof($_FILES['inputfcount3']['tmp_name']);
if ($totalPics >= 2) {
$file_type0 = ($_FILES['inputfcount3']['type'][0]) ;
$file_type1 = ($_FILES['inputfcount3']['type'][1]) ;
$file_type2 = ($_FILES['inputfcount3']['type'][2]) ;
$file_type2 = ($_FILES['inputfcount3']['type'][3]) ;
$file_type2 = ($_FILES['inputfcount3']['type'][4]) ;
$file_size0 = ($_FILES['inputfcount3']['size'][0]) ;
$file_size1 = ($_FILES['inputfcount3']['size'][1]) ;
$file_size2 = ($_FILES['inputfcount3']['size'][2]) ;
$file_error0 = ($_FILES['inputfcount3']['error'][0]) ;
$file_error1 = ($_FILES['inputfcount3']['error'][1]) ;
$file_error2 = ($_FILES['inputfcount3']['error'][2]) ;
if ( ($file_type0 == "image/jpeg") || ($file_type0 == "image/png") && ($file_type1 == "image/jpeg") || ($file_type1 == "image/png") && ($file_type2 == "image/jpeg") || ($file_type2 == "image/png")&& ($file_error0 == 0) && ($file_error1 == 0) && ($file_error2 == 0)) {
$sql = "INSERT INTO ads (user_name,ad_topic,district,city,price,condtition,details,status,date_time) VALUES (";
$sql .= "'$user_name',";
$sql .= "'$ad_topic',";
$sql .= "'$distict',";
$sql .= "'$city',";
$sql .= "'$price',";
$sql .= "'$condition',";
$sql .= "'$details',";
$sql .= "'waiting',";
$sql .= "'$currentDateTime')";
$q = mysql_query($sql,$link);
$last_id = mysql_insert_id();
echo "\nAD Details added Successfully, Waiting for Admin Approval";
$folder_name = './assets/img/products/'.$user_name;
$sub_folder_name = $user_name.'_adid_'.$last_id;
$sub_folder_dir = $folder_name.'/'.$sub_folder_name;
if(!file_exists($folder_name)){
$create_folder = mkdir('./assets/img/products/'.$user_name);
if (!file_exists($sub_folder_dir)) {
$create_sub_folder = mkdir($folder_name.'/'.$sub_folder_name);
}else{
echo "\nsub folder exists if con folder name";
}
}else{
if (!file_exists($sub_folder_dir)) {
$create_sub_folder = mkdir($folder_name.'/'.$sub_folder_name);
echo "\nDOWN HERE";
}else{
echo "\n sub folder exists if con folder name";
}
}
$file_tmp0 = ($_FILES['inputfcount3']['tmp_name'][0]) ;
$file_tmp1 = ($_FILES['inputfcount3']['tmp_name'][1]) ;
$file_tmp2 = ($_FILES['inputfcount3']['tmp_name'][2]) ;
$paths=array($sub_folder_name.'_0.jpg',$sub_folder_name.'_1.jpg',$sub_folder_name.'_2.jpg');
$destination0 = $folder_name.'/'.$sub_folder_name.'/'.$sub_folder_name."_0.jpg";
$mov_pic = move_uploaded_file($file_tmp0,$destination0);
PicResize($sub_folder_dir.'/',$sub_folder_name.'_0.jpg'); // $sub_folder_name Contains $user_name.'_adid_'.$last_id line number 56
$destination1 = $folder_name.'/'.$sub_folder_name.'/'.$sub_folder_name."_1.jpg";
$mov_pic = move_uploaded_file($file_tmp1,$destination1);
PicResize($sub_folder_dir.'/',$sub_folder_name.'_1.jpg');// $sub_folder_name Contains $user_name.'_adid_'.$last_id line number 56
$destination2 = $folder_name.'/'.$sub_folder_name.'/'.$sub_folder_name."_2.jpg";
$mov_pic = move_uploaded_file($file_tmp2,$destination2);
PicResize($sub_folder_dir.'/',$sub_folder_name.'_2.jpg');// $sub_folder_name Contains $user_name.'_adid_'.$last_id line number 56
if($mov_pic>0){
$pic0 = $user_name.'/'.$sub_folder_name.'/'.$sub_folder_name."_0.jpg";
$pic1 = $user_name.'/'.$sub_folder_name.'/'.$sub_folder_name."_1.jpg";
$pic2 = $user_name.'/'.$sub_folder_name.'/'.$sub_folder_name."_2.jpg";
$sqlPic = "UPDATE ads SET pic0='$pic0', pic1='$pic1', pic2='$pic2' WHERE adid='$last_id'";
$qry = mysql_query($sqlPic,$link);
}
}else{ // Ending IF condition of Image type, size, validations
echo "\n Not Suitable Image";
}
}
else{
echo "\n YOU HAVE UPLOADED $totalPics PICTURES, YOU MUST HAVE TO UPLOAD 02 PICTURES";
}
}
我面临一些问题,比如如果我上传2张图片获取$ file_type2 =的错误未定义偏移量($ _FILES ['inputfcount3'] ['type'] [2]);如何修复该错误并简化此代码?
答案 0 :(得分:0)
当你上传两张图片时,它自然会出现这个错误 - $file_type2
用于第三张图片不存在。您必须将所有$file_type
更改为:
$file_type2 = (isset($_FILES['inputfcount3']['type'][2]) ? $_FILES['inputfcount3']['type'][2] : '') ;
您的$_FILES
数组如下所示:
Array
(
[inputfcount3] => Array
(
[name] => Array
(
[0] => test1.jpg
[1] => test2.png
)
[type] => Array
(
[0] => image/jpeg
[1] => image/png
)
[tmp_name] => Array
(
[0] => /tmp/phpq62nsC
[1] => /tmp/phpKTijeC
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 63722
[1] => 3254
)
)
)
我认为你不需要任何foreach
- 只需将你的数据库文件名放在一起,如上所示。我有一个建议:使用basename($_FILES['file_upload_int']['name'])
作为文件名。
这样您就不会覆盖以jgp
结尾的文件,即使它是png
。
出于安全原因,我还建议您使用PDO
代替mysql
函数和$_POST
代替$_REQUEST
(如果您尚未发送每个帖子的表单)