简体   繁体   English

不检查现有文件? 的PHP

[英]Not checking for the existing file? PHP

I'm trying to make a user account system using text documents. 我正在尝试使用文本文档制作用户帐户系统。 But the issue i'm having with my code is that it just overwrites the document rather than testing if it exists first then if it doesn't create the document. 但是我的代码存在一个问题,那就是它只会覆盖文档,而不是先测试文档是否存在,然后再测试它是否不会创建文档。 Any ideas why this is? 任何想法为什么会这样?

<?php
    if (isset($_GET['NewUsername'])){
        if (isset($_GET['NewPassword'])){           
            $NewPassword = $_GET['NewPassword'];
            $NewUsername = $_GET['NewUsername'];
            if (file_exists("USERS/".$NewUsername)) {
                echo 'Error: User Already Exists!';
            }
            else{
                $myFile=fopen("USERS/".$NewUsername.".txt","w") or exit("Can’t open file!");
                fwrite($myFile, $NewPassword);
                fclose($myFile);
                echo 'Account Created!';
            }
        }
    }   
?>

You are writing to a file with a .txt extension - but you are using file_exists to test for a file without the extension.... 您正在写入扩展名为.txt的文件-但您正在使用file_exists测试不含扩展名的文件...。

So, change 所以,改变

if (file_exists("USERS/".$NewUsername)) {

to: 至:

if (file_exists("USERS/".$NewUsername ."txt")) {

You are not using the correct file name to check if file already exists. 您没有使用正确的文件名来检查文件是否已经存在。 Try changing the code with the one below. 尝试使用以下代码更改代码。

if (file_exists("USERS/".$NewUsername.".txt")) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM