1 Star 0 Fork 0

ica / lyb_0

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
upload.php 5.15 KB
一键复制 编辑 原始数据 按行查看 历史
Moe-boshi 提交于 2018-06-18 22:44 . 第一次提交
<?php
session_start();
require_once './core/conn.php';
// 允许上传的图片后缀
$allowedExts = array(
"gif",
"jpeg",
"jpg",
"png"
);
$temp = explode(".", $_FILES["upImg"]["name"]);
echo $_FILES["upImg"]["size"];
$extension = end($temp); // 获取文件后缀名
if ((($_FILES["upImg"]["type"] == "image/gif") || ($_FILES["upImg"]["type"] == "image/jpeg") || ($_FILES["upImg"]["type"] == "image/jpg") || ($_FILES["upImg"]["type"] == "image/pjpeg") || ($_FILES["upImg"]["type"] == "image/x-png") || ($_FILES["upImg"]["type"] == "image/png")) && ($_FILES["upImg"]["size"] < 204800) && // 小于 200 kb
in_array($extension, $allowedExts)) {
if ($_FILES["upImg"]["error"] > 0) {
echo "错误:: " . $_FILES["upImg"]["error"] . "<br>";
} else {
echo "上传文件名: " . $_FILES["upImg"]["name"] . "<br>";
echo "文件类型: " . $_FILES["upImg"]["type"] . "<br>";
echo "文件大小: " . ($_FILES["upImg"]["size"] / 1024) . " kB<br>";
echo "文件临时存储的位置: " . $_FILES["upImg"]["tmp_name"] . "<br>";
$fileName = $_FILES['upImg']['name']; // 得到上传文件的名字
$name = explode('.', $fileName); // 将文件名以'.'分割得到后缀名,得到一个数组
$newPath = $_SESSION['uid'] . '.' . $name[1];
move_uploaded_file($_FILES["upImg"]["tmp_name"], "himg/" . $newPath);
echo "文件存储在: " . "himg/" . $newPath;
$himg_url = "himg/" . $newPath;
$update_sql = "UPDATE user SET himg = '$himg_url' WHERE id = $_SESSION[uid]";
if (mysqli_query($conn, $update_sql)) {
$_SESSION['himg'] = $himg_url;
exit(json_decode("ok"));
} else {
exit(mysqli_error());
}
}
} else {
echo "非法的文件格式";
}
/*
* $php_path = dirname(__FILE__) . '/';
* $php_url = dirname($_SERVER['PHP_SELF']) . '/upload';
* //文件保存目录路径
* $save_path = $php_path . './upload/face/';//默认为 upload.php所在目录
* //文件保存目录URL
* $save_url = $php_url . './';//默认为 upload.php所在目录
* //定义允许上传的文件扩展名
* $ext_arr = array("gif", "jpg", "jpeg", "png", "bmp");
* //最大文件大小
* $max_size = 1024*10000;//(默认1M)
*
* $save_path = realpath($save_path) . '/';
* //有上传文件时
* if (empty($_FILES) === false) {
* //原文件名
* $file_name = $_FILES['uppic']['name'];
* //服务器上临时文件名
* $tmp_name = $_FILES['uppic']['tmp_name'];
* //文件大小
* $file_size = $_FILES['uppic']['size'];
* //错误类型
* $file_error = $_FILES['uppic']['error'];
*
* //检查错误类型 0:文件上传成功。1:超过了文件大小php.ini中即系统设定的大小。2:超过了文件大小
* if ($file_error>'0') {
* exit("返回错误: 上传文件($file_name)大小超过限制。最大".($max_size/1024)."KB");
* }
*
* //检查文件名
* if (!$file_name) {
* exit("返回错误: 请选择文件。");
* }
* //检查目录
* if (@is_dir($save_path) === false) {
* exit("返回错误: 上传目录不存在。($save_path)");
* }
* //检查目录写权限
* if (@is_writable($save_path) === false) {
* exit("返回错误: 上传目录没有写权限。($save_url)");
* }
* //检查是否已上传
* if (@is_uploaded_file($tmp_name) === false) {
* exit("返回错误: 临时文件可能不是上传文件。($file_name)($tmp_name)");
* }
* //检查文件大小
* if ($file_size > $max_size) {
* exit("返回错误: 上传文件($file_name)大小超过限制。最大".($max_size/1024)."KB");
* }
* $temp_arr = explode(".", $file_name);
* $file_ext = array_pop($temp_arr);
* $file_ext = trim($file_ext);
* $file_ext = strtolower($file_ext);
* if (in_array($file_ext, $ext_arr) === false) {
* exit("返回错误: 上传文件扩展名是不允许的扩展名。");
* }
*
* // echo "上传的文件: " . $file_name . "<br />";
* // echo "文件类型: " . $file_ext . "<br />";
* // echo "文件大小: " . ($file_size / 1024) . " Kb<br />";
* // echo "临时文件: " . $tmp_name . "<br />";
*
* //创建文件夹
* if(!file_exists($save_path)){
* mkdir($save_path);
* }
* //新文件名
* //$new_file_name = $_POST['fid'] . '.' . $file_ext;
* $new_file_name = $_POST['fid'] . '.jpg';
* //移动文件
* $file_path = $save_path . $new_file_name;
* @chmod($file_path, 0644);//修改目录权限(Linux)
* if (move_uploaded_file($tmp_name, $file_path) === false) {//开始移动
* echo "图片上传失败";
* exit;
* }
* else{
* echo "图片上传成功"."<br><script>window.close();</script>";
* $file_url = $save_url . $new_file_name;
* $fileName = uniqid('image',true);
* }
* }
*
*
* //调整上传图片的大小
* $width=150;
* $height=150;
* $size=getimagesize($file_path);
* if($size[2]==1)
* $im_in=imagecreatefromgif($file_path);
* if($size[2]==2)
* $im_in=imagecreatefromjpeg($file_path);
* if($size[2]==3)
* $im_in=imagecreatefrompng($file_path);
* $im_out=imagecreatetruecolor($width,$height);
* imagecopyresampled($im_out,$im_in,0,0,0,0,$width,$height,$size[0],$size[1]);
* imagejpeg($im_out,$file_path);
* chmod($file_path,0777);
* imagedestroy($im_in);
* imagedestroy($im_out);
* ?>
*/
?>
PHP
1
https://gitee.com/ica520/lyb_0.git
git@gitee.com:ica520/lyb_0.git
ica520
lyb_0
lyb_0
master

搜索帮助