您好,我有一个用于在数据库中插入多张图片的脚本,当我想删除现有文件并在每次进行更新时插入新文件但我不知道如何仅在输入是文件时进行删除,如果不只是跳过那部分。这是我的代码:
$size=$_FILES['files']['size'];
if($size > 1) {
$query = "Delete from Imagini_Hotel where ID_Hotel='$id_hotel'";
$result = mysql_query($query)
or die("query failed: " . mysql_error());
}
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 9097152){
$errors[]='File size must be less than 9 MB';
}
if($file_size <= 0)
{
}
else
{
$query="Insert into Imagini_Hotel (`ID_Hotel`,`Nume_Poza`,`Prioritate`) VALUES('$id_hotel','$file_name','$key'); ";
$desired_dir="../../../images/hotel/$id_hotel";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0777); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
mysql_query($query);
}else{
print_r($errors);
}
}}
if(empty($error)){
echo "Success";
}