php file Upload, download
In the past has been doing Java, the recent career to engage in PHP development, always feel powerless. These days there is a need for PHP file upload and download. To share their own learning materials.
?
1. First request page upload.html
Administration-upload New filesUpload New News Files
?
2.php processing the data requested by the client upload.html
<title>Uploading ...</title>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 has 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 '], $ 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 is uploaded echo ' Preview of uploaded file contents:
'; Echo $contents; Echo '
';? >
?
3.php File Download
In general, the above 3 code snippets simply introduced the php file upload download, there are many problems to solve, such as uploading large files when how to deal with, batch upload, large file download and so on. Of course, like Java, PHP also has a lot of frameworks to solve this.