Home > Blockchain >  Expects parameter 1 to be resource error for Fopen and fwrite
Expects parameter 1 to be resource error for Fopen and fwrite

Time:12-16

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.

CodePudding user response:

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

CodePudding user response:

You may have forgotten a slash / after

$fp = fopen($_POST["name"]."
  •  Tags:  
  • php
  • Related