php 图片上传源码下载[gif,jpg]

 更新时间:2016年11月25日 16:03  点击:1185
php 图片上传源码下载[gif,jpg]

<!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>

很多朋友都是用js来实现级联菜单了,那样有一个不好就是更新可能要修改源码,现在提供的这款ajax+php是一个实时的,不好之处在于耗费服务器资源了。

main.php
<script language="javascript" src="initajax.js"></script><!--InitAjax()-->
<script>
<!--

function getoption(select1,target)
{
    if(select1.value!=0){
        //select1是提交数据的来源的select菜单名
        var url = "getoption.php?pid="+select1.value;//取得xml的url
        //alert(url);
        var ajax = InitAjax();
        var i = 0;
        ajax.open("GET", url, true);
        ajax.onreadystatechange = function() {
            //如果执行是状态正常,那么就把返回的内容赋值给指定的地方
            if (ajax.readyState == 4 && ajax.status == 200) {
                var obj = ajax.responseXML;
                var properties = obj.getElementsByTagName("property");
                var name,value;
                target.options.length = 1;
                for (var i=0,x=1;i<properties.length;i++,x++) {
                    name = properties[i].getElementsByTagName("name")[0].firstChild.nodeValue;
                    value = properties[i].getElementsByTagName("value")[0].firstChild.nodeValue;
                    target.options[x] = new Option();
                    target.options[x].text = name;
                    target.options[x].value = value;
                }
            }
        }
        ajax.send(null);
    }
}


<form method="post" name="form" action="index.php">
<select name="level1" onchange="getoption(document.form.level1,document.form.level2);"/>
<option selected="ture" value="0">===选择===</option>
<?
if ($nrows>0){
    for ($i=0;$i<$nrows;$i++){
        echo "<option value="{$results[ID][$i]}">{$results[NAME][$i]}</option>";
    }
}
?>
</select>
<select name="level2" onchange="getoption(document.form.level2,document.form.level3);">
<option selected="ture" value="0">===选择===</option>
</select>

iniajax.js:
function InitAjax()
{
    var ajax=false;
    try {
        ajax = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            ajax = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            ajax = false;
        }
    }
    if (!ajax && typeof XMLHttpRequest!='undefined') {
        ajax = new XMLHttpRequest();
    }
    return ajax;
}

getoption.php:
<?php
if($pid=$_GET["pid"])
{
    header("Content-type: text/xml;charset=GB2312");
    echo "<?xml version="1.0" encoding="GB2312"?>";
    include("./oracle.inc");
    $sql="select * from AJAXTEST where PARENTID = $pid";
    //echo $sql;
    $stmt=ociparse($handle,$sql);
    ociexecute($stmt);
    ocicommit($handle);
    ocilogoff();
    $nrows=ocifetchstatement($stmt,$results);
    echo "<properties>";
    //echo "<row>{$nrows}</row>";
    for ($i=0;$i<$nrows;$i++){
    echo "<property>";
    echo "<value>{$results[ID][$i]}</value>";
    echo "<name>{$results[NAME][$i]}</name>";
    echo "</property>";
    }
    echo "</properties>";
}
?>

本代码的功能是把你要上传的图片保存到服务器,然后再把当前图重按比例生成一个小的缩略图哦。

 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);
  }
  }

我想很多站长都知道fck这个编辑器吧,fck是一个在线文档编辑器,上传的图片是不会保存到数据库中的,所以我们要想办法把它正则出来,下面是一个正则fck input图像域的代码。

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

[!--infotagslink--]

相关文章

  • 使用PHP+JavaScript将HTML页面转换为图片的实例分享

    这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Photoshop古装美女图片转为工笔画效果制作教程

    今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • 利用JS实现点击按钮后图片自动切换的简单方法

    下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
  • C#实现HTTP下载文件的方法

    这篇文章主要介绍了C#实现HTTP下载文件的方法,包括了HTTP通信的创建、本地文件的写入等,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮

    jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13
  • js实现上传图片及时预览

    这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
  • Photoshop枪战电影海报图片制作教程

    Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
  • php实现文件下载实例分享

    举一个案例:复制代码 代码如下:<?phpclass Downfile { function downserver($file_name){$file_path = "./img/".$file_name;//转码,文件名转为gb2312解决中文乱码$file_name = iconv("utf-8","gb2312",$file_name...2014-06-07
  • python opencv通过4坐标剪裁图片

    图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
  • 使用PHP下载CSS文件中的图片的代码

    共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
  • Visual Studio 2015下载和安装图文教程

    这篇文章主要为大家详细介绍了Visual Studio 2015下载和安装图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • PHP swfupload图片上传的实例代码

    PHP代码如下:复制代码 代码如下:if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { $upload_file = $_FILES['Filedata']; $fil...2013-10-04
  • ps怎么制作图片阴影效果

    ps软件是现在很多人比较喜欢的,有着非常不错的使用效果,这次文章就给大家介绍下ps怎么制作图片阴影效果,还不知道制作方法的赶紧来看看。 ps图片阴影效果怎么做方法/...2017-07-06
  • C#中图片旋转和翻转(RotateFlipType)用法分析

    这篇文章主要介绍了C#中图片旋转和翻转(RotateFlipType)用法,实例分析了C#图片旋转及翻转Image.RotateFlip方法属性的常用设置技巧,需要的朋友可以参考下...2020-06-25
  • OpenCV如何去除图片中的阴影的实现

    这篇文章主要介绍了OpenCV如何去除图片中的阴影的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-29
  • JavaScript 如何禁止用户保存图片

    这篇文章主要介绍了JavaScript 如何禁止用户保存图片,帮助大家完成需求,更好的理解和使用JavaScript,感兴趣的朋友可以了解下...2020-11-19