php 文件编程
文件编程 文件编程 文件编程的必要性 ① 在网站开发中,我们要使用到文件的上传和下载,就是经典的文件编程使用. 文件的基本概念 文件就是保存数据(二进制数据,文本数据),在程序员的角度看,文件就是数据来源. 文件流 在对文件进行操作的过程中,我们是以
文件编程
文件编程
文件编程的必要性
① 在网站开发中,我们要使用到文件的上传和下载,就是经典的文件编程使用.
文件的基本概念
文件就是保存数据(二进制数据,文本数据),在程序员的角度看,文件就是数据来源.
文件流
在对文件进行操作的过程中,我们是以流的概念来操作.
从该图我们可以看出. 如果你的数据是从程序(内存) 向 文件(磁盘) 流动,则我们称为 输出流. , 反之, 则称为输入流.
php 文件操作的基本用法
① 如何获取文件的信息.
1.1 fopen 打开文件
基本用法
fopen(文件路径[相对路径/绝对路径], 打开模式);
模式:
代码:
//1文件信息
//打开文件
$file_path="test.txt";
//该函数返回一个指向文件的指针
if($fp=fopen($file_path,"r")){
$file_info=fstat($fp);
echo "
1 2 3 |
|
//取出看看
echo"
文件大小是 {$file_info['size']}";
echo"
文件上次修改时间 ".date("Y-m-dH:i:s)",$file_info['mtime']);
echo"
文件上次访问时间 ".date("Y-m-dH:i:s)",$file_info['atime']);
echo"
文件上次change时间 ".date("Y-m-dH:i:s)",$file_info['ctime']);
}else{
echo "打开文件失败";
}
//关闭文件!!!很重要
fclose($fp);
//***第二种方式获取文件信息
echo"
".filesize($file_path);
echo"
".date("Y-m-d H:i:s",fileatime($file_path));
echo "
".filectime($file_path);
echo"
".filemtime($file_path);
?>
fstat函数
该函数返回有几个信息
[atime]=> 1316831240 【该文件上一次被访问的时间】
[mtime]=> 1316831237 【该文件上一次内容被修改时间】
[ctime]=> 1316831228 【该文件上一次 文件所有者/文件所在组 修改】
② 如何读文件
//读文件
//1.打文件
//打开文件
$file_path="test.txt";
//该函数返回一个指向文件的指针
//先判断文件是否存在
//************************第一种读取方法********************
/* if(file_exists($file_path)){
//打开文件
$fp=fopen($file_path,"a+");
//读内容,并输入
//**** 第一种读取方法**************
$con=fread($fp,filesize($file_path));
echo "文件的内容是:
";
//在默认情况下,我们得到内容输出到网页后,不会换行,因为网页
//不认\r\n 是换行符, \r\n ->
$con=str_replace("\r\n","
",$con);
echo $con;
}else{
echo "文件不存在!";
}
fclose($fp);*/
//*************第二种读取方法,一个函数****************
/* $con=file_get_contents($file_path);
//替换
$con=str_replace("\r\n","
",$con);
echo $con;*/
//*************第三种读取方法,循环读取(对付打文件)*********
$fp=fopen($file_path,"a+");
//我们设置一次读取1024个字节
$buffer=1024;
$str="";
//一边读,一边判断是否到达文件末尾
while(!feof($fp)){
//读
$str=fread($fp,$buffer);
}
$str=str_replace("\r\n","
",$str);
echo $str;
fclose($fp);
?>
文件读取的实际用法
我们连接数据库的时候,可以把用户名,密码,主机,配置到一个外部文件.,然后再php运行时,实时的获取
db.ini
host=192.168.1.23
user=admin
password=12345
dbname=test
$arr1=parse_ini_file("db.ini");
print_r($arr1);
?>
③ 如何写文件
//如何写文件
//1.传统的方法
/* $file_path="test.txt";
if(file_exists($file_path)){
//如果是追加内容,则使用a+append
//如果是全新的写入到文件 ,则使用 w+write
$fp=fopen($file_path,"w+");
$con="\r\n你好!";
for($i=0;$i
fwrite($fp,$con);
}
}else{
}
echo "添加ok";
fclose($fp);*/
//2.第二种方式写入文件
$file_path="test.txt";
$con="北京你好!\r\n";
for($i=0;$i
$con.="北京你好!\r\n";
}
file_put_contents($file_path,$con,FILE_APPEND);
echo "ok";
?>
④ 拷贝一个文件(图片)
//拷贝图片
$file_path=iconv("utf-8","gb2312","C:\\Documentsand Settings\\All Users\\Documents\\My Pictures\\示例图片\\Winter.jpg");
if(!copy($file_path,"d:\\bb.jpg")){
echo "error";
}else{
echo "ok";
}
?>
⑤ 文件的下载任务...
参看我们的http 课程.
补充
⑥ 文件创建,删除/ 文件夹的创建和删除.
//文件及文件夹的创建和删除.
//1.创建文件夹 d:/shunping
/* if(!is_dir("d:/shunping2")){
if( mkdir("d:/shunping2")){
echo "创建文件夹ok";
}else{
echo "创建文件夹err";
}
}else{
echo "该文件夹有了";
}*/
//2.能不能一次性多个文件(层级),创建ok
/* $path="d:/shunping3/aaa/bbb/cccc/ddd";
if(!is_dir($path)){
if( mkdir($path,0777,true)){
echo "创建文件夹ok";
}else{
echo "创建文件夹err";
}
}else{
echo "该文件夹有了";
}*/
//3.删除文件夹
//如果文件夹下有文件,或者目录,均不能删除ok
/* if(rmdir("d:/shunping2")){
echo "删除文件夹ok";
}else{
echo "err";
}*/
//4.文件的创建
//在d:/shunping3 目录下,创建一个文件并写入hello
/* $file_path="d:/shunping3/aa.txt";
$fp=fopen($file_path,"w+");
fwrite($fp,"hello,world");
fclose($fp);
echo "创文件ok";*/
//5.删除文件
$file_path="d:/shunping3/aa.txt";
if(is_file($file_path)){
if(unlink($file_path)){
echo "删除ok";
}else{
echo "删除error";
}
}else{
echo "文件不存在";
}
?>
php文件编程实际运用-文件上传
代码:
upload.php这是一个上传文件的界面.
如果要上传文件,我们需要把
上传文件
代码:
上传文件需要考虑的细节:
(1), 如何控制用户上传的文件大小
(2), 如何控制用户上传的文件类型
(3), 如何防止用户图片覆盖问题
(4), 如何防止同一个用户上传的文件名相同问题
upload.php
uploadprocess.php
//1.接收提交文件的用户
$username=$_POST['username'];
$fileintro=$_POST['fileintro'];
//echo $username.$fileintro;
//我们这里需要使用到 $_FILE
/* echo "
1 2 3 |
|
//获取文件的大小
$file_size=$_FILES['myfile']['size'];
if($file_size>2*1024*1024){
echo "文件过大,不能上传大于2m文件";
exit();
}
//获取文件的类型
$file_type=$_FILES['myfile']['type'];
if($file_type!='image/jpg' &&$file_type!='image/pjpeg'){
echo "文件类型只能是 jpg的";
exit();
}
//判断是否上传ok
if(is_uploaded_file($_FILES['myfile']['tmp_name'])){
//把文件转存到你希望的目录
$uploaded_file=$_FILES['myfile']['tmp_name'];
//我们给每个用户动态的创建一个文件夹
$user_path=$_SERVER['DOCUMENT_ROOT']."/file/up/".$username;
//$user_path=iconv("utf-8","gb2312",$user_path);
//判断该用户是否已经有文件夹
if(!file_exists($user_path)){
mkdir($user_path);
}
//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
$file_true_name=$_FILES['myfile']['name'];
$move_to_file=$user_path."/".time().rand(1,1000).substr($file_true_name,strrpos($file_true_name,"."));
if(move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file))){
echo $_FILES['myfile']['name']."上传ok";
}else{
echo "上传失败";
}
}else{
echo "上传失败";
}
?>
php绘图技术
php绘图坐标体系介绍:
php绘图的基本步骤
① 创建画布
② 画出各种图形
③ 输出图形(1. 输出到页面 2. 输出到一个文件,创建新图片)
④ 销毁图形(释放内存资源.)
? 如果要使用php来绘制图片,则需要启用 gd2库
在php.ini文件中
extension=php_gd2.dll
? 我们网站开发,常用的图片格式(jpg/jpeg , gif , png bmp WBMP , XBM)
jpg/jpeg 图片网站中用的多,压缩率高.图片清晰.图片比较小
gif : 支持动画. 它支持256色, 因为大量gif 文件 flash.
png: 支持无损压缩,高保真. 文件比较大
php绘图基本用法

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

C is an ideal choice for beginners to learn system programming. It contains the following components: header files, functions and main functions. A simple C program that can print "HelloWorld" needs a header file containing the standard input/output function declaration and uses the printf function in the main function to print. C programs can be compiled and run by using the GCC compiler. After you master the basics, you can move on to topics such as data types, functions, arrays, and file handling to become a proficient C programmer.
