search
HomeBackend DevelopmentPHP TutorialPHP创建/删除/复制文件夹、文件_PHP

学习了PHP的文件编程,其中PHP自身提供了复制文件的函数(copy)。自己也写了一个功能差不多的复制图片的函数,以此在这里记录一下。
在说该函数之前,先介绍一下使用PHP创建/删除文件夹、文件等知识。
1.创建文件夹

<&#63;php 
 //使用 file_exists("d:/mydir") 或 is_dir("d:/mydir")判断该文件夹是否存在 
 if(!file_exists("d:/mydir")) { 
  if(mkdir("d:/mydir")) { 
   echo "创建文件夹成功"; 
  }else{ 
   echo "创建文件夹失败"; 
  } 
 } else { 
  echo "该文件夹已存在"; 
 } 
&#63;> 

 2.创建多级目录(上面所说的,只能创建一级目录)

<&#63;php 
 //使用 file_exists("d:/mydir") 或 is_dir("d:/mydir")判断该文件夹是否存在 
 $path="d:/mydir/p/h/p/test"; 
 if(!file_exists($path)) { 
  if(mkdir($path,0777,true)) { 
   echo "创建文件夹成功"; 
  }else{ 
   echo "创建文件夹失败"; 
  } 
 } else { 
  echo "该文件夹已存在"; 
 } 
&#63;> 

3.删除文件夹

<&#63;php 
 //如果文件夹下有文件,或者目录,均不能删除成功 
 if(rmdir("d:/mydir/p/h/p/test")) { 
  echo "删除文件夹成功"; 
 } else { 
  echo "删除文件夹成功"; 
 } 
&#63;> 

 4.文件的创建

<&#63;php 
 //在d:/mydir目录下,创建一个文件并写入hello 
 $file_path="d:/mydir2/test.txt"; 
 $fp=fopen($file_path,"w+"); 
  
 fwrite($fp,"hello,world"); 
 fclose($fp); 
 echo "文件写入成功"; 
&#63;>

 5.删除文件

<&#63;php 
 $file_path="d:/mydir2/test.txt"; 
 if(is_file($file_path)) { 
  if(unlink($file_path)) { 
   echo "删除成功"; 
  } else { 
   echo "删除失败"; 
  } 
 } else { 
  echo "文件不存在"; 
 } 
&#63;> 

复制文件的函数:

<&#63;php 
 //PHP自身提供的copy文件函数:应用拷贝图片 
 //copy("来源","地点") 
 //$file_path=iconv("utf-8","gb2312","含中文路径"); 
 //将utf-8编码转为gb2312码 
 /*if(!copy("C:\\bh.PNG","D:\\bh2.png")) { 
  echo 'error'; 
 } else { 
  echo 'ok'; 
 }*/ 
 
 
 //自制拷贝文件的函数 
 function myCopyFunc($res, $des) { 
  if(file_exists($res)) { 
   $r_fp=fopen($res,"r"); 
    
   //定位 
   $pos=strripos($des,"\\"); 
   $dir=substr($des,0,$pos); 
   if(!file_exists($dir)) { 
    //可创建多级目录 
    mkdir($dir,0777,true); 
    echo "创建目录成功<br/>"; 
   } 
 
   $d_fp=fopen($des,"w+"); 
   //$fres=fread($r_fp,filesize($res)); 
 
   //边读边写 
   $buffer=1024; 
   $fres=""; 
   while(!feof($r_fp)) { 
    $fres=fread($r_fp,$buffer); 
    fwrite($d_fp,$fres); 
   } 
 
   fclose($r_fp); 
   fclose($d_fp); 
 
   echo "复制成功"; 
  } else { 
   echo "源文件不存在"; 
  } 
 } 
 
 myCopyFunc("C:\\bh.PNG","D:\\PHPTest\\test\bh2.PNG"); 
 //myCopyFunc("C:\\bh.PNG","D:\\bh.PNG"); 
&#63;> 

以上代码包含PHP文件编程中的文件读取与写入功能,希望对大家学习php程序设计有所帮助。

Statement
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
PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.