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 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    45
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Problem Saving file to a Directory

    This works
    PHP Code:

    if (!file_exists("Filestore/$filename.txt")){

    $fp fopen("Filestore/$filename.txt""w") or die("Can't open file"); 
    This does not work
    PHP Code:

    $year 
    date("y");
     
    if (!
    file_exists("Filestore/$year/$filename.txt")){

    $fp fopen("Filestore/$year/$filename.txt""w") or die("Can't open file"); 
    I have a form and when posted its contents are saved to a text file in Filestore directory.
    When I modified it to save it to a directory based on what year it is it does not work. Any ideas would be apreciated

  • #2
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    Try this
    PHP Code:
    $year date("y"); 
      
    if (!
    file_exists("Filestore/".$year."/".$filename.".txt")){ 

    $fp fopen("Filestore/".$year."/".$filename.".txt""w") or die("Can't open file or create file"); 
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    45
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Does not work getting this message

    Can't open file or create file

    With my first atempt i got the message

    Can't open file

    Edit My bad just seen the or die statement
    Last edited by trazix; 08-22-2009 at 06:10 PM.

  • #4
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    ok you need to check if the year folder exist if not create it and chmod 777

    if it dose chmod it to 777 so php can write to the directory

    one min and i will show code to check for it and create it if it dose not exist

    PHP Code:
    $year date("y"); 
      
    if (!
    file_exists("Filestore/".$year)){
            
    mkdir("Filestore/".$year0777);  
        } else{
             if(!
    is_writable("Filestore/".$year)) chmod("Filestore/".$year0777);
        }

    if (!
    file_exists("Filestore/$year/$filename.txt")){ 

    $fp fopen("Filestore/$year/$filename.txt""w") or die("Can't Create file"); 
    Last edited by barkermn01; 08-22-2009 at 06:18 PM.
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • #5
    New Coder
    Join Date
    Jul 2009
    Posts
    45
    Thanks
    11
    Thanked 0 Times in 0 Posts
    The folder 2009 does exist. Checked the permissions and they are all ticked as being on
    Permissions
    u[ rwx ] g[ rwx ]

  • #6
    New Coder
    Join Date
    Jul 2009
    Posts
    45
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Sorry barkermn01 that dont work for me. It saves ok to Filestore directory I cant see why it will not save into 2009 directory

  • #7
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    Quote Originally Posted by trazix View Post
    Sorry barkermn01 that dont work for me. It saves ok to Filestore directory I cant see why it will not save into 2009 directory
    yea that's weird as all i know php need to save a file is directory needs to be chmod 0777, All permissions, and the file exists, dose then file already exist in the directory??

    Hrm i gonna write a script and try some thing for you
    PHP Code:
    $year date("y");
    $filename time();
      
    if (!
    file_exists("Filestore/".$year)){
            
    mkdir("Filestore/".$year0777);  
        } else{
             if(!
    is_writable("Filestore/".$year)) chmod("Filestore/".$year0777);
        }

    if (!
    file_exists("Filestore/$year/$filename.txt")){ 
        
    $fp fopen("Filestore/$year/$filename.txt""w") or die("Can't Create file"); 
    }else{
        
    unlink("Filestore/$year/$filename.txt");
        
    $fp fopen("Filestore/$year/$filename.txt""w") or die("Can't Create file");

    Last edited by barkermn01; 08-22-2009 at 06:26 PM.
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • Users who have thanked barkermn01 for this post:

    trazix (08-22-2009)

  • #8
    New Coder
    Join Date
    Jul 2009
    Posts
    45
    Thanks
    11
    Thanked 0 Times in 0 Posts
    That created a directory 09 but did not save a file also I got no or die message this time

  • #9
    New Coder
    Join Date
    Jul 2009
    Posts
    45
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Problem solved it was the y should have been Y The origanal code works with it , I must state that I am a beginner at this PHP and must also thank barkermn01 for your time.

  • #10
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    Quote Originally Posted by trazix View Post
    Problem solved it was the y should have been Y The origanal code works with it , I must state that I am a beginner at this PHP and must also thank barkermn01 for your time.
    No Problem
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)


  •  

    Posting Permissions

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