php 图片上传并生成缩略图代码
if($_FILES['image']['size']){
if($_FILES['image']['type'] == "image/pjpeg"){
$im = @imagecreatefromjpeg($_FILES['image']['tmp_name']);
$n_bmp.='.jpg';
}elseif($_FILES['image']['type'] == "image/x-png"){
$im = @imagecreatefrompng($_FILES['image']['tmp_name']);
$n_bmp.='.png';
}elseif($_FILES['image']['type'] == "image/gif"){
$im = @imagecreatefromgif($_FILES['image']['tmp_name']);
$n_bmp.='.gif';
}
ResizeImage($im,8888,8888,$n_bmp);
ImageDestroy ($im);
$n_tag =1;
}
function Uploadfile($str){
$save_path = './product/';//文件保存目录路径
$ext_arr = array('rar','zip','jpg','gif','png','bmp');//定义允许上传的文件扩展名
$max_size = 1000000;//最大文件大小
$file_rand ='';
$file_ext ='';
@mkdir($save_path, 0777); //更改目录权限
if ($_FILES[$str]['name']) {//有上传文件时
$file_name = $_FILES[$str]['name'];//原文件名
$tmp_name = $_FILES[$str]['tmp_name'];//服务器上临时文件名
$file_size = $_FILES[$str]['size'];//文件大小
if (@is_dir($save_path) === false) {//检查目录
alert("上传目录不存在。",'');
}
if (@is_writable($save_path) === false) {//检查目录写权限
alert("上传目录没有写权限。",'');
}
if (@is_uploaded_file($tmp_name) === false) {//检查是否已上传
alert("临时文件可能不是上传文件。",'');
}
if ($file_size > $max_size) {//检查文件大小
alert("上传文件大小超过限制。",'');
}
$temp_arr = explode(".", $file_name);//获得文件扩展名
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
$file_rand= md5(date("Y-m-d"));
if (in_array($file_ext, $ext_arr) === false) {//检查扩展名
alert("上传文件扩展名是不允许的扩展名。",'');
}
if (move_uploaded_file($tmp_name, $save_path.$file_rand.'.'.$file_ext) === false) {//移动文件
alert("上传文件失败。",'');
}
}
return $save_path.$file_rand.'.'.$file_ext;
}
function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,'./product_e/'.$name);
ImageDestroy ($newim);
}else{
ImageJpeg ($im,'./product_e/'.$name);
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
color: #FFFFFF;
}
body {
background-color: #999;
margin-left: 0px;
margin-top: 0px;
}
a:link {
color: #FFFFFF;
}
a:visited {
color: #FFFFFF;
}
a:active {
color: #FFFFFF;
}
-->
</style></head>
<body>
<?php
$dest_folder = "/picture/";
if(!file_exists($dest_folder)){
mkdir($dest_folder);
}
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$houzhui=substr($_FILES["pictures"]["name"][$key],-4);
if ($_FILES["pictures"]["size"][$key]<=1024*100) //100K
{
if ($houzhui==".jpg" or $houzhui==".gif")
{
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = date("ymdhs")."_".mt_rand(10000,99999).$houzhui;
$uploadfile = $dest_folder.$name;
move_uploaded_file($tmp_name, $uploadfile);
echo "<script>
parent.img.value="".$uploadfile."";
parent.sm1.disabled=false;
</script>
<div align=center>贴图上传成功。[<a href=upload.htm onclick="parent.sm1.disabled=true;">重新上传</a>]</div>";
}
else
echo "<script>alert('对不起,您上传文件格式不对!请上传.gif或.jpg格式的图片文件!');history.go(-1);</script>";
}
else
echo "<script>alert('对不起,您上传的文件太大了!请上传文件大小小于100k的图片文件!');history.go(-1);</script>";
}
}
?>
</body>
</html>
function get_img($str){
$dpath = 'images/img.jpg';
preg_match('/<input [^>]*>/im',stripslashes(stripslashes($str)), $arr);
preg_match('/src=(.+?)"/im', $arr[0], $arra);
if( strlen(@$arra[1])>10 ){
$dpath = str_replace(""","",@$arra[1]);
}
return "<img src='$dpath' width='163' height='113' />";
}
本站原创转载注明: www.111cn.net
php 文件重命名 rename我们这里是把文件重命名了,就是用到了rename函数哦,好了费话我也不说多了我们来看看这个读取目录文件然后给文件命名的代码吧。
function CheckDir($dir_path)
{
if(is_dir($dir_path))
{
if($dir_file=opendir($dir_path))
{
while(($dir_list=readdir($dir_file))!==false)
{
if($dir_list!="." && $dir_list!="..")
{
rename($dir_path.$dir_list,$dir_path.$dir_list.'.txt');
}
}
}
else
{
echo($dir_path."<br/>");
}
}
else
{
echo($dir_path."<br/>");
}
}
本站原创转载注明www.111cn.net
php 读取文件内容并保存到数据库本程序的功能是读取目录下的txt 文件,然后保存到数据哦,就这么简单,下面我们就来看看我是如何把txt文件内容保存到mysql数据表的吧。
<?php
include('../boke/inc/re_conn.php');
CheckDir('file/');
function CheckDir($dir_path)//遍历目录
{
if(is_dir($dir_path))
{
if($dir_file=opendir($dir_path))
{
while(($dir_list=readdir($dir_file))!==false)
{
if($dir_list!="." && $dir_list!="..")
{
$title =substr($dir_list,6,strlen($dir_list)-4);
//echo $title;
openFile($dir_path.$dir_list,$title);
}
}
}
else
{
echo($dir_path."<br/>");
}
}
else
{
echo($dir_path."<br/>");
}
}
function savaDate($title,$content)//保存数据
{
$title = str_replace('.txt','',$title);
echo $title;
$content = addslashes($content);
$sql ="insert into fil(title,content,siteindex,filesendid,systype,z_type) values('$title','$content',1,'admin',2,31)";
if( mysql_query( $sql ) )
{
echo 'success<br>';
}
else
{
echo mysql_error();
}
}
function openFile($filePath,$title)//读取文件内容提要
{
if( is_file( $filePath ) )
{
$content = file_get_contents( $filePath );
if( !empty( $content ) )
{
savaDate($title,$content);
}
else
{
;
}
}
}
?>
本站原创转载注明: http://www.111cn.net
相关文章
- PHP代码如下:复制代码 代码如下:if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { $upload_file = $_FILES['Filedata']; $fil...2013-10-04
- 本案例非通用,仅作笔记以备用 修改后的结果是 百度编辑器里上传的图片路径为/d/file/upload1...2014-07-03
- 这篇文章主要介绍了Java实现将图片上传到webapp路径下 路径获取方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-11-12
- 这篇文章主要介绍了c#生成高清缩略图的二个示例,需要的朋友可以参考下...2020-06-25
- 这款批量生成缩略图代码可以生成指定大小的小图哦,并且支持文件批量上传。 这款教程会用到php文件 view.php config.php funs.php index.php 功能: -------...2016-11-25
- 这篇文章主要介绍了C#实现为一张大尺寸图片创建缩略图的方法,涉及C#创建缩略图的相关图片操作技巧,需要的朋友可以参考下...2020-06-25
- upload.php复制代码 代码如下:<?phpif(isset($_FILES["myfile"])){$ret = array();$uploadDir = 'images'.DIRECTORY_SEPARATOR.date("Ymd").DIRECTORY_SEPARATOR;$dir = dirname(__FILE__).DIRECTORY_SEPARATOR.$upl...2014-06-07
- if( isset($_FILES['upImg']) ) { if( $userGroup[$loginArr['group']]['upload'] == 0 ) { echo '{"error":"您所在的用户组无权上传图片!"}'; } else...2016-11-25
ASP.NET百度Ueditor编辑器实现上传图片添加水印效果
这篇文章主要给大家介绍了ASP.NET百度Ueditor编辑器1.4.3这个版本实现上传图片添加水印效果的相关资料,文中通过图文及示例代码介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。...2021-09-22- 这篇文章主要介绍了Yii框架实现图片上传的方法,结合实例形式较为详细的分析了Yii框架实现图片上传功能的具体步骤与相关操作技巧,需要的朋友可以参考下 本文实例...2017-07-06
- 这款文件上传实用代码,可以方便的上传你指定的文件或图片,同时也可以快速的限制上传图片文件类或大小。 /* * created on 2010-6-21 * * the class for image...2016-11-25
- 这篇文章主要给大家介绍了在CentOS系统下编译安装nginx及配置缩略图插件的方法教程,文中给出了详细的安装步骤,对大家具有一定的参考价值,有需要的朋友们下面来一起看看吧。...2017-07-06
- 今天写了一个上传多张图片并生成缩略图的小程序。当然因为是菜鸟,所以写的一般。但还是学到了不少东西。现在上代码。...2021-09-22
- 这篇文章主要介绍了Nginx配合php实现生成实时缩略图功能,这在一些特殊场合可能会要用到,需要的朋友可以参考下...2016-01-27
- 对于生成缩略图一般做法是通过后端语言php等来生成,但是为了给服务器减压,我们或许可以从前端来着手,先生成好不同尺寸的缩略图,传给后端,而后端只需要将前端传过来的图片进行存储就好了...2020-10-03
- 这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能哦,可以说是一款完美的图片上传类哦。 代码如下 复制代码 ...2016-11-25
- 多文件上传其实就包括了图片及各种文件了,下面介绍的是一款PHP多文件上传类,一共两个文件,upp.php 和 uploadFile.php,upp.php,这是前台所显示的表单文件了,默认的是四个...2016-11-25
php discuz chhome 图片上传swfupload功能
php discuz chhome 图片上传swfupload功能 这上传与discuz来比, 还相差太远. 功能也欠缺. 除了部分内置的url引向,我们改不了之外, 其它的数据都是可以修改的. <?...2016-11-25- 生成jpg缩略图字节,本人的小软件中需要用到的功能,所以自己做了一个函数,和大家分享 为什么要生成字节而不是文件,这是为了方便后续处理啦^_^...2020-06-25
- 分享一个网友写的php图片上传类,支持加水印,生成略缩图功能哦,面是配置和可以获取的一些信息(每一个配置信息都有默认值,如无特殊需要,可以不配置): 代码如下 ...2016-11-25