[英]php - upload script mkdir saying file already exists when same directory even though different filename
[英]Quit script if directory already exists
我知道这听起来像一个愚蠢的问题,但我无法解决这个问题。 基本上我只想在目录中的文件夹已经存在的情况下退出脚本,但我得到的只是带有回显“la cartella esiste”的空白页面,我猜这是因为退出功能。 我想要的是将消息显示在页面本身上,就像显示的其他错误一样,但不继续使用代码。 你们对如何进行有什么建议吗?
<?php
require('db.php');
if (isset($_POST['upload'])) {
$batch_number = stripslashes($_REQUEST['batch_number']);
$batch_number = mysqli_real_escape_string($con, $batch_number);
$product_name = stripslashes($_REQUEST['product_name']);
$product_name = mysqli_real_escape_string($con, $product_name);
$vial_size = stripslashes($_REQUEST['vial_size']);
$vial_size = mysqli_real_escape_string($con, $vial_size);
$Sterile = $_POST['Sterile'];
$Macchina = $_POST['Macchina'];
$location = "immagini/$batch_number/";
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
$allowTypes = array('jpg','png','jpeg', 'bmp');
$img_name = array_filter($_FILES['image']['name']);
$select = mysqli_query($con, "SELECT * FROM info_flaconi WHERE batch_number = '". $_REQUEST['batch_number']."'");
if(file_exists($location) && is_dir($location)) {
exit ("la cartella esiste");
} else{
mkdir("immagini/$batch_number/", 0777, true);
echo "Cartella creata.";
}
if(!empty($img_name)){
foreach($_FILES['image']['tmp_name'] as $key=>$val){
//file upload path
$fileName= $_FILES['image']['name'][$key];
$fileName_tmp= $_FILES['image']['tmp_name'][$key];
$targetPath = $location .$fileName;
$ext=strtolower(pathinfo($targetPath, PATHINFO_EXTENSION));
$uploadDate = date('Y-m-d H:i:s');
$uploadOk = 1;
// Check whether file type is valid
if(in_array($ext, $allowTypes)){
// Upload file to server
if(move_uploaded_file($fileName_tmp, $targetPath)){
$sqlVal = $fileName;
} else{
$response = array(
"status" => "alert-danger",
"message" => "File coud not be uploaded.");
}
}else{
$response = array(
"status" => "alert-danger",
"message" => "Only .jpg, .jpeg, .png and bmp file formats allowed.");
}
if(!empty($sqlVal)){
$query = "INSERT INTO `info_flaconi` (batch_number, product_name, vial_size, vial_image, uploaded_on, Sterile_Area, Macchina)
VALUES ('$batch_number', '$product_name', '$vial_size', '$sqlVal', '$uploadDate', '$Sterile', '$Macchina')";
$result = mysqli_query($con, $query);
if($result){
$response = array(
"status" => "alert-success",
"message" => "Immagini caricate correttamente.");
}else{
$response = array(
"status" => "alert-danger",
"message" => "Files coudn't be uploaded due to database error.");
}
}
}
}else {
// Error
$response = array(
"status" => "alert-danger",
"message" => "Per favore seleziona le immagini da caricare.");
}
}
?>
看起来您需要为其他代码(您尚未向我们展示)设置
$response<\/code>数组以显示消息。
一种方法是将您向我们展示的代码包装在
do { ... } while (false)<\/code>块中(其中“...”是您在上面显示的所有代码),并在您当前调用
exit()<\/code>的位置
exit()<\/code> ,将其更改为:
$response = array(
"status" => "alert-danger",
"message" => "la cartella esiste"
);
break;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.