Home > Backend Development > PHP Tutorial > php file upload

php file upload

WBOY
Release: 2016-07-29 09:13:13
Original
1171 people have browsed it

Write a form file upload.html:



    <meta charset="UTF-8">
    <title>上传新文件</title>


<h1>上传新文件</h1>
Copy after login

File to process the form: upload.php



    <meta charset="UTF-8">
    <title><strong>上传文件</strong></title>


<?php if($_FILES[&#39;userfile&#39;][&#39;error&#39;]>0){
    echo 'Problem:';
    switch ($_FILES['userfile']['error']){
        case 1:echo '文件超过 upload_max_filesize';
            break;
        case 2:echo '文件超过max_file_size';
            break;
        case 3:echo '文件只上传了一部分';
            break;
        case 4:echo '没有文件上传';
            break;
        case 6:echo '不能<strong>上传文件</strong>:没有指定临时目录';
            break;
        case 7:echo '<strong>上传文件</strong>失败:不能写入到磁盘';
            break;
    }
    exit;
}
//判断文件是不是正确的MIME格式
if($_FILES['userfile']['type'] !='text/plain'){
    echo 'Problem:文件不是plain text';
    exit;
}

//将文件放到指定的地方
$upfile='uploads/'.$_FILES['userfile']['name'];
if(is_uploaded_file($_FILES['userfile']['tmp_name'])){
    if(!move_uploaded_file($_FILES['userfile']['tmp_name'],$upfile)){
        echo 'Problem:不能移动文件到指定的文件夹';
        exit;
    }
}else{
    echo 'Problem:文件可能受到影响。文件名:';
    echo $_FILES['userfile']['name'];
    exit;
}
 echo '文件上传成功<br><br>';

$c
$c
file_put_contents($_FILES['userfile']['name'],$contents);

echo '<p>浏览上传的文件的内容:<br></p><hr>';
echo nl2br($contents);
echo '<br><hr>';
?>

Copy after login
php file upload

The above has introduced PHP file upload, including the content of uploading files. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template