ramyakumar Posted November 7, 2009 Share Posted November 7, 2009 I am creating a form and storing the values in file. Could anyone please tell me how to write a code in php for creating a filename based on the user name as in the form. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/180706-filename-like-the-user-name/ Share on other sites More sharing options...
.josh Posted November 7, 2009 Share Posted November 7, 2009 fwrite or file_put_contents Quote Link to comment https://forums.phpfreaks.com/topic/180706-filename-like-the-user-name/#findComment-953387 Share on other sites More sharing options...
ramyakumar Posted November 8, 2009 Author Share Posted November 8, 2009 I tried this method with fwrite or file_put_contents but that too did not give the required result. Quote Link to comment https://forums.phpfreaks.com/topic/180706-filename-like-the-user-name/#findComment-953420 Share on other sites More sharing options...
mrMarcus Posted November 8, 2009 Share Posted November 8, 2009 to simply create the filename, something like this: $filename = $_POST['username']; now, if you're looking to create the file, name the file, access the file, etc., you need to be more specific with your questions. otherwise, hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/180706-filename-like-the-user-name/#findComment-953427 Share on other sites More sharing options...
ramyakumar Posted November 8, 2009 Author Share Posted November 8, 2009 Thanks for the reply but it did not work for me. I am creating the file name to store the form values using the name provided by the user. Suppose if there is a user with name "Peter" then I want the file name created in his name like "Peter.txt". I hope this should help you people to help me with the code. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/180706-filename-like-the-user-name/#findComment-953447 Share on other sites More sharing options...
mikesta707 Posted November 8, 2009 Share Posted November 8, 2009 Maybe if you post your code we can help a little better? Quote Link to comment https://forums.phpfreaks.com/topic/180706-filename-like-the-user-name/#findComment-953455 Share on other sites More sharing options...
ramyakumar Posted November 8, 2009 Author Share Posted November 8, 2009 Thanks. It worked by using the code $file=$name; But I want the filename in a particular directory like PHP/Peter.txt. This is not happening even though I set the permissions of the file and the directory. It displays an error "fopen cannot open directory: Is a directory /PHP/Peter.txt ". Could anyone please help me with this. Quote Link to comment https://forums.phpfreaks.com/topic/180706-filename-like-the-user-name/#findComment-953546 Share on other sites More sharing options...
blueman378 Posted November 8, 2009 Share Posted November 8, 2009 what os are you using for your server? also, i may be wrong but i believe you need to set a flag for creating the file if it does not exist. try "w" eg <?php $handle = fopen("/PHP/" . $file , "w"); ?> or change the flag to just "r+" if you dont want to overwrite it, eg simply append it. Quote Link to comment https://forums.phpfreaks.com/topic/180706-filename-like-the-user-name/#findComment-953634 Share on other sites More sharing options...
ramyakumar Posted November 8, 2009 Author Share Posted November 8, 2009 I am using putty server with Windows XP as my os I am already using flag, a to create and append to the file. <?php $handle=fopen("/PHP/" . $file, "a"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/180706-filename-like-the-user-name/#findComment-953742 Share on other sites More sharing options...
mikesta707 Posted November 8, 2009 Share Posted November 8, 2009 firstly, you should put quotes around the flag. Not required, but that is probably drawing a Notice message. secondly, look at what the error says "fopen cannot open directory" what do you think that means? you will need to make that directory before you add files to it. are you sure that directory exists? Quote Link to comment https://forums.phpfreaks.com/topic/180706-filename-like-the-user-name/#findComment-953746 Share on other sites More sharing options...
ramyakumar Posted November 9, 2009 Author Share Posted November 9, 2009 Yes I have already given "a" in the fopen. I am very sure the directory is created and even the permissions to access it are set but still it is giving me warning but still it wont allow it to create too. Warning: fopen(PHP): failed to open stream: Is a directory in /home/ram/public_htmlPHP/formfile.php on line 24 Warning: fclose(): supplied argument is not a valid stream resource in /home/ram/public-html/PHP/formfile.php on line 34 When I used only fopen function with the flags set to it, it did not allow me to store the contents in the file too. I had to use getFileName($path) and then it worked but now it is not allowing me to store into the directory too whatever I do. Quote Link to comment https://forums.phpfreaks.com/topic/180706-filename-like-the-user-name/#findComment-954053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.