...

View Full Version : How can i create new file in php?



sunnynosid
09-19-2011, 04:52 PM
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?

jimhill
09-19-2011, 06:21 PM
<?php
$file = fopen("myfile.php","w+");
echo fputs($file,"Hello World. Testing!");
fclose($file);
?>

gvre
09-19-2011, 06:37 PM
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.

sunnynosid
09-20-2011, 03:14 AM
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?

gvre
09-20-2011, 11:03 AM
Webserver's user needs write access to directory, not the file. Check my previous answer for details.

sunnynosid
09-20-2011, 02:26 PM
Please provide me a complete code to create a file. Keep in mind that i am facing the above mentioned problems.

Wanna
09-20-2011, 02:29 PM
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

gvre
09-20-2011, 02:43 PM
The web server must have execute permission on a directory to switch (cd) to it, so 666 won't work.

sunnynosid
09-20-2011, 03:32 PM
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.

Inigoesdr
09-20-2011, 03:38 PM
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.

sunnynosid
09-21-2011, 04:37 AM
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.

sunnynosid
09-21-2011, 05:13 AM
Huh! Problem solved. At last i found the solution. Anyways, thanks all.:thumbsup:

Wanna
09-21-2011, 08:22 AM
Could you please say your solution here? So other people with the same problem can also see your solution.

sunnynosid
09-21-2011, 12:06 PM
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.

3magic
09-21-2011, 12:14 PM
Thanks for sharing these ideas.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum