簡體   English   中英

我如何在 VScode 中使用 PHP 代碼創建 .txt 文件

[英]How do i create .txt file with PHP code in VScode

我剛剛開始學習 PHP,並從 VS Code 開始,但不幸的是,當我嘗試創建一個新的簡單的“data.txt”文件時,但沒有任何運氣。 這是代碼

<?php
mb_internal_encoding('UTF-8');
echo '<pre>' . print_r($_POST, true) . '</pre>';
$pageTitle = 'Форма';
include 'header.php';
$groups = [1 => 'Приятели', 2 => 'Бивши', 3 => 'Бъдещи', 4 => 'Колеги'];
$error = true;

if ($_POST) {
    $username = trim($_POST['username']);
    $phone = trim($_POST['phone']);
    $selectedGroup = (int) $_POST['group'];

    if (mb_strlen($username) < 4) {
        echo '<p>Името е прекалено късо</p>';
        $error = true;
    }
    if (mb_strlen($phone) < 6 || mb_strlen($phone) > 12) {
        echo '<p>Невалиден Телефон</p>';
        $error = true;
    }
    if (!array_key_exists($selectedGroup, $groups)) {
        echo '<p>Невалидна Група</p>';
        $error = true;
    }

    if (!$error) {
        $result = $username . '!' . $phone . '!' . $selectedGroup;
        file_put_contents('data.txt', $result);
    }
}
?> 

<a href="index.php">Списък</a>
<form method="POST">
    <div> Име: <input type="text" name="username" /> </div>
    <div>Телефон <input type="text" name="phone" /> </div>
    <div> 
        <select name="group">
            <?php foreach ($groups as $key => $values) {
                echo '<option value="' . $key . '">' . $values . '</options>';
            } ?>
        </select>
    </div>
    <div><input type="submit" name="Добави" /> </div>
</form>
<?php include 'footer.php';
?>

每次我重新加載服務器時,它都不會創建任何新的“.txt”文件,即使我嘗試使用不同的值也無法創建。 enter code her enter code here e我查找了不同的信息,我嘗試將權限更改為 -- 755 -- 'Path' ==>> /var/www/html。 調試也是如此,我已經嘗試過但沒有顯示錯誤,假設這是我試圖查看代碼 100 次但沒有結果的一些小事情。

提前致謝。

不存在$error可能為 false 的條件(您將其初始化為 true 並僅將其設置為 true),因此您永遠無法進入if(!$error){塊。

你可能想改變那個初始

$error = true;

$error = false;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM