PHP file upload analysis
PHP file upload is something that every programmer will encounter. So how much do you know about PHP file upload? This article mainly describes the detailed method of uploading PHP files. You can refer to it.
1. General file upload, unless the file is very small. Like a 5M file, it may take more than a minute to upload. But in PHP, the default maximum execution time of this page is 30 Seconds. That is to say, if it exceeds 30 seconds, the script will stop executing. This will result in the inability to open the web page. At this time, we can modify max_execution_time
Look for
max_execution_time
max_execution_time = 0
set_time_limit();
set_time_limit(0);//0表示没有限制
post_max_size = 150M
upload_max_filesize = 100M
<form enctype="multipart/form-data" action="dealfileupload.php" method="POST">
<input name="userfile" type="file"/> <input type="submit" value="上传"/> </form>
<?php $docroot=$_SERVER['DOCUMENT_ROOT']; $fileupload=$docroot."/upload/".$_FILES['userfile']['name']; if (move_uploaded_file($_FILES['userfile']['tmp_name'],$fileupload)) { echo "文件上传成功"; } else { echo "文件上传失败"; } ?>
bool move_uploaded_file (string filename, string destination)
<form enctype="multipart/form-data" action="dealfilesupload.php" method="POST">
<input name="file1" type="file"/>
<input name="file2" type="file"/>
<input type="submit" value="上传"/> </form> dealfilesupload.php <?php $docroot=$_SERVER['DOCUMENT_ROOT']; $file1upload=$docroot."/upload/".$_FILES['file1']['name']; $file2upload=$docroot."/upload/".$_FILES['file2']['name']; if (@move_uploaded_file($_FILES['file1']['tmp_name'],$file1upload)) { echo "文件1上传成功"; } else { echo "文件1上传失败"; } print "<br/>"; if (@move_uploaded_file($_FILES['file2']['tmp_name'],$file2upload)){ echo "文件2上传成功"; }else{ echo "文件2上传失败"; } ?>
(4) fwrite 写入文件内容
fwrite (打开的文件,写入的内容);
(5) fclose 关闭打开的文件
fclose (打开的文件);
(6) file_get_contents(“路径和文件名”);//功能是读取整个文件内容
(7) file_ exists(path);//函数检查文件或目录是否存在
(8) chmod(相对路径,代表PHP可访问的权限数值八进制);//更改文件或文件夹的访问权限模式
2、目录操作常用函数
mkdir() 建立新目录函数
语法:
mkdir(path,[int mode,][recursive,][context])
说明:path是必须参数,给出将要新建的文件夹所在的路径和名称,最好是使用绝对路径;mode是可选参数,默认值是0777(尝试以最大目录权限进行操作),该参数是八进制,即要设置该参数就必须将数值以0开头;recursive是可选参数,指出是否使用递归模式;context是可选参数,规定文件句柄的环境。Context 是可修改流的行为的一套选项。创建成功返回TRUE,否则返回FALSE。
rmdir()删除一个指定名称的空目录
语法:rmdir(目录路径和名称)
说明:尝试删除 目录路径和名称 所指定的目录。该目录必须是空的,而且要有相应的权限。如果成功则返回 TRUE,失败则返回 FALSE。
unlink() 删除文件函数
语法:unlink(路径和文件名)
说明:删除 路径和文件名称 所指定的文件。和 Unix C 的 unlink() 函数相似。如果成功则返回 TRUE,失败则返回 FALSE。
3、其它文件操作函数
(1)string basename(string path [,string suffix]) ,path参数给出一个文件的全路径字符串,函数返回基本的文件名。如文件名以suffix结束,则去掉这部分
(2)string dirname(string path) ,返回路径中的目录部分。
(3)array pathinfo(string path) ,返回文件路径的信息,包含以下的数组单元dirname,basename,extension.
(4)string realpath(string path) ,返回规范化的绝对路径名
(5)bool copy( string source ,string dest) ,将文件从source复制到dest
例:copy("hello.txt","temp.php");
(6)float disk_free_space(string directory ) ,返回目录中的可用空间
例:$df = disk_free_space("F:\");
echo $df.''
'';
(7)float disk_total_space(string directory) ,获取指定磁盘总空间
例:
$df=disk_total_space("F:\"); echo $df.''<br>'';
(8)int file_put_contents(string filename,string data[,int flags[,resource context]]),将一个字符串写入文件
(9)string file_get_contents(string filename [,int use_include_path[,resource context]]) ,将整个文件作为一个字符串读入。不需要之前fopen()
例:
$lines=file_get_contents("hello.txt"); echo nl2br($lines);
(10)int fileatime(string filename) ,取得文件的上次访问时间
例:
echo date("F d Y H:i:s",fileatime($filename);
(11)int filemtime(string filename),取得文件的最近修改时间
例:
echo date("F d Y H:i:s",filemtime($filename);
(12)array stat(string filename) 给出文件的信息or lstat( string filename) or fstat( resource handle)
例:
$fileinfo =stat($filename);
echo "<table border=1><th>数字下标</th><th>关键(自PHP 5.1.4)</th>"; foreach($fileinfo as $num=>$info) { echo "<tr><td>".$num."</td><td>".$info."</td></tr>"; } echo "</table>";
(13)string filetype(string filename) ,获取文件的类型
例:echo filetype($filename);
(14)bool is_dir(string filename) ,判断给定的文件名是否是一个目录
例:
if(is_dir($filename)) echo $filename.''为目录<br>''; else echo $filename.''非目录<br>'';
(15)bool flock(int handle,int operation [,int &wouldblock]) ,进行文件锁定
operation: LOCK_SH:共享锁定
LOCK_EX: 独占锁定
LOCK_UN: 释放锁定
(16)bool is_uploaded_file(string filename)
判断文件是否通过HTTP POST上传
(17)bool move_uploaded_file(string filename,string destination)
检测文件是否是合法的上传文件,是则移动到destination 指定的文件
(18)array file(path,include_path,context)
函数把整个文件读入一个数组中。与 file_get_contents() 类似,不同的是 file() 将文件作为一个数组返回。数组中的每个单元都是文件中相应的一行,包括换行符在内。如果失败,则返回 false。
path 必需。规定要读取的文件。include_path 可选。如果也想在 include_path 中搜寻文件的话,可以将该参数设为 "1"。context 可选。规定文件句柄的环境。 context 是一套可以修改流的行为的选项。若使用 null,则忽略。
注释:如果碰到 PHP 在读取文件时不能识别 Macintosh 文件的行结束符,可以激活 auto_detect_line_endings 运行时配置选项。
(19) string fgetss()
fgetss() 函数从打开的文件中读取一行并过滤掉 HTML 和 PHP 标记。与 fgets() 相同,不同的是 fgetss 尝试从读取的文本中去掉任何 HTML 和 PHP 标记。
使用方法:fgetss(file,length,tags)
file 必需。规定要读取的文件。length 可选。规定要读取的字节数。默认是 1024 字节。该参数在 PHP 5 之前是必需的。tags 可选。规定将被删除的标签。可以用可选的第三个参数 tags 指定哪些标记不被去掉。若失败,则返回 false。
<?php $file = fopen("test.htm","r"); echo fgetss($file,1024,"<p>,<b>");//保留p和b标记 fclose($file); ?>
(20) string fgets()
fgets() 函数从文件指针中读取一行。file 必需。规定要读取的文件。length 可选。规定要读取的字节数; 默认是 1024 字节。
从 file 指向的文件中读取一行并返回长度最多为 length - 1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length - 1 字节后停止(要看先碰到那一种情况)。如果没有指定 length,则默认为 1K,或者说 1024 字节。
若失败,则返回 false。
4、遍历文件夹中的文件
$dir=opendir(''文件夹路径名'');//打开文件夹,返回一个文件夹对象(句柄)
while($fileName=readdir($dir))//readdir(参数是用opendir打开的文件夹对象名),每执行一次都会
取出指定文件夹中的一个文件名称,且文件指针向下移动一个
{ echo ''fileName=''.$fileName. ''<br />'';//输出取得的文件名 } ?>
opendir() 函数返回一个目录句柄即文件夹对象,可由 closedir(),readdir() 和 rewinddir() 使用。
若成功,则该函数返回一个目录流,否则返回 false 以及一个 error。可以通过在函数名前加上 "@" 来隐藏 error 的输出。
readdir() 函数返回由 opendir() 打开的目录句柄中的文件名称。若成功,则该函数返回一个文件名,否则返回 false。
5、文件上传成功后要清除其所占用的内存,方法是使用imagedestroy()函数。详见手册
篇幅有点长,希望帮助到大家,让大家对PHP文件上传有更清楚的思路。
相关推荐:
The above is the detailed content of PHP file upload analysis. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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 is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
