写一个表单文件upload.html:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>上传新文件</title>
</head>
<body>
<h1>上传新文件</h1>
<form action="upload.php" method="post" enctype="multipart/form-data"/>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
<label for="userfile">上传一个文件:</label><input type="file" name="userfile" id="userfile"/>
<input type="submit" value="<strong>上传文件</strong>"/>
</div>
</form>
</body>
</html><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><strong>上传文件</strong></title>
</head>
<body>
<?php
if($_FILES['userfile']['error']>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/><hr/></p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/1500">
<img src="https://img.php.cn/upload/ai_manual/000/969/633/68b7a441772f6218.png" alt="Cutout老照片上色">
</a>
<div class="aritcle_card_info">
<a href="/ai/1500">Cutout老照片上色</a>
<p>Cutout.Pro推出的黑白图片上色</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="Cutout老照片上色">
<span>20</span>
</div>
</div>
<a href="/ai/1500" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="Cutout老照片上色">
</a>
</div>
';
echo nl2br($contents);
echo '<br/><hr/>';
?>
</body>
</html>
以上就介绍了php之文件上传,包括了上传文件方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号