Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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"); 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading