How can I use PHP to create a new file?

Discussion in 'PHP' started by Imozeb, Apr 12, 2010.

  1. #1
    How would I make PHP get the contents of a .php file, populate some fields on the .php via the DOM, and then save the new file to a new location while leaving the old template file intact? I've read a bit about the DOM for PHP but it doesn't seem to be working for me.
     
    Imozeb, Apr 12, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    You would use http://php.net/manual/en/function.fopen.php to open the existing file... check associated functions like fread while you are there
    then regular expressions to replace the content
    then fwrite and fclose to create and close the copy.

    If the php file is really a template its good practice to give it a non-php file extension so that people can't inadvertently run it. Safer still to have the files below the root.
     
    sarahk, Apr 12, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I was thinking about using fopen but then I couldn't figure out an easy way to get the elements by ID becasue I don't know how to use regexps very well. Do you think you could give me an example of how use regexps to get an element by its id?

    Thanks.
     
    Imozeb, Apr 12, 2010 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #4
    me neither :) I just know that's what you'd need to do.

    If it's just templating you are trying to do why don't you use SMARTY or one of the other templating tools?
    If you don't want to use them then they'd still be worth a look at the source code to see how they work.
     
    sarahk, Apr 12, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well. Do you think that PHP's DOM commands would work or is that just for XML files?

    Mabye some regexps pro could help me create a regexps that would work like Javascripts document.getElementById('id');
     
    Imozeb, Apr 12, 2010 IP
  6. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #6
    What exactly will you be storing in the file? If it is just a data structure (array, object etc) then you can serialize the data the be stored in your external file.
    When you read this file back in, unserialize the data and you will have your data structure in tact which you can then output elsewhere.

    You could also look into storing it as XML or another interchangeable format.
     
    lukeg32, Apr 13, 2010 IP
  7. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No. I trying to create an entire PHP page that is populated with values and object from a database and elsewhere. The problem is that once I have create the file I have to save it. This is the reason I am trying to use PHP DOM to get the ID's so I can easily populate the fields.
     
    Imozeb, Apr 13, 2010 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #8
    Honestly... just use smarty and save the output. Why recreate the wheel?
     
    sarahk, Apr 13, 2010 IP
  9. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I must reinvent the wheel!!! :) But seriously, when you think about it, once you start using premade scripts like JQuery and Smarty who can really call you a programmer. Basicly, programmers who use programming libraries or premade scripts to generate their entire code, can't really call themselves programmers because they don't know how to solve programmers problems. Now a good portion of the programming world are people who use real programmers codes to make most of their script. Which is sad. I prefer, to go old school, and to generate all my code without using premade scripts. So do you think you could tell me if PHP DOM would work and if not could you answer this question? Do PHP variables have a max size limit or can I put any number of bytes in them?

    Thanks!
     
    Imozeb, Apr 13, 2010 IP
  10. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #10
    Its a whole other debate but in the real world most of us don't have clients with unlimited budgets so that we can research security, efficiency etc. Even on my own projects I have to weigh up the return on investment (the cost of my time) and decide what is the best approach.

    You have done well to find a client who is happy to let you start from scratch.
     
    sarahk, Apr 13, 2010 IP
  11. Warll

    Warll Peon

    Messages:
    122
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Unless your writing your site in Machine code you'd be a hypocrite to criticize code-reuse.

    You're wasting time and money if your rejecting perfectly good solutions just because you have a sadistic fetish.

    There are a billion and one unsolved problems that you are keeping yourself from solving by.... I could go on all day, but I think I shouldn't, that would stop me from solving some problems.
     
    Warll, Apr 13, 2010 IP
    sarahk likes this.
  12. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Yes. I suppose that time and money is of a concern but if people just piece together a site with pre-made scripts *without modifying them* and call themselves great programmers, I just find that wrong.
     
    Imozeb, Apr 13, 2010 IP
  13. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #13
    Agree with warll, I choose good and better solutions than wasting my time.
     
    guardian999, Apr 14, 2010 IP
  14. alberrambo

    alberrambo Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    fopen function use with the writing mode fopen('filename','w');
     
    alberrambo, Apr 14, 2010 IP