Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question How can i create new file in php?

    I am using the following script to create a new file:

    <?
    fopen(myfile.php, "w+");
    ?>

    But, i am getting this msg:

    Warning: fopen(myfile.php) [function.fopen]: failed to open stream: No such file or directory in /home/aaskacla/public_html/Aaskablogs/Createblog/createblogscript.php on line 6

    Please help me. Where is the problem?

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Posts
    271
    Thanks
    3
    Thanked 40 Times in 40 Posts
    PHP Code:
    <?php
    $file 
    fopen("myfile.php","w+");
    echo 
    fputs($file,"Hello World. Testing!");
    fclose($file);
    ?>
    If you can't stand behind your troops, feel free to stand in front of them
    Semper Fidelis

  • #3
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    As jimhill wrote, add quotes to filename ("myfile.php" instead of myfile.php). If this fails, try changing permissions to directory.

    You don't have write permissions to directory "/home/aaskacla/public_html/Aaskablogs/Createblog/". You can change permissions through ftp client or by executing "chmod 777 /home/aaskacla/public_html/Aaskablogs/Createblog/" if you have shell access.

  • #4
    New Coder
    Join Date
    Sep 2011
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Exclamation

    Hey Jimhill, I tried your code:

    <?php
    $file = fopen("myfile.php","w+");
    echo fputs($file,"Hello World. Testing!");
    fclose($file);
    ?>

    But it gives me the following error:

    Warning: fopen(myfile.php) [function.fopen]: failed to open stream: Permission denied in /home/aaskacla/public_html/Aaskablogs/Createblog/createblogscript.php on line 7

    Warning: fputs(): supplied argument is not a valid stream resource in /home/aaskacla/public_html/Aaskablogs/Createblog/createblogscript.php on line 8

    Warning: fclose(): supplied argument is not a valid stream resource in /home/aaskacla/public_html/Aaskablogs/Createblog/createblogscript.php on line 9

    ------------------------------------------------------------------------------------

    Hello gvre, please tell me if my file will be secure after doing chmod 777 to it.

    How can i use chmod with fopen to create a new file?
    Last edited by sunnynosid; 09-20-2011 at 04:49 AM. Reason: forgotten something

  • #5

  • #6
    New Coder
    Join Date
    Sep 2011
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Please provide me a complete code to create a file. Keep in mind that i am facing the above mentioned problems.

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    You need to chmod the directory where you are writing in to 777 (or 666 if he only needs writing rights)
    then you wont face this problem

  • #8

  • #9
    New Coder
    Join Date
    Sep 2011
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks wanna. But how can i chmod my directory? I tried the following:

    <?php
    chmod("/Askablogs/Myaccount/", "777");
    $file = fopen("/Aaskablogs/Myaccount/myfile.php","w+");
    fclose($file);
    ?>

    But i got the following error:

    Warning: chmod() [function.chmod]: No such file or directory in /home/aaskacla/public_html/Aaskablogs/Createblog/createblogscript.php on line 16

    Warning: fopen(/Aaskablogs/Myaccount/myfile.php) [function.fopen]: failed to open stream: No such file or directory in /home/aaskacla/public_html/Aaskablogs/Createblog/createblogscript.php on line 17

    Warning: fclose(): supplied argument is not a valid stream resource in /home/aaskacla/public_html/Aaskablogs/Createblog/createblogscript.php on line 18

    That's why i am saying to provide me complete code as my code is having errors. So, please provide me complete code else i will be unable to understand.

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by sunnynosid View Post
    That's why i am saying to provide me complete code as my code is having errors. So, please provide me complete code else i will be unable to understand.
    Code was not provided because generally you use FTP/cPanel/etc. to CHMOD something as opposed to doing it in code because it's more difficult to use code, as you may have noticed.

  • #11
    New Coder
    Join Date
    Sep 2011
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I tried even this:

    I went to my cp, then to file manager, then opened Aaskablogs, then right clicked on Myaccount and set the permissions to 777 manually and then tried to create file with the previously provided code but got the same error(Warning: fopen(/Aaskablogs/Myaccount/myfile.php) [function.fopen]: failed to open stream: No such file or directory in /home/aaskacla/public_html/Aaskablogs/Createblog/createblogscript.php on line 17
    ).

    I am very confused now.

  • #12
    New Coder
    Join Date
    Sep 2011
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Huh! Problem solved. At last i found the solution. Anyways, thanks all.

  • #13
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Could you please say your solution here? So other people with the same problem can also see your solution.

  • #14
    New Coder
    Join Date
    Sep 2011
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Smile

    Solution:

    1) When using fopen, the permissions to the folder, in which you want to create your file, should be set to 777.

    2) You should give the full file pathname in fopen command i.e. home/..../public_html/yourfolder/yourfile

    3) And the code is:
    <?php
    fopen("yourfullfilepath", "w+");
    ?>

    These three points are enough to create a file with fopen.

  • #15
    Banned
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for sharing these ideas.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •