Appending a header to all PHP files - php

I am editing PHP files on my server. I am in the habit of including several comments at the top of each file containing licensing information, my web address, and the version number of the software. I'd like a more convenient way of maintaining this practice without having to manually deal with it when I open and update PHP files on my server. It would be nice if I could also append a timestamp of my recent modification at the bottom of the file too.
I am currently using Dreamweaver 8.0.2.
If Dreamweaver would append this header to the top (taking into account that the file may have a header already in place), that would be acceptable.
If the FTP server would append the file (assuming it is a PHP file) when I download or upload it, that may work too.
I'm unsure what approach to take or what would work for this.

The better option is use "Beyond Compare". It lets you to see file as Explorer and allows you to open multiple files and editing with saving.
I always uses that. So using that, you can open multiple files on server and save them. You don't have to wait for them that they are saving it on server. Just switch to another file(in new tab)
:)

#David: by whom*
Best solution is to include a header script that checks the last edit date for the time stamp and simply outputs the rest of the information.
All licensing information, web address, and version number of the software should be only changed once for all the files: Thus, echoing from php the comments or simply putting them on a html part would be enough.
As for the timestamp of recent modification, you could use the following script to get the last file update time:
$time = filemtime($filename); // You may use $_SERVER['PHP_SELF']
$convenient_date = date('d-m-Y', $time);
echo "Last modified on {$convenient_date}.";

Related

Is there a way to read the "actual file creation date" with PHP?

I am currently doing a project on website programming with PHP and mySQL. In general the project allows users to upload their files to the server and perform searches later according to the "file creation date". I use double quotation here since I want to extract the date when the file was created from nothing (not the file modified date).
I have been looking at a lot of programming forums and reference websites while most of them just introduced the use of filemtime() and filectime(). I have tried both of them and they only returned the "file modified time" (the time when the files were uploaded to the server by me in this development stage).
Knowing the actual file creation time is very important to me because I will use it to perform timeline search later as a requirement of the project. I have got stuck here. Really appreciate any kind help and suggestions.
I am using "xampp" for the web and database servers (the app has the configuration by default) and HTTP+PHP for the front end.
When a user uploads a file in a web browser, what actually happens is that three pieces of information are added to the form submission data:
A filename (which a browser will base on the name it had on the user's system)
A file type (which will be the browser's best guess, usually just based on the filename)
The binary contents of the file
Even if both the user's computer and your server have somewhere to record the creation date of the file, this won't be transmitted with the upload.
There's also something very important thing to bear in mind when building any web-based system: you only have the information the user gives you, and that information could be deliberately or accidentally wrong. If there was a timestamp, it might be wrong because the user's clock is wrong; or it might have been sent by a piece of software that let the user manually set it.

Least disruptive way to download file using PhP. Prevent disrupting of ongoing updating of it

I am working on a website in which it would be useful to to allow a user the option of downloading the content of a file, even when it's going to be updated by another user at the same time or later.
My problem is that the solution I've tried so far allows downloading, but will disrupt any later updating of the file. I don't think I can represent the code relating to the updating concisely (it is spread over multiple files), except that it's through AJAXing the data (which I'm not sure why it would cause this problem). In case it's relevant, this is a file which gets updated multiple times.
When I use fireftp I can download the file without disrupting this process, which makes me optimistic there's a PhP solution. I am currently downloading the data by Ajaxing the file contents to the page the "downloading user" is on. The code for this (within php) is:
$file_contents = file_get_contents ($_POST['file'])); //file address comes through Ajax POST request.
echo ($file_contents); //to access the content client side
Is there another way to access the text/content within a file without any unintended consequences on other server processing of it?

Overwriting files properly

I am trying to manage caching on heavily used webpage written in PHP. I have marked some cacheable sections of PHP code, which I want to execute only pre-cache when administrator make changes in CMS. For this, I use this method:
I have file (for example "index-source.php") with some marked ares of PHP code, which are interpretable alone. When admin change some settings, these marked parts are executed and replaced with result (for example MySQL queries which reads menu items from DB are replaced with generated HTML menu). Resulted file is saved as new "index.php", which still have some PHP code, which can't be optimized by caching.
Now to my problem
If we assume, that this server is heavilly load, which means there is for example 100 requests per second, which in PHP requires file index.php. If I will use file_put_contents() to overwrite this index.php with new pre-cached version, is there any risk, that some requests will be interrupted, because of locked/not fully overwritten file? Basically I want to somehow update my PHP file and assure that PHP will include complete old or complete new version of that file or wait few milliseconds until file is overwritten. I dont't want PHP to fail require or load partially overwritten file.
Is that possible? Thanks
file_put_contents is not what you want.
Have a look at this project, and dive into the source to get a feel for what challenges you may have to face as well as the solution chosen.
https://github.com/PHPSocialNetwork/phpfastcache

FTP: Can I overwrite a file and maintain it's modified date?

I have hundreds of mp3 files on my server. Each file's modified-date is important because it is fetched by PHPs filemtime to represent it's upload date (since there's no way to determine an upload time without storing values in a database).
I have come across an audio issue in which all the files need to be normalized and re-uploaded to the server. This would, of course, change the modified-date of each file to "today". I need each file to retain it's original modified-date.
I'm not sure if this is a software-recommendation question or a programming question, so I apologize if this is the wrong .SE site. Is this even possible?
You should be able to set the modified time with touch: http://php.net/manual/en/function.touch.php
This requires PHP > 5.3 and the user running the script (probably your web user unless you run it from the cli) needs to have write permission on the file.
You have two options for implementation:
Store the filenames and their mtimes in temporary storage (either a file or a database table). When you finish the upload, run through all of the files and use touch to reset the mtime.
As you upload the files, check to see if the file already exists. If it does, grab the mtime in a temporary variable, overwrite the file, then touch it with the correct mtime.
I know this isn't the answer you're looking for, but it would make far more sense to start storing this information in a database than relying on the last-modified date. This way you can show your users the date that they need to know and retain the true date of modification.
An approach like this also gives you much more flexibility.
As requested by #Snailer - for the sake of closing the question.

Preventing the browser caching a linked file

I've been tasked to maintain a PHP website with a function which automatically generates RTF files and provides a link to download. Each time the previously generated file is overwritten by the new one.
However, it seems that upon attempting to download the generated file, the browser will sometimes retrieve a cached version which is normally different from the latest version (so you get the same document as last time rather than the one you requested).
I managed to work around this by giving each generated file a unique name based on the current timestamp but this generates lots of clutter in the directory which will need to be cleaned out periodically. Ideally I would like to tag this file such that the browser won't cache it and will get the latest version every time. How might I achieve this?
In addition to the possibility of adding a random GET string to the URL (often the easiest way), it would also be possible to solve this by sending the right headers.
Because you are generating static files, this would require a setting in a .htaccess file. It would have to look like this:
<FilesMatch "\.(rtf)$">
Header set Cache-Control "no-store"
</FilesMatch>
Easiest way? Instead of linking to http://yoursite.com/file.rtf, link to http://yoursite.com/file.rtf?<?=time()?> . This will append a query string parameter which will vary each time the client requests it, and it won't therefore be cached.
You could tag the current time value to the file you serve
.../file.rtf?15072010141000
That way you don't have to generate unique names, but you can ensure future requests are not cached.
Although the simple solution of using no-cache header as suggested by Pekka will work, you will lose the potential benefit of caching if same file is downloaded several times.
If the RTF file is big and you would like your user to enjoy benefit of caching when the file is actually not changed, you might want to check this answer:
How to use HTTP cache headers with PHP

Categories