百科狗-知识改变命运!
--

copy() - 拷贝文件 - php 文件目录函数

梵高1年前 (2023-11-21)阅读数 19#技术干货
文章标签文件

copy()

(PHP 4, PHP 5, PHP 7)

拷贝文件

说明

copy(string $source,string $dest[,resource $context]): bool

将文件从$source拷贝到$dest。

copy() - 拷贝文件 - php 文件目录函数

如果要移动文件的话,请使用rename()函数。

参数

$source

源文件路径。

$dest

目标路径。如果$dest是一个 URL,则如果封装协议不支持覆盖已有的文件时拷贝操作会失败。

Warning

如果目标文件已存在,将会被覆盖。

$context

A valid context resource created withstream_context_create().

返回值

成功时返回TRUE,或者在失败时返回FALSE

更新日志

版本说明
5.3.0增加了对 context 的支持。
4.3.0如果启用了“fopen wrappers”的话,$source和$dest都可以是 URL。更多细节见fopen()。

范例

Example #1copy()例子

参见

  • move_uploaded_file() 将上传的文件移动到新位置
  • rename() 重命名一个文件或目录
  • The section of the manual abouthandling file uploads
Having spent hours tacking down a copy() error: Permission denied , (and duly worrying about chmod on winXP) , its worth pointing out that the 'destination' needs to contain the actual file name ! --- NOT just the path to the folder you wish to copy into.......
DOH !
hope this saves somebody hours of fruitless debugging
It take me a long time to find out what the problem is when i've got an error on copy(). It DOESN'T create any directories. It only copies to existing path. So create directories before. Hope i'll help,
Don't forget; you can use copy on remote files, rather than doing messy fopen stuff. e.g. 
A nice simple trick if you need to make sure the folder exists first:

That simple.
Here is a simple script that I use for removing and copying non-empty directories. Very useful when you are not sure what is the type of a file.
I am using these for managing folders and zip archives for my website plugins.

Cheers!
Copying large files under Windows 8.1, from one NTFS filesystem to another NTFS filesystem, results in only the first 4 GiB copied and the rest of the file is ignored.
So, if you think to have files larger than 4 GiB, instead of doing:
  copy($source,$destination);
it is much better to do something like:
  exec("xcopy $source $destination");
I will check to see if this issue is valid also under Linux.
It depends on PHP not being compiled in 64 bit mode?
Here's a simple recursive function to copy entire directories
Note to do your own check to make sure the directory exists that you first call it on. 
some hosts disable copy() function and say its for security 
and for some copy is important so this is and simple function that do same as copy function effect 
how smart php can help us l like php 
It's worth noting that copy() sets the destination file's last modified time/date.
My own 'cp -R' written in PHP.
Hopefully it will work for your situation. I'm using it in a web based file manager for my CMS. 
If you try to copy a file to itself - e.g. if the target directory is just a symlink to the source directory - copy will return false. just like on the command line.
you can also try xcopy command by using Shell to move/copy files/folders from one place to another
here is the code:

by executing this command, it will move folder along with all contents to destination.
-adnan
Paths and filenames with Japanese characters are not handled correctly if you are running Apache/PHP on a Windows machine.
With the following code you can convert e.g. the network path to the appropriate encoding so that Windows understands where to look:

There are however a number of characters that cannot be converted correctly like '②' and '﨑' because there are no SJIS equivalents.
Thanks for all the comments, i wrote this function for fully supporting file and directory copy. 
As you may have noticed there are feature that didn't have time to implement , but if you have time to implement them or even add more cool features, please notify me as well :). you can find me here http://sina.salek.ws/en/contact
PS : It was very useful for me, hope you find it useful as well. 
Below a code snippet for downloading a file from a web server to a local file.
It demonstrates useful customizations of the request (such as setting a User-Agent and Referrer, often required by web sites), and how to download only files if the copy on the web site is newer than the local copy.
It further demonstrates the processing of response headers (if set by server) to determine the timestamp and file name. The file type is checked because some servers return a 200 OK return code with a textual "not found" page, instead of a proper 404 return code.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)