create file in another directory with php

后端 未结 5 1201
一生所求
一生所求 2021-02-01 21:51

My folder structure is like -

root
  admin
    create_page.php
  pages
    my_page1.php
    my_page2.php

I have code for creating a new php fi

相关标签:
5条回答
  • 2021-02-01 22:12

    Here's an example using the more simpler file_put_contents() wrapper for fopen,fwrite,fclose

    <?php 
    error_reporting(E_ALL);
    
    $pagename = 'my_page1';
    
    $newFileName = './pages/'.$pagename.".php";
    $newFileContent = '<?php echo "something..."; ?>';
    
    if (file_put_contents($newFileName, $newFileContent) !== false) {
        echo "File created (" . basename($newFileName) . ")";
    } else {
        echo "Cannot create file (" . basename($newFileName) . ")";
    }
    ?>
    
    0 讨论(0)
  • 2021-02-01 22:15

    Had the same Problem, File will not be created in a folder having different permission, make sure the permissions of the folders are same like other files.

    0 讨论(0)
  • 2021-02-01 22:31
    $ourFileHandle = fopen("../pages/" .$ourFileName, 'w') or die("can't open file");
    

    Make the above change to the third line and it will probably work; I tried it and it worked.

    0 讨论(0)
  • 2021-02-01 22:31

    Try this

    $pagename = 'your_filename';
    $newFileName = '../pages/'.$pagename.".php";
    
    0 讨论(0)
  • 2021-02-01 22:35

    have you deliberately missed out on concatinating $dir_path and $ourFileName;

    now

    1. is your directory/file writable?
    2. check for your current working directory
    3. is your error_reporting on ?
    0 讨论(0)
提交回复
热议问题