|
本文分享一段php上传图片的代码,通过内置的php函数实现文件上传,有需要的朋友参考下。
1,form表单部分 <!-- Form Area --> <form enctype="multipart/form-data" action="uploader.php" method="post"> Select Image: <input type="file" name="userfile"> <input type="submit" value="Upload!"> </form> <!-- Form Area --> 登录后复制 2,上传图片文件的php代码
<?php
# Variables
$path = "images/";
$max_size = "200000";
# File
$filename = $_POST['userfile'];
# Control
if (!isset($HTTP_POST_FILES['userfile'])) exit;
if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
if ($HTTP_POST_FILES['userfile']['size']>$max_size) {
echo "文件太大,超过了上传文件的最大限制。The Max File Size is $max_size KB<br>n";
exit;
}
# Type Control
if (
($HTTP_POST_FILES['userfile']['type']=="image/gif") ||
($HTTP_POST_FILES['userfile']['type']=="image/jpg") ||
($HTTP_POST_FILES['userfile']['type']=="image/bmp") ||
($HTTP_POST_FILES['userfile']['type']=="image/png") ||
($HTTP_POST_FILES['userfile']['type']=="image/jpeg")
)
{
# If File Exist
if (file_exists($path . $HTTP_POST_FILES['userfile']['name']))
{
echo "同名的文件已存在。<br>";
exit;
}
$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
if (!$res){
echo "上传失败!<br>";
exit;
}
else{
echo "上传成功!<br>";
}
echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>";
echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>";
echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>";
echo "View Image";
}
else
{
echo "错误的文件类型<br>";
exit;
}
}
?>登录后复制 注意: 在php5以后的代码,已经不再使用这样的方式,改用全局变量$_FILE来接收上传数据了。 |
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号