简体   繁体   中英

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....

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")) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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