首页 > php代码 > php文件上传代码详细

php文件上传代码详细

注明:文件上传能否成功要看你的写文件目录是否可用,表单与post获取的名是不是相同,以及php教程.ini中上传文件大小是不是在你可写范围之内。

<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

php代码。

<?php

$store_dir='d:upload'; //文件上传后存储在服务器的路径
$uploadfile="$store_dir".basename($_files['sendfile']['name']); //上传文件的原始名字
$uploadfile_tmp=$_files['sendfile']['name_tmp']; //上传文件的临时名字
$err_msg=$_files['sendfile']['error']; //上传文件时产生的错误信息
if ($err_msg){
print "错误代码: $err_msg<br/>";
}
if (!is_writeable($store_dir)){ //检查上传的文件是否可写
print "$store_dir 目录不可写n";
exit;
}
else{
print "$store_dir 目录可写n";
}
if(isset($_files['sendfile'])) { 
if(is_uploaded_file($uploadfile_tmp)){ //检查上传的文件是否存在,如果存在则对其进行下一步操作
print "文件检验成功n";
}
else {
print "文件检验失败,可能遭受文件上传攻击!";
exit;
}
if (move_uploaded_file($uploadfile_tmp,$uploadfile)) { //对上传的合法文件,将其重命名并移动到服务器的上传文件夹中
print "文件上传成功n";
}
else{
print "移动文件失败,可能遭受文件上传攻击!";
exit;
}
print "文件上载成功!<br/>";
}
else{
print "文件上载失败!<br/>";
}

本文地址:http://www.phprm.com/code/37267.html

转载随意,但请附上文章地址:-)

标签:文件上传

相关文章

发表留言