Home > Backend Development > PHP Problem > How to copy files in php

How to copy files in php

藏色散人
Release: 2023-03-12 18:50:01
Original
6357 people have browsed it

How to copy files in php: 1. Create a PHP sample file; 2. Use the "protected function fileCopy(string $file, string $path){...}" method to copy files.

How to copy files in php

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

How to copy files in php?

php file processing: file copy

The code is as follows:

  /**
     * @description: 文件复制
     * @param  string $file 文件
     * @param  string $path 文件路径
     * @return: 
     */
    protected function fileCopy(string $file, string $path){
        $dir=dirname($file);                             
        $fileName= str_replace( $dir. '/','', $file);  //获取文件名
        if(!is_dir($path)){   //判断目录是否存在
            //不存在则创建
            mkdir(iconv("UTF-8", "GBK",$path),0777,true); //iconv方法是为了防止中文乱码,保证可以创建识别中文目录,不用iconv方法格式的话,将无法创建中文目录,第三参数的开启递归模式,默认是关闭的
        }
        copy(public_path($file),$path.$fileName);   //public_path()是laravel的自带方法生成public目录的绝对路径
    }
Copy after login

iconv method is to prevent Chinese garbled characters and ensure that Chinese characters can be created and recognized Directory, if the iconv method format is not used, the Chinese directory will not be created.

The second parameter is the permission. The default is 0777, which is the highest permission. After creation, click to view the third parameter without write permission.

Turn on the recursive mode. The default is false. After turning it on, it will detect the directory you pass in. It will create the parent directory based on the path you pass in and then create subdirectories recursively until the directory is complete. If it is not turned on, only the first-level directory will be created.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to copy files in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template