©
本文檔使用 php中文網手册 發布
(PHP 4, PHP 5, PHP 7)
copy — 拷贝文件
$source
, string $dest
[, resource $context
] )
将文件从 source
拷贝到 dest
。
如果要移动文件的话,请使用 rename() 函数。
source
源文件路径。
dest
目标路径。如果 dest
是一个 URL,则如果封装协议不支持覆盖已有的文件时拷贝操作会失败。
如果目标文件已存在,将会被覆盖。
context
A valid context resource created with stream_context_create() .
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
版本 | 说明 |
---|---|
5.3.0 | 增加了对 context 的支持。 |
4.3.0 |
如果启用了“fopen wrappers”的话,source 和
dest 都可以是 URL。更多细节见 fopen() 。
|
Example #1 copy() 例子
<?php
$file = 'example.txt' ;
$newfile = 'example.txt.bak' ;
if (! copy ( $file , $newfile )) {
echo "failed to copy $file ...\n" ;
}
?>
[#1] hugo_2000 at gmx dot at [2015-06-26 14:43:49]
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.
[#2] Bas Vijfwinkel [2015-05-20 07:09:30]
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:
<?PHP
function convertPath($path)
{
//split networkpath into parts
$parts = explode('\\',$path);
// convert each part to SJIS
foreach($parts as $index => $part)
{
$parts[$index] = iconv('UTF-8','SJIS//IGNORE',$part);
}
// put the network path back together
return implode('\\',$parts);
}
$oldname = convertPath('c:/Temp/??????.pdf');
$newname = convertPath('\\\\PFSV0100\\DATA\\06??????e\\333328_??????\\test_??????????.pdf');
copy($oldname, $newname);
?>
There are however a number of characters that cannot be converted correctly like '??' and '?D' because there are no SJIS equivalents.
[#3] ASchmidt at Anamera dot net [2014-05-28 01:49:05]
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.
<?php
// $fURI: URL to a file located on a web server
// $target_file: Path to a local file
if ( file_exists( $target_file ) ) {
$ifmodhdr = 'If-Modified-Since: '.date( "r", filemtime( $target_file ) )."\r\n";
}
else {
$ifmodhdr = '';
}
// set request header for GET with referrer for modified files, that follows redirects
$arrRequestHeaders = array(
'http'=>array(
'method' =>'GET',
'protocol_version' =>1.1,
'follow_location' =>1,
'header'=> "User-Agent: Anamera-Feed/1.0\r\n" .
"Referer: $source\r\n" .
$ifmodhdr
)
);
$rc = copy( $fURI, $target_file, stream_context_create($arrRequestHeaders) );
// HTTP request completed, preserve system error, if any
if( $rc ) {
if ( fclose( $rc ) ) {
unset( $err );
}
else {
$err = error_get_last();
}
}
else {
$err = error_get_last();
}
// Parse HTTP Response Headers for HTTP Status, as well filename, type, date information
// Need to start from rear, to get last set of headers after possible sets of redirection headers
if ( $http_response_header ) {
for ( $i = sizeof($http_response_header) - 1; $i >= 0; $i-- ) {
if ( preg_match('@^http/\S+ (\S{3,}) (.+)$@i', $http_response_header[$i], $http_status) > 0 ) {
// HTTP Status header means we have reached beginning of response headers for last request
break;
}
elseif ( preg_match('@^(\S+):\s*(.+)\s*$@', $http_response_header[$i], $arrHeader) > 0 ) {
switch ( $arrHeader[1] ) {
case 'Last-Modified':
if ( !isset($http_content_modtime) ) {
$http_content_modtime = strtotime( $arrHeader[2] );
}
break;
case 'Content-Type':
if ( !isset($http_content_image_type) ) {
if ( preg_match('@^image/(\w+)@ims', $arrHeader[2], $arrTokens) > 0 ) {
if ( in_array(strtolower($arrTokens[1]), $arrValidTypes)) {
$http_content_image_type = $arrTokens[1];
break;
}
}
throw new Exception( "Error accessing file $fURI; invalid content type: $arrHeader[2]", 2);
}
break;
case 'Content-Disposition':
if ( !isset($http_content_filename) && preg_match('@filename\\s*=\\s*(?|"([^"]+)"|([\\S]+));?@ims', $arrHeader[2], $arrTokens) > 0 ) {
$http_content_filename = basename($arrTokens[1]);
}
break;
}
}
}
}
if ( $http_status ) {
// Make sure we have good HTTP Status
switch ( $http_status[1] ) {
case '200':
// SUCCESS: HTTP Status is "200 OK"
break;
case '304':
throw new Exception( "Remote file not newer: $fURI", $http_status[1] );
break;
case '404':
throw new Exception( "Remote file not found: $fURI", $http_status[1] );
break;
default:
throw new Exception( "HTTP Error, $http_status[2], accessing $fURI", $http_status[1] );
break;
}
}
elseif ( $err ) {
// Protocol / Communication error
throw new Exception( $err['message'], $err['type'] );
}
else {
// No HTTP status and no error
throw new customException( "Unknown HTTP response accessing $fURI: $http_response_header[0]", -1 );
}
?>
Notes:
1. Currently copy() does NOT appropriately handle the 304 response code. Instead of NOT performing a copy (possibly setting the RC), it will overwrite the target file with an zero length file.
2. There may be a problem accessing a list of remote files when HTTP 1.1 protocol is used. If you experience time-out errors, try the default 1.0 protocol version.
[#4] absorbentshoulderman at gmail dot com [2013-04-03 00:51:59]
A nice simple trick if you need to make sure the folder exists first:
<?php
$srcfile='C:\File\Whatever\Path\Joe.txt';
$dstfile='G:\Shared\Reports\Joe.txt';
mkdir(dirname($dstfile), 0777, true);
copy($srcfile, $dstfile);
?>
That simple.
[#5] promaty at gmail dot com [2011-05-17 11:13:43]
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.
<?php
// removes files and non-empty directories
function rrmdir($dir) {
if (is_dir($dir)) {
$files = scandir($dir);
foreach ($files as $file)
if ($file != "." && $file != "..") rrmdir("$dir/$file");
rmdir($dir);
}
else if (file_exists($dir)) unlink($dir);
}
// copies files and non-empty directories
function rcopy($src, $dst) {
if (file_exists($dst)) rrmdir($dst);
if (is_dir($src)) {
mkdir($dst);
$files = scandir($src);
foreach ($files as $file)
if ($file != "." && $file != "..") rcopy("$src/$file", "$dst/$file");
}
else if (file_exists($src)) copy($src, $dst);
}
?>
Cheers!
[#6] cory at veck dot ca [2011-04-29 23:17:44]
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.
<?php
define('DS', DIRECTORY_SEPARATOR); // I always use this short form in my code.
function copy_r( $path, $dest )
{
if( is_dir($path) )
{
@mkdir( $dest );
$objects = scandir($path);
if( sizeof($objects) > 0 )
{
foreach( $objects as $file )
{
if( $file == "." || $file == ".." )
continue;
// go on
if( is_dir( $path.DS.$file ) )
{
copy_r( $path.DS.$file, $dest.DS.$file );
}
else
{
copy( $path.DS.$file, $dest.DS.$file );
}
}
}
return true;
}
elseif( is_file($path) )
{
return copy($path, $dest);
}
else
{
return false;
}
}
?>
[#7] Yoga Wibowo Aji [2011-02-08 09:22:54]
you can try this code if you don't having permission to access the shell
this code is alternative for file copy
<?php
$source = 'myfile.doc';
$destination = 'clonefile.doc';
$data = file_get_contents($source);
$handle = fopen($destination, "w");
fwrite($handle, $data);
fclose($handle);
?>
[#8] eng-ayman at aymax dot net [2009-08-30 05:48:48]
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
<?php
function copyemz($file1,$file2){
$contentx =@file_get_contents($file1);
$openedfile = fopen($file2, "w");
fwrite($openedfile, $contentx);
fclose($openedfile);
if ($contentx === FALSE) {
$status=false;
}else $status=true;
return $status;
}
?>
[#9] Sina Salek [2009-06-02 05:02:19]
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.
<?php
function smartCopy($source, $dest, $options=array('folderPermission'=>0755,'filePermission'=>0755))
{
$result=false;
if (is_file($source)) {
if ($dest[strlen($dest)-1]=='/') {
if (!file_exists($dest)) {
cmfcDirectory::makeAll($dest,$options['folderPermission'],true);
}
$__dest=$dest."/".basename($source);
} else {
$__dest=$dest;
}
$result=copy($source, $__dest);
chmod($__dest,$options['filePermission']);
} elseif(is_dir($source)) {
if ($dest[strlen($dest)-1]=='/') {
if ($source[strlen($source)-1]=='/') {
//Copy only contents
} else {
//Change parent itself and its contents
$dest=$dest.basename($source);
@mkdir($dest);
chmod($dest,$options['filePermission']);
}
} else {
if ($source[strlen($source)-1]=='/') {
//Copy parent directory with new name and all its content
@mkdir($dest,$options['folderPermission']);
chmod($dest,$options['filePermission']);
} else {
//Copy parent directory with new name and all its content
@mkdir($dest,$options['folderPermission']);
chmod($dest,$options['filePermission']);
}
}
$dirHandle=opendir($source);
while($file=readdir($dirHandle))
{
if($file!="." && $file!="..")
{
if(!is_dir($source."/".$file)) {
$__dest=$dest."/".$file;
} else {
$__dest=$dest."/".$file;
}
//echo "$source/$file ||| $__dest<br />";
$result=smartCopy($source."/".$file, $__dest, $options);
}
}
closedir($dirHandle);
} else {
$result=false;
}
return $result;
}
?>
[#10] gimmicklessgpt at gmail dot com [2009-05-20 16:04:46]
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.
<?php
function recurse_copy($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
?>
[#11] nensa at zeec dot biz [2009-01-27 11:44:03]
When I recently had to copy a few millions of small files (< 1kb) from one NAS to another and for some reasons had to do that file by file I compared the following function with copy($src, $dest) and shell_exec("cp -r $src $dest").
Surprisingly stream_copy seamed to be slightly faster (at least in this specific context).
<?php
function stream_copy($src, $dest)
{
$fsrc = fopen($src,'r');
$fdest = fopen($dest,'w+');
$len = stream_copy_to_stream($fsrc,$fdest);
fclose($fsrc);
fclose($fdest);
return $len;
}
?>
[#12] allasso residing at signalmesa dot com [2008-11-28 03:15:41]
As far as recursive copy, something like this seems to work fine for me:
<?php
$output = shell_exec( " cp -r -a dir_source
define("DS","/",true);
define('BASE_PATH',realpath(dirname(__FILE__)).DS,true);
?>
You can call any file any time without any problems
<?php
include BASE_PATH.'inc/class.php';
?>
[#5] minimalist at intelligence dot com [2012-03-10 17:33:16]
If you want to get the parent parent directory of your script, you can use this:
<?php
//Example script path: home/content/en/script.php
$parentparentdir=basename(dirname(dirname(__FILE__)));
echo $parentparentdir; //will output 'content'
?>
[#6] nhl261 at yahoo dot com [2011-06-26 20:54:14]
As usual, to include or require a file, we use this
<?php
require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'my_file.php';
?>
in rare case, we have current file existing at the root directory, dirname would return C:\ or / , then the line above contains 2 slashes \\ or //
To handle this this case, we use rtrim to clear slashes.
<?php
require rtrim(dirname(__FILE__), '/\\') . DIRECTORY_SEPARATOR . 'my_file.php';
?>
Also, another use of dirname is to get virtual directory (url path), the issue is the same as above, we have to check and process before concatenating strings
[#7] subzey at immelman dot ru [2010-09-07 09:51:26]
In some situations (I can't locate the dependencies) basename and dirname may return incorrect values if parsed string is in UTF-8.
Like, dirname("glossary/???????-??????") will return "glossary" and basename("glossary/???????-??????") will return "-??????".
Quickfix is
str_replace("!$!", "", dirname(str_replace("/", "!$!/!$!", $q)))
[#8] webyazilimci84 at gmail dot com [2009-11-25 06:06:53]
In my mvc based framework i make BASE_PATH and BASE_URL definitions like the following and both work well in the framework without problem.
index.php :
define('BASE_PATH',realpath('.'));
define('BASE_URL', dirname($_SERVER["SCRIPT_NAME"]));
BASE_PATH is for server side inclusions.
BASE_URL is for client side inclusions (scripts, css files, images etc.)
[#9] ken dot forslund at hp dot com [2009-05-29 13:33:07]
A key problem to hierarchical include trees is that PHP processes include paths relative to the original file, not the current including file.
A solution to that, is to prefix all include paths with:
<?php str_replace('//','/',dirname(__FILE__)); ?>
this will generate a base path relative to the current file, which will then allow an include behavior similar to C/C++.
thus, to include a file that is 1 in the parent directory:
<?php require_once( str_replace('//','/',dirname(__FILE__).'/') .'../parent.php'); ?>
to include a file that is in the same directory:
<?php require_once( str_replace('//','/',dirname(__FILE__).'/') .'neighbor.php'); ?>
to include a file that is in a subdirectory:
<?php require_once( str_replace('//','/',dirname(__FILE__).'/') .'folder/sub.php'); ?>
Notice that all paths we reference must NOT begin with a /, and must be relative to the current file, in order to concatenate correctly.
[#10] joe dot naylor at gmail dot com [2008-12-13 10:07:02]
The dirname function does not usually return a slash on the end, which might encourage you to create links using code like this:
$url = dirname($_SERVER['PHP_SELF']) . '/somepage.php';
However dirname returns a slash if the path you specify is the root, so $url in that case would become '//somepage.php'. If you put that URL as the action on a form, for example, submitting the form will try to go to http://somepage.php.
I ran into this when I wrote a site on a url with a path, www.somehost.com/client/somepage.php, where the code above works great, but then wanted to put it on a subdomain, client.somehost.com/somepage.php, where things started breaking.
The best solution would be to create a function that generates absolute URLs and use that throughout the site, but creating a safe_dirname function (and an htaccess rewrite to fix double-slashes just in case) fixed the issue for me:
<?php
function safe_dirname($path)
{
$dirname = dirname($path);
return $dirname == '/' ? '' : $dirname;
}
?>
[#11] Tom [2008-07-20 11:13:18]
Expanding on Anonymous' comment, this is not necessarily correct. If the user is using a secure protocol, this URL is inaccurate. This will work properly:
<?php
// Is the user using HTTPS?
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')) ? 'https://' : 'http://';
// Complete the URL
$url .= $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// echo the URL
echo $url;
?>
[#12] Anonymous [2008-06-04 13:01:10]
A simple way to show the www path to a folder containing a file...
echo "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
[#13] rmahase at gmail dot com [2008-05-11 23:31:30]
this little function gets the top level public directory
eg. http://www.mysite.com/directory1/file.php
or http://www.mysite.com/directory1/directory2/directory3/file.php
will both return "directory1" ...which is the top level directory
<?php
function public_base_directory()
{
//get public directory structure eg "/top/second/third"
$public_directory = dirname($_SERVER['PHP_SELF']);
//place each directory into array
$directory_array = explode('/', $public_directory);
//get highest or top level in array of directory strings
$public_base = max($directory_array);
return $public_base;
}
?>
[#14] ts at dev dot websafe dot pl [2008-03-23 15:55:06]
Inside of script.php I needed to know the name of the containing directory. For example, if my script was in '/var/www/htdocs/website/somedir/script.php' i needed to know 'somedir' in a unified way.
The solution is:
<?php
$containing_dir = basename(dirname(__FILE__));
?>
[#15] Zingus J. Rinkle [2007-09-10 10:55:35]
Most mkpath() function I saw listed here seem long and convoluted.
Here's mine:
<?php
function mkpath($path)
{
if(@mkdir($path) or file_exists($path)) return true;
return (mkpath(dirname($path)) and mkdir($path));
}
?>
Untested on windows, but dirname() manual says it should work.
[#16] hans111 at yahoo dot com [2007-01-26 16:25:15]
The same function but a bit improved, will use REQUEST_URI, if not available, will use PHP_SELF and if not available will use __FILE__, in this case, the function MUST be in the same file. It should work, both under Windows and *NIX.
<?php
function my_dir(){
return end(explode('/', dirname(!empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : !empty($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : str_replace('\\','/',__FILE__))));
}
?>
[#17] Xedecimal at gmail dot com [2006-10-24 11:35:10]
Getting absolute path of the current script:
<?php
dirname(__FILE__)
?>
Getting webserver relative path of the current script...
<?php
function GetRelativePath($path)
{
$npath = str_replace('\\', '/', $path);
return str_replace(GetVar('DOCUMENT_ROOT'), '', $npath);
}
?>
later on
<?php
GetRelativePath(dirname(__FILE__));
?>
If anyone has a better way, get to the constructive critisism!
[#18] legolas558 dot sourceforge comma net [2006-07-10 06:52:16]
The best way to get the absolute path of the folder of the currently parsed PHP script is:
<?php
if (DIRECTORY_SEPARATOR=='/')
$absolute_path = dirname(__FILE__).'/';
else
$absolute_path = str_replace('\\', '/', dirname(__FILE__)).'/';
?>
This will result in an absolute unix-style path which works ok also on PHP5 under Windows, where mixing '\' and '/' may give troubles.
[EDIT by danbrown AT php DOT net: Applied author-supplied fix from follow-up note.]
[#19] renich at woralelandia dot com [2005-08-10 10:15:09]
--- Edited by tularis@php.net ---
You could also have a look at the getcwd() function
--- End Edit ---
A nice "current directory" function.
function current_dir()
{
$path = dirname($_SERVER[PHP_SELF]);
$position = strrpos($path,'/') + 1;
print substr($path,$position);
}
current_dir();
I find this usefull for a lot of stuff! You can maintain a modular site with dir names as modules names. At least I would like PHP guys to add this to the function list!
If there is anything out there like it, please tell me.
[#20] klugg this-is-junk at tlen dot pl [2005-07-18 07:14:50]
Attention with this. Dirname likes to mess with the slashes.
On Windows, Apache:
<?php
echo '$_SERVER[PHP_SELF]: ' . $_SERVER['PHP_SELF'] . '<br />';
echo 'Dirname($_SERVER[PHP_SELF]: ' . dirname($_SERVER['PHP_SELF']) . '<br>';
?>
prints out
$_SERVER[PHP_SELF]: /index.php
Dirname($_SERVER[PHP_SELF]: \
[#21] tobylewis at mac dot com [2005-06-24 06:52:20]
Since the paths in the examples given only have two parts (e.g. "/etc/passwd") it is not obvious whether dirname returns the single path element of the parent directory or whether it returns the whole path up to and including the parent directory. From experimentation it appears to be the latter.
e.g.
dirname('/usr/local/magic/bin');
returns '/usr/local/magic' and not just 'magic'
Also it is not immediately obvious that dirname effectively returns the parent directory of the last item of the path regardless of whether the last item is a directory or a file. (i.e. one might think that if the path given was a directory then dirname would return the entire original path since that is a directory name.)
Further the presense of a directory separator at the end of the path does not necessarily indicate that last item of the path is a directory, and so
dirname('/usr/local/magic/bin/'); #note final '/'
would return the same result as in my example above.
In short this seems to be more of a string manipulation function that strips off the last non-null file or directory element off of a path string.
[#22] Holger Th?lking [2005-04-27 15:31:05]
If you merely want to find out wether a certain file is located within or underneath a certain directory or not, e.g. for White List validation, the following function might be useful to you:
<?php
function in_dir ($file, $in_dir)
{
$dir = realpath ($file);
$in_dir = realpath ($in_dir);
if (!is_dir ($file)) {
$dir = dirname ($file);
}
do {
if ($dir === $in_dir) {
$is_in_dir = TRUE;
break;
}
} while ($dir !== ($dir = dirname ($dir)));
return (bool) @$is_in_dir;
}
?>
[#23] soywiz at hotmail dot com [2004-01-01 18:41:25]
You can use it to get parent directory:
dirname(dirname(__FILE__))
...include a file relative to file path:
include(dirname(__FILE__) . '/path/relative/file_to_include.php');
..etc.
[#24] andrey at php dot net [2003-01-14 07:02:17]
Code for write permissions check:
<?php
error_reporting(E_ALL);
$dir_name = '/var/www/virtual/phpintra/htdocs/php/';
do {
$b_is_writable = is_writable($dir_name);
echo sprintf("Dir[%s]Writable[%s]\n", $dir_name, $b_is_writable? 'YES':'NO');
}while (($dir_name = dirname($dir_name)) !='/');
?>
[#25] rudecoder at yahoo dot com [2003-01-11 12:43:37]
dirname can be used to create self referencing web scripts with the following one liner.
<?php
$base_url = str_replace($DOCUMENT_ROOT, "", dirname($PHP_SELF));
?>
Using this method on a file such as:
/home/mysite/public_html/wherever/whatever.php
will return:
/wherever
Now $base_url can be used in your HTML to reference other scripts in the same directory.
Example:
href='
<?php=$base_url?>
/myscript.php'
[#26] dave at corecomm dot us [2003-01-08 10:20:29]
I very much appreciated Fredrich Echol's suggestion (rwf at gpcom dot net) of how to find a base path, but found that it failed when the initial script was already in the root folder -- dirname('/rootscript.php')=='/' and dirname('/include/includescript.php')=='/include' which have the same number of slashes. This variation is what I'm now using:
<?php
if (!defined("BASE_PATH")) define('BASE_PATH', dirname($_SERVER['SCRIPT_NAME'])=='/' ? './' : str_repeat("../", substr_count(dirname($_SERVER["SCRIPT_NAME"]), "/")));
?>
This explicitly checks for the root path (/) and uses './' as the base path if we're in the root folder.
I put this at/near the top of any file that calls another. (I used define for my own convenience; should work just fine with variables and without testing to see if you already did it.)
Note that in both cases (root-folder script and non-root-folder script), BASE_PATH will include a trailing slash. At least with Apache on Darwin (Mac OS X), you can include(BASE_PATH.'/myfile.php'); and the doubled slash won't cause any problems, giving the same result as include(BASE_PATH.'myfile.php'); .
[#27] tapken at engter dot de [2002-04-30 12:09:50]
To get the directory of current included file:
<?php
dirname(__FILE__);
?>
For example, if a script called 'database.init.php' which is included from anywhere on the filesystem wants to include the script 'database.class.php', which lays in the same directory, you can use:
<?php
include_once(dirname(__FILE__) . '/database.class.php');
?>