编码:
$tmp_file_path = '/path/to/tmp/file';
// move file from tmp dir
$new_file_path = '/path/to/new/location/file';
while(file_exists($new_file_path)){
$new_file_path = $new_file_path . microtime(true);
usleep(100000);
}
// HERE ANOTHER INSTANCE OF THIS SCRIPT COULD HAVE ALREADY TAKEN NEW NAME,
// $new_file_path
// BUT NEXT CALL TO rename function OVERWRITES IT WITH NEW CONTENT!
rename($tmp_file_path, $new_file_path ); //Attempts to rename oldname to newname,
// moving it between directories if necessary. If newname exists, it will be
// overwritten.
PHP中有什么解决方案可以让file_exists
函数创建一个文件,如果它不存在,就像touch
在一个原子操作中一样?