PHP file lock, upload and download _php tutorial

Source: Internet
Author: User
Tags flock

PHP file lock, upload and download


Summary file locking mechanism, upload and download 1. File locking is now in the attention of what distributed, concurrency, and so on, in fact, the operation of the file is also concurrent, in the network environment, multiple users at the same time to access the page, the same server on the same file to read, if, this user just read half, Another user wrote the message, then the previous user read the error data, in the database appears to be called dirty data, and if a user writes half, another user also writes to the file, then caused the write data confusion and errors, so PHP has a lock mechanism, similar to the database lock, When a user in the operation of the file with some kind of lock, so that at the same time other users can not operate the file or only limited operation, to ensure the correctness of the file data in these cases. The main use of the Flock function, prototype: BOOL Flock (Resource $handle, int $operation [, int & $wouldblock]), the first parameter is a handle to the file variable, the second is to lock the way, respectively, Lock_sh: Shared Lock (Share), the lock that is added when the file is read, lock, the other user can no longer write to the file, but read the contents of the file, LOCK_EX: Exclusive lock (exclude), or an exclusive lock, used when writing the file, add the lock, Only the current user can write, the other users can not read and write; Lock_nb: Additional locks, a large number of user access operations during a short period of file locking may cause the flock to block when locked, and if the lock can be used to avoid the situation (is not so much to solve the problem of a large number of read and write operations, I'm afraid not. ); Lock_un: Release the lock, release the various locks in the front, and unlock them once. If it is easy to plug, you can also use the third parameter wouldblock, if it is set to 1, after locking will block other processes to do some operations, but not supported on Windows, additional lock lock_nb,windows is not supported. In addition, fclose operations that close handle variables can also release these locks. Talk less, see Code Copy Code ), its Name property is set to Max_file_size, the reason for setting this value, is to set a file size value, to avoid the user to send a large file for half a day and then tell him: Sorry, your file is too large-_-its Value property is the size of the file, in bytes. Of course, some books say that this value is only as a reference, can be easily deceived, here is only a symbolic expression, it is a pity that my rookie on the safety of little knowledge, only know common injection, XSS, etc., for the moment. Then you can write a simple can't be any more simple page, as the client with: 2, the server file upload to the servers to go through some processing process, like online shopping delivery Express, to the destination also scored a class, confirm the destination right and wrong bar. The subsequent processing of the destination requires a PHP script, which specifies the processing script for the commit when the form is submitted with the Action property. We know that in PHP, $_post saves post-delivered data, and the information about the uploaded file is stored in $_files, assuming the server-side script is: '; Print_r ($_files); Echo ' _post:
';     Print_r ($_post); Regardless of how the server handles it, let's look at what the two arrays have: see the options for the files array to guess, these are the names, types, sizes, error messages, etc. of the uploaded file, and the files are two-dimensional arrays. Before figuring out these options it is necessary to understand a few PHP configuration options, open the php.ini file, and find the following four items (in fact, see note): File_uploads: Whether to allow the transfer of files via HTTP, the default is on allow; Upload_max_filesi Ze: Allows the maximum size of the file to be passed, in M, which is the option for server-side profile settings; Max_file_uploads: The number of long files allowed to be passed on a single request; Post_max_size: The maximum size of the data is passed through post, because file delivery is also Post method, also count post delivery, it is important to note that it must be larger than the upload_max_filesize option, because not only will upload the file in a single post pass, but also pass other values, such as the above data in the post array, must take into account,       For example, Upload_max_filesize set to 150M, this can be set to 200M; Upload_tmp_dir: The temporary directory to upload files, the configuration file default is empty, will use the operating system default temporary directory, so the above files array of tmp_name in the familiar path can be explained, using the Windows default storage temporary files directory,       and the server defaults to the file name modification. So where does the uploadfile in the files array come from, why use it as a key name, because the Name property of the upload control is UploadFile, which is tagged with the upload file information for this control, so we can put multiple upload controls, set a different name, Of course, the same name can be set, it is entirely possible to put them all in an array inside, such as。 Now look back to the file array's key name represents the information, type is MIME type, with/delimited, preceded by the main type, followed by the specific file type, error must indicate error, there are several cases, 0: no error, upload success; 1: file exceeded php config directive upload _max_filesize the size specified in the 2: The file exceeds the max_file_size specified in the HTML form, 3: The file is only partially uploaded; 4: no file upload.       Now the questions about the files array are all clear. The problem is, is not upload success does not do any processing, of course, not always in a temporary directory, the upload is more than necessary to move the file to another place, and PHP provides a special and safe function. The Is_uploaded_file function, which determines whether HTTP post uploads can be used to ensure that malicious users cheat scripts and manage those files, such as/etc/pass (again Linux ...). As to how it is, I am not sure.      The Move_uploaded_file function, which moves the uploaded file to a new location, also determines whether the file is a legitimate upload, that is, through HTTP POST, which returns the Boolean type True when they run successfully.      For a long while, upload the file about to go through the following steps: 1, the client write the upload control script, and pass a limit file size hidden value; 2, the server first judge the files array error value, see if there is error, 3, determine whether the type of upload allowed (can not judge);      4, Judge in the server script inside whether to exceed the specified file size, 5, upload to a temporary location, generate a new file name (to prevent the existing name of the file is overwritten), check and move to the new directory. Client preparation has just been done, see Server processing code: copy Code
  0) {switch ($error) {case 1:exit (' Maximum file upload limit exceeding PHP configuration ');             Case 2:exit (' Maximum file upload limit over HTML form ');             Case 3:exit (' files are only partially uploaded ');             Case 4:exit (' No files uploaded ');         Default:exit (' Unknown type error '); }}//2, determine whether to allow the type of upload $extension = PathInfo ($_files[' uploadfile ' [' name '], pathinfo_extension);         Gets the extension if (!in_array ($extension, $typeWhiteList)) {if ($extension = = ") exit (' Do not allow to upload empty type file ');    else exit (' Do not allow uploads '. $extension. ' type file ');    }//3, determine whether to allow the size if ($_files[' uploadfile ' [' Size '] > $max _size) {exit (' exceeds the allowable upload '. $max _size. ' bytes ');   }//4, to the specified position $filename = date (' YMD '). Rand (1000, 9999); Generate a new file name to prevent overwriting if (Is_uploaded_file ($_files[' uploadfile ') [' tmp_name ']) {//determine if HTTP POST uploads if (!move_upload Ed_file ($_files[' uploadfile ' [' tmp_name '], $upload _path. $filename.         $extension) {exit (' cannot be moved to specified position '); } else{echo ' File uploadSuccess 
'; echo ' filename: '. $upload _path. $filename. '. $extension. '
'; }} else{exit (' File not uploaded by legal means '); The copy code wanted to quickly experience a piece, the result reported a warning, said time set dependent system ... bug always so unexpected, set the time, try again, perfect! 3. File download file download is relatively simple, simple file download only need to use an HTML link is enough, using the tag, href attribute to specify the resource location, a little. However, this approach can only handle MIME types that are not recognized by the browser, such as RAR, 7z, and other compressed data. Copy Code <title>Donwload file</title> Header.txt
Php.zip
Pic.icoCopy the code for these browsers do not know the type of file, point link, it directly box to let you download, some browsers even directly down, then for the text txt, JPG and other browser default recognition of the type of file, a click will be directly displayed on the page, such as the above Header.txt, Pic.ico. How to download them without showing them on the page, use the header function. The header function will be sent to the header information, please take the file as an attachment, so that when clicked, it will also be downloaded. Copy Code

http://www.bkjia.com/PHPjc/917111.html www.bkjia.com true http://www.bkjia.com/PHPjc/917111.html techarticle php file lock, upload and download summary file locking mechanism, upload and download 1. File locking is now in the attention of what distributed, concurrency, and so on, in fact, the operation of the file is also ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.