简体   繁体   中英

Expects parameter 1 to be resource error for Fopen and fwrite

My code is perfectly creating the folder and file. But i want that, all the newly created folders must go in a parent folder. For now, it creates folders in the root directory. But i want that it should create folder and file in a sub folder like "docs/then new folders should come here with files in it".

I get some errors when i try to put folders and files in a sub folder, though it creates the directory but files aren't being created.

Here is the code

<?php  
    $dirname = $_POST["name"];  
    $filename = "/{$dirname}/";  
    
    if (file_exists($filename)) {  
        echo "The directory {$dirname} exists";  
    } else {  
        mkdir("User Folders/{$dirname}/", 0777, true);  
    $content = "Name:".$_POST["name"]." Email:".$_POST["email"]; 
    $fp = fopen($_POST["name"]."User Folders/Customer Details.txt","wb"); 
    fwrite($fp,$content); 
    fclose($fp); 
        echo "The directory {$dirname} was successfully created.";  
    }  
?>

Here is the error

Warning: fopen(TeamthunderUser Folders/Customer Details.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\fj\badmin\CreateFolder.php on line 10

Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\fj\badmin\CreateFolder.php on line 11

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\fj\badmin\CreateFolder.php on line 12 The directory Teamthunder was successfully created.

The directory you created is within User Folders . But you're putting $_POST['name'] before User Folders . It should be:

$fp = fopen("User Folders/$dirname/Customer Details.txt","wb"); 

You may have forgotten a slash / after

$fp = fopen($_POST["name"]."

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