PHP练习7 文件上传

本实验实现上传文件到服务器,并通过页面呈现目录下文件。

1. upload.html



  Administration - upload new files


Upload new news files

Upload this file:

PHP练习7 文件上传_第1张图片

2.  upload.php  服务器上创建/uploads目录,提交按钮上传文件保存在里面



  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

'; echo 'File name: '.$_FILES['userfile']['name']; ?>

 PHP练习7 文件上传_第2张图片

[root@mail uploads]# pwd
/uploads
[root@mail uploads]# ls -l
total 4
-rw-r--r-- 1 apache apache 12 Oct  8 22:42 file1.txt

 3. 使用dir类显示目录列表



  Browse Directories


Browsing

Handle is $dir->handle

"; echo "

Upload directory is $dir->path

"; echo '

Directory Listing:

    '; while(false !== ($file = $dir->read())) //strip out the two entries of . and .. if($file != "." && $file != "..") { echo ''.$file.'
    '; } echo '
'; $dir->close(); ?>

PHP练习7 文件上传_第3张图片

你可能感兴趣的:(PHP)