add comment to php files

Posted on 2006-03-30
Last Modified: 2006-11-18
Hello!  I have a project i'm working on that i have failed to include comments at the top of all of my php files *rolleyes*
anyway, I need to create a script that reads all php files in a certain directory and recurses directories; and simply write a comment lets say in the form of /* my comment */ at the first line.  obviously i would rather not have the entire comment on one line.  rather i would like to have the comment taking up lets say, the first 100 characters per line then go to the  
/*  my comment that starts here
* the rest of my comment
i know this may sound trivial and it was my mistake in forgetting to do this but i'm lazy and there are too many files to edit  :)
anyone have a solution for this?
thank you very much in advance!
Question by:linuxrox
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    >at the top of all of my php files
    the comments with /* etc  */ need to be inside the <?php  tag ...  ?>
    the /* can indeed span several lines
    to comment only 1 line (ie the rest of it), use the following: // comment

    to comment outside the <?php ?> tags, you have to use the html comment tags:
    <!--  comment  -->
    this also works like the /* */ over multiple lines

    Author Comment

    angelIII:  :)  you don't seem to understand....  I'm very aware of the comment structure of php :)
    what i'm asking for is a way to open files using fopen to open all php files and insert a comment of my choosing at line 1 of all php files in recursive directories :)
    also i fully understand html comments :)
    -------thank you for the input but i'm not sure you fully understood what i'm asking for here.
    LVL 142

    Accepted Solution

    sorry, indeed i didn't read as from line 2...

    so the q is:
    how to modify all (.php) files in a folder (+subfolders), by adding some comment lines in the front of it...

    I would start with something like this:

    now, per file, I would copy the file to another extention as safe backup, then rewrite the original file with the comment lines + copying the original file contents:

    $fin = fopen($copy_file, "r+");
    $fout = fopen($original_file,"a");

    fwrite($fout,"write your comment first");

    // now, copy the rest of the file
    $data = fgets($fin,$maxread);
    while ((ftell($fin)<$maxread)) {
        $data = fgets($fin,$maxread);


    Author Comment

    thx angelIII:
    i'm getting ready to head to sleep but let me look at this first thing in the morning!  thanks for the response!
    have a great night.
    thanks again!
    LVL 49

    Expert Comment

    I agree with angelIII on the backup part, but for the replacement I would go with:

    $comment = "\n/* Comment line 1\n * Comment line 2\n * Comment Line 3\n */";

    $input = file_get_contents($copy_file);
    $fout  = fopen($original_file, 'w');
    fwrite($fout, str_replace('<'.'?php', '<'.'?php'.$comment, $input));

    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    Roonaan, would that not put the comment before ANY <?php opening tag?
    LVL 49

    Assisted Solution

    Yeah, I had to add an additional ,1 into the statement. But it places the comment after the <?php:

    fwrite($fout, str_replace('<'.'?php', '<'.'?php'.$comment, $input, 1));


    Author Comment

    thx guys.  I had forgotten about this question! :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    php simulator 5 56
    Simple Mosaic Web Library 8 50
    What does this mean? 6 24
    Make my code go to a certain link and input the date 13 19
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now