我有下面的代码,它运行良好。它允许文件上传到数据库,同时移动到指定的文件夹。但是,当我只上传一个文件时,所有其他三个数据库列的值是插入此类型。 aw3er45tr56。没有文件扩展名。我需要一个善良的人帮助我解决这些问题。
if(!empty($DescAd)){
$fileData1 = pathinfo(basename($_FILES["AdImage1"]["name"]));
$fileData2 = pathinfo(basename($_FILES["AdImage2"]["name"]));
$fileData3 = pathinfo(basename($_FILES["AdImage3"]["name"]));
$fileData4 = pathinfo(basename($_FILES["AdImage4"]["name"]));
$fileName1 = uniqid() . '.' . $fileData1['extension'];
$fileName2 = uniqid() . '.' . $fileData2['extension'];
$fileName3 = uniqid() . '.' . $fileData3['extension'];
$fileName4 = uniqid() . '.' . $fileData4['extension'];
$target_path1 = 'BusinessAdsUploads/'. $fileName1;
$target_path2 = 'BusinessAdsUploads/'. $fileName2;
$target_path3 = 'BusinessAdsUploads/'. $fileName3;
$target_path4 = 'BusinessAdsUploads/'. $fileName4;
while(file_exists($target_path1,$target_path2,$target_path3,$target_path4))
{
$fileName1 = uniqid() . '.' . $fileData['extension'];
$fileName2 = uniqid() . '.' . $fileData['extension'];
$fileName3 = uniqid() . '.' . $fileData['extension'];
$fileName4 = uniqid() . '.' . $fileData['extension'];
$target_path1 = 'BusinessAdsUploads/'. $fileName1;
$target_path2 = 'BusinessAdsUploads/'. $fileName2;
$target_path3 = 'BusinessAdsUploads/'. $fileName3;
$target_path4 = 'BusinessAdsUploads/'. $fileName4;
}
move_uploaded_file($_FILES['AdImage1']['tmp_name'],$target_path1);
move_uploaded_file($_FILES['AdImage2']['tmp_name'],$target_path2);
move_uploaded_file($_FILES['AdImage3']['tmp_name'],$target_path3);
move_uploaded_file($_FILES['AdImage4']['tmp_name'],$target_path4);
$query="insert into `MyAds` values('','$bisnaId','$user_id','$Category','$subCategory','$fileName1','$fileName2','$fileName3','$fileName4','$ItemTitle','$DescAd','$AdPrice','Personal Business')";
if($query_run=mysql_query($query)){
header('location:PreviewBisnaAd.php');
}
}
答案 0 :(得分:0)
您要做的第一件事是消除文件2,3和4的冗余代码,并通过一次上传再次尝试您的代码。如果要上传多个,只需多次使用您的功能即可。
您获得没有扩展名的值的原因是因为当您尝试仅为一个需要4的函数上传一个文件时,其他3个为空。您的程序将uniqid()
添加到每个程序并将其插入到数据库中。