【问题标题】:Quit script if directory already exists如果目录已存在则退出脚本
【发布时间】:2022-02-09 05:34:51
【问题描述】:

我知道这听起来像是一个愚蠢的问题,但我无法理解这个问题。基本上我只想在目录中的文件夹已经存在时才退出脚本,但我得到的只是带有 echo "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.");
           
        }
    }                                       
    ?>  
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset="UTF-8">
            <title>Import Immagini</title>
            <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700;900&display=swap" rel="stylesheet">
            <link rel="stylesheet" href="style_5.css">    
        </head>
        <body>
        <header>
            <div class="wrapper">
                <div class="logo">
                    <img src="biotech.png" alt="">
                </div>
        <ul class="nav-area">
        <li><a href="dashboard.php">Home</a></li>
        <li><a href="table/index.html">Contatti</a></li>
        <li><a href="logout.php">Logout</a></li>
        
        </ul>
        </div>
                <div class="inserimento">
                <h1> Inserisci flaconi da caricare </h1>
                <form action="" method="POST" enctype="multipart/form-data">
                    
                    
                    <input type = "file" name= "image[]"  id="chooseFile" multiple><br>
                    <label> Inserisci numero batch: </label><input type='text' name="batch_number" class='form-control' required placeholder="inserisci batch number"/><br>
                    <label> Inserisci prodotto: </label><input type='text' name="product_name" class='form-control' required placeholder="inserisci nome prodotto "/><br>
                    <label> Inserisci grandezza flacone: </label><input type='text' name="vial_size" class='form-control' required placeholder="inserisci formato flacone"/><br>
                    
                                         
                   
                   
                    
                    <select class='form-control' name="Sterile">

                        <option value="" >--Select--</option>
                        <option value="Sterile Area 1" >Sterile Area 1</option>
                        <option value="Sterile Area 2" >Sterile Area 2</option>
                        <option value="Sterile Area 3" >Sterile Area 3</option>
                        <option value="Sterile Area 4" >Sterile Area 4</option>
                        <option value="Sterile Area 5" >Sterile Area 5</option>
                        <option value="Sterile Area 6" >Sterile Area 6</option>
                        <option value="PDS" >PDS</option>
                    </select><br>

                    <select class="form-control" name="Macchina" autofocus="autofocus" required>
                   
                        <option value="" >--Select--</option>
                        <option value="Sedeneider" >Sedeneider</option>
                        <option value="Groheninger" >Groheninger</option>
                        <option value="Optrel400" >Optrel400</option>
                    </select>
                    <input type="submit" name="upload" value="Upload Image/Data"><br>
                </div>
                
                </form>
                     
                <?php if(!empty($response)) {?>
                <div class="alert <?php echo $response["status"]; ?>">
                   <?php echo $response["message"]; ?>
                </div>
            <?php }?>            
        </body>     
        
        </html>

【问题讨论】:

标签: php html


【解决方案1】:

您似乎需要为其他代码(您尚未向我们展示)设置 $response 数组以显示消息。一种方法是将您向我们展示的代码包装在 do { ... } while (false) 块中(其中“...”是您在上面显示的所有代码),并在您当前调用 exit() 的位置,将其更改为:

$response = array(
  "status" => "alert-danger",
  "message" => "la cartella esiste"
);
break;

break 的调用将退出循环并继续执行根据$response 的内容绘制页面的其余代码。

这是您的代码,已按说明进行了修改:

<?php
require('db.php');

do { // Begin one-time loop

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)) {
        $response = array(
            "status" => "alert-danger",
            "message" => "la cartella esiste"
        );
        break;
    } 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."
        );
    }
}
} while (false); // Loop will only execute once
?>  

【讨论】:

  • 我已经按照您的建议进行了尝试,但我得到“致命错误:'break' 不在 'loop' 或 'switch' 上下文中”。我想这是因为我的 php 版本。我贴了整个代码,你给其他代码设置$response数组是什么意思?
  • 如果你得到那个错误,那么你还没有像我展示的那样把你所有的代码放在一个do { ... } while (false)循环中。它与您的 PHP 版本无关。至于设置$response,看起来需要这样做,以便$response中的信息可以通过您没有显示给我们的其他代码显示在页面上。
猜你喜欢
  • 2020-05-31
  • 2015-02-28
  • 2021-11-15
  • 2014-05-19
  • 1970-01-01
  • 2013-11-25
  • 1970-01-01
  • 2014-01-08
  • 2016-06-23
相关资源
最近更新 更多