add comment to php files

Posted on 2006-03-30
Medium Priority
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 next..like:  
/*  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
  • 3
  • 3
  • 2
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16339621
>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

ID: 16339645
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 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 1600 total points
ID: 16339750
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);

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions


Author Comment

ID: 16339772
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

ID: 16340092
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 143

Expert Comment

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

Assisted Solution

Roonaan earned 400 total points
ID: 16340308
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

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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
I recently worked on a Wordpress site that utilized the popular ContactForm7 (https://contactform7.com/) plug-in that only sends an email and does not save data. The client wanted the data saved to a custom CRM database. This is my solution.
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…

624 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