欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php文件下传、上载

程序员文章站 2024-02-16 21:04:28
...
php文件上传、下载

以前一直在做java,最近转行去搞php开发,总觉得力不从心。这几天有个需求,是关于php的文件上传和下载的。给大家分享下自己的学习资料。

?

1.先来个请求页面upload.html

Administration - upload new files

Upload new news files

Upload this file:

?

2.php处理客户端请求的数据upload.html

Uploading...

Uploading file...

0) { echo 'Problem: '; switch ($_FILES['userfile']['error']) { case 1: echo 'File exceeded upload_max_filesize'; break; case 2: echo 'File exceeded max_file_size'; break; case 3: echo 'File only partially uploaded'; break; case 4: echo 'No file uploaded'; break; case 6: echo 'Cannot upload file: No temp directory specified.'; break; case 7: echo 'Upload failed: Cannot write to disk.'; break; } exit; } // Does the file have the right MIME type? if ($_FILES['userfile']['type'] != 'text/plain') { echo 'Problem: file is not plain text'; exit; } // put the file where we'd like it $upfile = '/uploads/'.$_FILES['userfile']['name']; if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile)) { echo 'Problem: Could not move file to destination directory'; exit; } } else { echo 'Problem: Possible file upload attack. Filename: '; echo $_FILES['userfile']['name']; exit; } echo 'File uploaded successfully

'; // reformat the file contents $fp = fopen($upfile, 'r'); $contents = fread ($fp, filesize ($upfile)); fclose ($fp); $contents = strip_tags($contents); $fp = fopen($upfile, 'w'); fwrite($fp, $contents); fclose($fp); // show what was uploaded echo 'Preview of uploaded file contents:

'; echo $contents; echo '

'; ?>

?

3.php文件下载

?总的来说,上面的3个代码段只是简单介绍了php文件的上传下载,还有很多问题要解决,就例如上传大文件的时候怎么处理,批量上传、大文件下载等等问题。当然跟java一样,php也有很多框架可以解决这方面。

php文件下传、上载

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频