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 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Location
    Redmond Wa
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problem Creating File with fopen

    Hello,

    I am fairly new to PHP, so while I think I am on the right track, I probably am not. I am attempting to make a script that runs through a database and creates new files for each id using the values of the variables. The variables themselves set the file path and the name of the file. The ID is auto incrementing, and I know the values are in the database.

    I will breifly explain my project. Im using the Google Maps API, and I populate my map with markers that get their data from a database. Each marker on the map is populated by the following javascript code:
    Code:
    var html = '<b>' + name + '</b> <br/>' + address + '<br />' + '<a href=\"/locations/' + state + '/' + city + '/' + name + '\">View Profile</a>'
    As you can see a URL path is set by the variables. I now need to run through the same database to actually create those files the markers are linking to.

    My code so far is the following
    PHP Code:
    <?php  


    $contents 
    'Testing to see if this makes it onto the page'


    include (
    'mysql_connect.php');
    $query "SELECT id, name, address, city, state, as sd FROM markers";
    $result = @mysql_query($query);

    if (
    $result) {
    while (
    $row mysql_fetch_array($resultMYSQL_ASSOC)) {

    $handle fopen($_SERVER['DOCUMENT_ROOT'].'/locations/'.$state.'/'.$city.'/'.$name.'.php'"w");  

    $lock flock($handleLOCK_EX);  

    if (
    $lock){ 
     
    fputs($handle$contents);  
     
    flock($handleLOCK_UN);  
    }  
    fclose($handle); 
    }


    ?>
    I worked through all the errors, and now I get a blank page when I run it so I would assume it should be working. However, no files are created in the locations.

    Any ideas on what I am doing wrong? And am I on the right path towards what I am working towards?

    Thanks

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    add this:
    PHP Code:
    ini_set('display_errors',1);
    error_reporting(E_ALL); 
    to the top of the file. This will probably show what's wrong in case you still have errors.
    I guess the problem is that you don't have write permission to that location.

    Edit: other thing, you can miss one or more directory on that path

    best regards
    Last edited by oesxyl; 03-26-2009 at 04:30 AM.

  • #3
    New to the CF scene
    Join Date
    Mar 2009
    Location
    Redmond Wa
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the response oesxyl.

    I added that code, and still get a blank page, so I am fairly sure now that the function itself works, but my connection to MySQL or my variable path is the problem.

    By removing the MySQL and the variables from the database, I got a code that successfully created a new file and added the value of $contents.

    PHP Code:
    <?php  

    $contents 
    'Testing to see if this makes it onto the page'

    $handle fopen(test.'.'.txt"w");  

    $lock flock($handleLOCK_EX);  

    if (
    $lock){ 
     
    fputs($handle$contents);  
     
    flock($handleLOCK_UN);  
    }  
    fclose($handle); 

    ?>
    I think at this point I will rebuild the code step by step and see if I can find the problem now that I have somewhere to start.

    If anyone has any ideas still, they are greatly apreciated.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Your file path is incorrect. The variables you're referencing don't exist in the code you posted. You need to use $row['state'], $row['city'], $row['name'] instead of $state, $city, $name. This is because the current result from your query is stored in an array called $row in this case. By the by, you can use mysql_fetch_assoc($result) vs. mysql_fetch_array($result, MYSQL_ASSOC).

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Pixel-Picasso View Post
    Thanks for the response oesxyl.

    I added that code, and still get a blank page, so I am fairly sure now that the function itself works, but my connection to MySQL or my variable path is the problem.

    By removing the MySQL and the variables from the database, I got a code that successfully created a new file and added the value of $contents.

    PHP Code:
    <?php  

    $contents 
    'Testing to see if this makes it onto the page'

    $handle fopen(test.'.'.txt"w");  

    $lock flock($handleLOCK_EX);  

    if (
    $lock){ 
     
    fputs($handle$contents);  
     
    flock($handleLOCK_UN);  
    }  
    fclose($handle); 

    ?>
    I think at this point I will rebuild the code step by step and see if I can find the problem now that I have somewhere to start.

    If anyone has any ideas still, they are greatly apreciated.
    Inigoesdr is right, I didn't see that,
    btw before writing to the file, you could echo the content to check if is what you expect.

    best regards

  • #6
    New to the CF scene
    Join Date
    Mar 2009
    Location
    Redmond Wa
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hmm I changed the code to include the $row but I still cannot get it to create the file path.

    If I remove the variables it works fine, so I guess it just is not getting the values of the variables stored in the database. Do you by chance see anything that looks wrong? Should I include .$row['id']. amywhere? Something just has to be off somewhere that relates to actually getting the values of the variables.

    My current code is:

    PHP Code:
    <?php  

    ini_set
    ('display_errors',1);
    error_reporting(E_ALL);  


    $contents 'Testing to see if this makes it onto the page'


    include (
    'mysql_connect.php');
    $query "SELECT id, name, state, city, as sd FROM markers";
    $result = @mysql_query($query);

    if (
    $result) {
    while (
    $row mysql_fetch_assoc($result)) {

    $handle fopen($_SERVER['DOCUMENT_ROOT'].'/locations/'.$row['state'].'/'.$row['city'].'/'.$row['name'].'.php'"w");  

    $lock flock($handleLOCK_EX);  

    if (
    $lock){ 
     
    fputs($handle$contents);  
     
    flock($handleLOCK_UN);  
    }  
    fclose($handle); 
    }


    ?>
    And thanks again for the help to everyone helping out, I really do apreciate it. I spent about 7 hours going through the archives and learned a ton of PHP here today.

    *edit*
    When I ran it without database variables for the path, my result was http://davasso.com/locations/test.php, so at least that part works.
    Last edited by Pixel-Picasso; 03-26-2009 at 06:21 AM. Reason: Forgot to mention something

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    $mydir $_SERVER['DOCUMENT_ROOT'].'/locations/';
    if(!empty(
    $row['state']) && !empty($row['city']) && !empty($row['name'])){
       
    $mydir .= $row['state'].'/'.$row['city'].'/';
       if(
    file_exists($mydir) && is_writable($mydir)){
          
    $mydir .= $row['name'].'.php';
          
    // put the rest of the code to write the file here
       
    }

    also you must add some checking and error message to see what's wrong if don't work.

    best regards

  • #8
    New to the CF scene
    Join Date
    Mar 2009
    Location
    Redmond Wa
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Added that code, unfortuanately same results so I think I will just have to toy around with it. It simply is not getting the values from the database, though it is connecting.

    As for the error reporting, what kind of error reporting should I use in this case? When I first created the code I had like 4 or 5 errors, and then fixed them and it quit telling me errors.

    Is there a simple method to see if my script is pulling the values?

    My current setup is
    PHP Code:
    <?php  

    ini_set
    ('display_errors',1);
    error_reporting(E_ALL);  

    $contents 'Testing to see if this makes it onto the page'

    include (
    'mysql_connect.php');
    $query "SELECT id, name, state, city, as sd FROM markers";
    $result = @mysql_query($query);

    if (
    $result) {
    while (
    $row mysql_fetch_assoc($result)) {

    $mydir $_SERVER['DOCUMENT_ROOT'].'/locations/';
    if(!empty(
    $row['state']) && !empty($row['city']) && !empty($row['name'])){
       
    $mydir .= $row['state'].'/'.$row['city'].'/';
       if(
    file_exists($mydir) && is_writable($mydir)){
          
    $mydir .= $row['name'].'.php';
          
    // put the rest of the code to write the file here

    $handle fopen($mydir.$row['state'].'/'.$row['city'].'/'.$row['name'].'.php',
    "w") or die("Could Not Open File");  
      

    $lock flock($handleLOCK_EX);  

    if (
    $lock){ 
     
    fputs($handle$contents);  
     
    flock($handleLOCK_UN);  
    }  
    fclose($handle); 
    }} 
    }}  

    ?>
    I added or die("Could Not Open File"); to see if it couldn't create the file, but apparently it thinks it is creating the file. I have never really had problems pulling simple data from a database before, usually seems to be fairly straight forward.

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    change what is commented:
    PHP Code:
    <?php  

    ini_set
    ('display_errors',1);
    error_reporting(E_ALL);  

    $contents 'Testing to see if this makes it onto the page'

    include (
    'mysql_connect.php');
    $query "SELECT id, name, state, city, as sd FROM markers";
    $result = @mysql_query($query);

    if (
    $result) {
    while (
    $row mysql_fetch_assoc($result)) {

    $mydir $_SERVER['DOCUMENT_ROOT'].'/locations/';
    if(!empty(
    $row['state']) && !empty($row['city']) && !empty($row['name'])){
       
    $mydir .= $row['state'].'/'.$row['city'].'/';
       if(
    file_exists($mydir) && is_writable($mydir)){
          
    $mydir .= $row['name'].'.php';
          
    // put the rest of the code to write the file here

    // next line is wrong because state, city and name is already in mydir
    // $handle = fopen($mydir.$row['state'].'/'.$row['city'].'/'.$row['name'].'.php', "w") or die("Could Not Open File"); 
    // must be:
    $handle fopen($mydir"w") or die("Could Not Open File");  
      

    $lock flock($handleLOCK_EX);  

    if (
    $lock){ 
     
    fputs($handle$contents);  
     
    flock($handleLOCK_UN);  
    }  
    fclose($handle); 
    }} 
    }}  

    ?>
    best regards

  • #10
    New to the CF scene
    Join Date
    Mar 2009
    Location
    Redmond Wa
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey thanks for the help oesxyl,

    Again no luck however, it simply absolutely refuses to pull those values from the database I guess. With all the different variations I have tried I think I'm going to have to throw in the towel on this one. I have tried finding references or examples that also use database values to create a path, however it seems that none exist that I can find.

    I was looking through the archives though, and came across http://www.codingforums.com/archive/.../t-107879.html. I dont exactly know how this works, however I kind of understand the concept. I couldnt find anymore topics that cover it, so I was wondering if anyone could possibly point me in the right direction to where I could learn a little more about the process and how it works. Or what keywords I should use to search for it.

    Thanks again.

  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Pixel-Picasso View Post
    Hey thanks for the help oesxyl,

    Again no luck however, it simply absolutely refuses to pull those values from the database I guess. With all the different variations I have tried I think I'm going to have to throw in the towel on this one. I have tried finding references or examples that also use database values to create a path, however it seems that none exist that I can find.

    I was looking through the archives though, and came across http://www.codingforums.com/archive/.../t-107879.html. I dont exactly know how this works, however I kind of understand the concept. I couldnt find anymore topics that cover it, so I was wondering if anyone could possibly point me in the right direction to where I could learn a little more about the process and how it works. Or what keywords I should use to search for it.

    Thanks again.
    try this:
    PHP Code:
    while ($row mysql_fetch_assoc($result)) {
        
    print_r($row); 
    check state, city and name to be not empty.

    best regards

  • #12
    New to the CF scene
    Join Date
    Mar 2009
    Location
    Redmond Wa
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah thanks, since the page is still blank and doesnt output the values, im guessing the problem lies somewhere in the MySQL?

    My code looks like
    PHP Code:
    <?php  

    ini_set
    ('display_errors',1);
    error_reporting(E_ALL);  

    $contents 'Testing to see if this makes it onto the page'

    include (
    'mysql_connect.php');
    $query "SELECT id, name, state, city, as sd FROM markers";
    $result = @mysql_query($query);

    if (
    $result) {

    while (
    $row mysql_fetch_assoc($result)) { 
        
    print_r($row);  
    }
    }
    ?>

  • #13
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Pixel-Picasso View Post
    Ah thanks, since the page is still blank and doesnt output the values, im guessing the problem lies somewhere in the MySQL?

    My code looks like
    PHP Code:
    <?php  

    ini_set
    ('display_errors',1);
    error_reporting(E_ALL);  

    $contents 'Testing to see if this makes it onto the page'

    include (
    'mysql_connect.php');
    $query "SELECT id, name, state, city, as sd FROM markers";
    $result = @mysql_query($query);

    if (
    $result) {

    while (
    $row mysql_fetch_assoc($result)) { 
        
    print_r($row);  
    }
    }
    ?>
    try this:
    PHP Code:
    if($result){
    // code....
    }else{
      print 
    '<pre>'.$query.'</pre>';
      print 
    '<pre>'.mysql_error().'</pre>';

    best regards


  •  

    Posting Permissions

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