SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I was wondering if anyone could help me with the following problem. I'm trying to create 'cached' files which 'if they exist' will be loaded for a search. If they don't exist I want them to be created and filled with $Output. The the next time the exact same search is done they should load from the created 'cache' file.

    In the manual it says that the fopen(file, 'w') function should create the file if it does not exist - however it does not seem to be doing so.


    Here is the snippet of related code

    if (file_exists($cachefile))
    {
    $fp = fopen($cachefile, "r");
    fpassthru($fp);
    echo "this is a cache file from " . $cachefile;
    }
    else
    {
    //Heres script to create $Output


    $fp = fopen($cachefile, "w");
    fwrite($fp, $Output);
    fclose($fp);
    }


    These are the warnings I'm getting:

    Warning: fopen("searchcache/search/books.txt","w") - No such file or directory in /home2/chilli/chillisauce-www/s.php on line 226

    Warning: Supplied argument is not a valid File-Handle resource in /home2/chilli/chillisauce-www/s.php on line 227

    Warning: Supplied argument is not a valid File-Handle resource in /home2/chilli/chillisauce-www/s.php on line 228


    Can anyone see the problem and help me with a solution.

    Cheers in advance

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Who is your webserver running as? And who owns the folder where you are trying to create the file, who owns the script that is trying to create the file? Sounds like a permissions problem to me.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well,

    I'm unsure of what you mean by 'who is your webserver running as'.

    The script is one I created.

    "who owns the folder where you are trying to create the file": - well the folder is not actual created - I thought the fopen(file, "w") would create the appropriate folders and then file e.g

    For the given example

    searchcache/search/books.txt

    Folders searchcache and search do not exist neither does file.txt

    But I thought these would be automatically created if the did not exist.

    Is it that you can only create files within folders that already exist?

    If that is the case I suppose I could change the variable
    $cachefile to something like

    /searchcache/search_books.txt

    and then create the folder searchcache.

    The reason I wanted it to create the file within created folders was so that it would be far more manageable for say deleting 'uncommon' searches etc if the files got to large.

    Is there 'simple' way to do what I want to do???


  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will probably need to check to see the folder exists, if it doesn't you would need to run a mkdir("dirname") and then a chmod(0777, "dirname"); first that way you are creating the folders and making them writable before you try and create files in them. does that make sense?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I kind of get the idea, but I would be really grateful if you could show me an example of how to do it.

    I guess this bit of code is ok
    if (file_exists($cachefile))
    {
    $fp = fopen($cachefile, "r");
    fpassthru($fp);
    echo "this is a cache file from " . $cachefile;
    }


    But I would need to alter this part:

    else
    {
    //Heres script to create $Output


    $fp = fopen($cachefile, "w");
    fwrite($fp, $Output);
    fclose($fp);
    }


    Could you create both the folder and file in one step - or do you need to create the folders, and then write the file to the folders?


  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First you will need to specify the folders you want use

    $searchdir = "/usr/www/pathto/searchcache";

    if (file_exists($cachefile))
    {
    $fp = fopen($cachefile, "r");
    fpassthru($fp);
    echo "this is a cache file from " . $cachefile;
    }


    But I would need to alter this part:

    else
    {
    if (!is_dir($searchdir) {
    mkdir($searchdir);
    chmod($searchdir, "777");
    chdir($searchdir);
    }

    $fp = fopen($cachefile, "w");
    fwrite($fp, $Output);
    fclose($fp);
    }



    That shjould do the trick I don't have time to test it right now, but let me know and then we can go from there.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

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