Why does PHP store uploaded files in a temporary location and what is the benefit?

前端 未结 3 714
别跟我提以往
别跟我提以往 2021-01-31 01:08

Okay I\'m totally new in this field and going through some tutorials and I found that while uploading files in PHP it stores them in a temporary location.

$file         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-31 01:54

    From What is the benefit of writing to a temp location, And then copying it to the intended destination?:

    • On most platforms, file moves are atomic, but file writes are not (especially if you can't write all the data in one go). So if you have the typical producer/consumer pattern (one process produces files, the other watches a directory and picks up everything it finds), writing to a temp folder first and only then moving to the real location means the consumer can never see an unfinished file.
    • If the process that writes the file dies halfway through, you have a broken file on your disk. If it's in a real location, you have to take care of cleaning it up yourself, but if it's in a temp location, the OS will take care of it. If the file happens to be created while a backup job is running, the job may pick up an incomplete file; temp directories are generally excluded from backups, so the file will only be included once moved to the final destination.
    • The temp directory may be on a fast-but-volatile filesystem (e.g. a ramdisk), which can be beneficial for things like downloading several chunks of the same file in parallel, or doing in-place processing on the file with lots of seeks. Also, temp directories tend to cause more fragmentation than directories with less frequent reads, writes, and deletes, and keeping the temp directory on a separate partition can help keep fragmentation of the other partitions down.

提交回复
热议问题