当前位置:网站首页>PHP文件上传小结(乱码,移动失败,权限,显示图片)
PHP文件上传小结(乱码,移动失败,权限,显示图片)
2022-06-30 19:49:00 【星哥玩云】
LAMP环境:
Linux Mint 16 32bits xfce
apache 2.4.6 Ubuntu
php 5.5.3
默认www是/var/www,我用符号连接到了/home/tony/www
然后修改sudo chmod 777 www
上传页面代码:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body>
<form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form>
</body> </html>
这个代码要加head指明字符集,下面的代码同理
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " .$_FILES["file"]["name"]. "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]."<br />"; } if(is_uploaded_file($_FILES["file"]["tmp_name"])){ echo "legal uploaded file<br>"; }else echo "illegai uploaded file<br>"; $src_path= $_FILES["file"]["tmp_name"]; $des_path= '/home/tony/www/upload/'."a.jpg"; //$des_path= '/home/tony/www/upload/'.$_FILES["file"]["name"]; echo $src_path . "<br />"; echo $des_path . "<br />"; if(file_exists($src_path)){ echo "file exists.<br />"; } if(move_uploaded_file($src_path,$des_path)){ echo "Stored in: "."<br />"; }else echo"<br>move failed."; printf("<img src=%s />","upload/a.jpg"); ?>
这段出来代码冗杂,不美观。但是说明了一些问题。
1/还是要指定字符集,不然在不同的浏览器或者系统上会有问题,apache2的配置文件不要AddDefaultCharset功能,如果改动过请改为AddDefaultCharset Off;重启apache2
2/临时文件在php执行之后就会消失,肉眼难见,可以在最后来个while(1);
3/有人问为什么不可以用其他函数来转移上传的文件呢?既然我都有路径了。http这套上传机制可以保证一定安全性,如果你能验证上传的安全性,其他函数也可以胜任
4/我老是move不成功,后来几经调查,是新的路径没有权限,都是linux的安全性惹的祸。试过用chmod -R 777 www,发现其下upload没有获得777的权限。。。这是bug?重新对upload改权限即可
最后插入图片用绝对路径不行,要用相对路径,到现在我都不明白为什么
应该在上传处理php那里加入一些文件类型检测啊,文件头检测啊,禁止运行的限制,这些安全性或者功能限制等到有空我再补上来吧。
边栏推荐
- Basic syntax of VB
- Application of JDBC in performance test
- 操作系统面试题汇总(不定期更新)
- 十分之坑,tar命令解压文件的时候竟然不能解析英文括号“()”
- Tencent conference application market was officially launched, with more than 20 applications in the first batch
- 基于slate构建文档编辑器
- Lombok
- Source code analysis of redis ziplist compressed list
- CADD course learning (2) -- target crystal structure information
- 新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果
猜你喜欢

Growth summer challenge is coming, exclusive community welfare is coming ~ get CSDN customized T-shirt for free

6-1漏洞利用-FTP漏洞利用
Detailed steps for Django to upload excel tables and write data to the database

8 - function

VR全景中特效是如何编辑的?细节功能如何展示?

【Try to Hack】Windows系统账户安全

Summary of operating system interview questions (updated from time to time)

【论文阅读】Trajectory-guided Control Prediction for End-to-end Autonomous Driving: A Simple yet Strong Baseline

建立自己的网站(20)

GeoServer安装
随机推荐
Transport layer uses sliding window to realize flow control
RP prototype resource sharing - shopping app
Build document editor based on slate
CV+Deep Learning——网络架构Pytorch复现系列——basenets(BackBones)(一)
项目经理不应该犯的错误
标配10个安全气囊,奇瑞艾瑞泽8安全防护无死角
为什么数字化转型战略必须包括持续测试?
Filebeat custom indexes and fields
Kubernetes为什么会赢,容器圈的风云变幻!
开会,OneMeeting,OK!
Ten percent of the time, the tar command can't parse the English bracket "()" when decompressing the file
【ICCV 2019】特征超分检测:Towards Precise Supervision of Feature Super-Resolution for Small Object Detection
CADD course learning (1) -- basic knowledge of drug design
企业中台规划和IT架构微服务转型
exness:美GDP终值意外加速萎缩1.6%
项目经理面试常见问题及回答技巧
8 - function
小学期,第三场-下午:WEB_xxe
【LeetCode】【SQL】刷题笔记
Network planning | [five transport layers and six application layers] knowledge points and examples