A web resource is a “point” in the information space of a worldwide network. The information displayed on it is usually presented statically, and the visitor views it. You can specify a link to a document or other resource and the visitor will download a file with information or follow the link.
Simple tools are used to transfer information to the server or to receive it from it. But their reasonable application and convenient logic of use are not so simple when it is necessary to organize safe, stable and reliable data processing.
File upload
There was a simple idea: there is upload and download. The first is to send the file to the server, the second is to receive it. To implement the first task, information can simply be displayed in a browser, in a window on a page or provide a link to an information resource. To implement the second task, it is enough to write a file upload form and its processing code on the server.
In this example (1) - two options for links (without using PHP) to download files from the server, (2) - a form for transferring files to the server. Here, the use of PHP is fundamental. It uses more advanced loading logic than usual.
In the general case, it is enough to write the given form in the page code and provide for the processing of the $ _FILES array.
Checking the file name (scCheckFileName function) is not a prerequisite, but for security and reliability - this is advisable to do. Define the rules for any downloaded file - the key to reliable operation of the web resource and respect for it by the visitor.
Server upload logic
Hosting is hostile, but using Russian letters and some characters in file names is fraught with danger. He can “write” the file, but then “cannot” read it or delete it. To avoid ambiguities, it is better to immediately rename the file and save it under the name that any hosting will adequately relate to.
In this context, uploading files to the server: PHP + MySQL. Using the language, the file is downloaded. Through the database, an association is established between the real file name and the unique one, which is automatically generated. The simplest solution, how to get its MD5 code using the real file name, is a unique sequence of 32 bytes. When uploading files to the server, PHP "turns on" on the server through AJAX.
Here the scfCheckFile () function encodes the real file name, generates a unique MD5 code from it, and starts the data transfer mechanism through AJAX. At this moment, the server does not download the file, but only checks for its presence using a unique code. If no such file is found, a new file name is generated, and a record about the beginning of the download is placed in the table row.
The scfGetFile () function confirms the fact that the file has been downloaded and provides an update to the record about it.
As a result: the file is loaded, the record in the table is formed. Uploading files to the server in PHP & AJAX using JavaScript is a good and reliable solution.
AJAX file upload mechanism
AJAX data transfer start is initiated by the InitXML () function. Uploading files to the server in PHP, JavaScript and AJAX with the proposed code is safe and reliable.
Receiving a response from the server is performed by the WaitReplySC () function. The codes on the bottom right are for two situations: checking for the presence of a file before downloading and the actual download procedure.
Coding options may be different, it is essential that the file download button is not active until it is selected and checked by the server.
PHP server code
The server should check the ability to download the file. Obstacles to downloading are determined by the problem being solved, but it is always important not to overwrite the information, that is, to foresee and block the downloading of files to the server on PHP again.
To solve this problem, the easiest way is to create a table in the MySQL database and mark the download of each file with one line there. Here you can store the real name of the file, its extension, download date, keep records of operations on it, etc.
This is a code to check for a file by its unique name. The function of the database object $ oBase NewViuq () defines a unique name for the loaded item and maps it to the real file name.
If the name check was successful, the visitor clicks on the download button and the AJAX mechanism initiates the upload of files to the server. PHP code corrects the entry in the MySQL table and the following result is obtained:
Another element appeared in the file system, and the table row of this file received the final content. The status field (f_status) contains the character "N", that is, operations on the downloaded file have not yet been.
Data Logic
The ubiquitous distribution of * docx, * .xlsx, * .pdf files and archives led to the formation of a not entirely correct concept: HTML & PHP downloading files from the server is a link to them. Moreover, it is not at all necessary that the visitor downloads the item. Often a programmer simply opens a file in a browser for viewing.
Many tasks involve changing information in such files. An employee of the company administering the site checks the set of relevant files, downloads them, modifies them and sends them back to the server. Such logic causes significant traffic, loads communication channels and makes it difficult to maintain a web resource and work with it.
Of course, it is impossible to change the current system of information relations in a particular company. If priority is given to the file organization, then it should be optimized. If it is possible to transform files into data and work with them directly, then the ideal option would be: download, convert them to an active form, which allows content to be changed by its elements. When it is necessary to download a file from the server, it is converted in the current updated state and sent to the consumer.