add comment to php files

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!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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
linuxroxAuthor Commented:
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.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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);


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

linuxroxAuthor Commented:
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!
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));

Guy Hengel [angelIII / a3]Billing EngineerCommented:
Roonaan, would that not put the comment before ANY <?php opening tag?
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));

linuxroxAuthor Commented:
thx guys.  I had forgotten about this question! :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.