基于jQuery Ajax PHP文件上传程序代码

<!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=gbk"/>
    <title>ajax file upload download by http://www.111cn.net</title>

 <link href="css/style.css" rel="stylesheet" type="text/css"/>
 <script type="text/网页特效" src="js/jquery-1.2.6.min.js"></script>
 <script type="text/javascript" src="js/jquery.livequery.min.js"></script>
 <script type="text/javascript" src="js/jquery.ajaxmultifileupload.js"></script>
 <script type="text/javascript">
 $(document).ready(function(){ 
  $("#maincol").ajaxmultifileupload();
 }); 
 </script>
</head>

<body>
 <div id="maincol"></div>
</body>
</html>

//php代码

<?php

function _resize($_w, $max_w, $_h, $max_h) {
 global $new_w, $new_h;
 
 if ($_w>$max_w || $_h>$max_h) {
  $x_ratio = $max_w / $_w;
  $y_ratio = $max_h / $_h;
  if (($x_ratio * $_h) < $max_h) {
   $new_h = ceil($x_ratio * $_h);
   $new_w =$max_w;
  }
  else {
   $new_w = ceil($y_ratio * $_w);
   $new_h=$max_h;
  }
 }
 else {
  $new_w = $_w;
  $new_h=$_h;
 }
}

if ($_files) {
 foreach ($_files as $f) {
   // get actual image type
   $ext = strtolower(str_replace("image/", "", image_type_to_mime_type(exif_imagetype($f['tmp_name']))));
   // get image type from file extension
   $ext_file = strtolower(str_replace("image/", "", $f['type']));
   // adjustment for ie mime types
   $ext_adjust = array("pjpeg" => "jpeg", "x-png" => "png");
   $ext_file = (array_key_exists($ext_file, $ext_adjust)) ? $ext_adjust[$ext_file] : $ext_file;
   
   // check if file actual and ext file types match
   if ( ($ext_file != $ext) )  {
    $ps教程t = array("problem" => array("name" =>$f['name'], "ext" => $ext, "ext_actual" => $ext_file));
   }
   else {
    
    $stat = stat($f['tmp_name']);
    $renamed = ($_post['mode']=='demo') ? $stat[9].'.'. substr(strrchr($f['name'], "."),1) : $f['name'];
    
    // set path for upload directory
    $f['_upload'] = $_server['document_root'] . $_post['upload'] . $renamed;
    // set path for image thumbnail directory
    $f['_thumb'] = $_server['document_root'] . $_post['thumb'] . $renamed;
    $f['src'] = "thumb/" . $renamed;
    
    chmod($f['tmp_name'], 0755);
    
    // get image size - requires gd library
    list($width, $height) = getimagesize($f['tmp_name']);
      
    $max_width = 120;
    $max_height = 60;
    _resize($width, $max_width, $height, $max_height);

    // create thumbnail with image_class (files included in download)
    include_once('imageclass/image_class.inc');
    set_time_limit(140);
    try{
     // create new image from posted $_files
     $image=& new image($f['tmp_name']);
     $image->resize(round($new_w),round($new_h));
     // save image to thumbnail directory
     $image->saveimage($f['_thumb'],$image->getimagedata());
     $image->clean();
     // move actual posted $_files to upload directory
     $moved = move_uploaded_file($f['tmp_name'], $f['_upload']);
     if ($moved) $pst = array ("img" => array("alt" => $f['_thumb'], "src" => $f['src'], "name" => $f['name'],  "rename" => $renamed, "width" => $new_w, "height" => $new_h));
    }
    catch(exception $e){
        $pst = array ("problem" => array("name" =>$f['name'], "error" => $e->__tostring()) );
    }    
   }
   echo "var pst = " . json_encode($pst);
 }
}
else if ($_post['deletefile']) {
 $_file = ($_post['mode']=='demo') ? urldecode($_post['deletefile']) : urldecode($_post['origname']);
 // delete tmp file
 $_unlink_upload = unlink($_server['document_root'] . $_post['upload'] . $_file);
 // delete thumb file
 $_unlink_thumb = unlink($_server['document_root'] . $_post['thumb']. $_file);
 
 if ($_unlink_thumb && $_unlink_upload) echo "file <span>" . $_post['origname'] . "</span> deleted.";
 else echo "problem deleting file " . $_file . ".";
}

   
/*
文件下载地址

http://down.111cn.net/php/2010/0901/20569.html
*/

时间: 2024-08-01 16:48:32

基于jQuery Ajax PHP文件上传程序代码的相关文章

基于HTML5 Ajax实现文件上传并显示进度条_AJAX相关

本文实例讲解了ajax上传文件及进度条的实现方法,分享给大家供大家参考,具体内容如下 效果图: html5上传是同步上传的方式,所以能够实现进度条的显示.1.上传文件: 首先我们用ajax来取得<input type="file" id="file_upload">的file对象: var file = null; var input = $("#file_upload"); //文件域选择文件时, 执行readFile函数 inpu

基于jQuery 20款文件上传插件介绍

文件上传是网站很常见的功能之一,通过使用 jQuery 可以让上传过程更加人性化,更好的用户体验.本文介绍20个jQuery的文件上传插件,其中有一些是教程. 1. Plupload Plupload 是一个Web浏览器上的界面友好的文件上传模块,可显示上传进度.图像自动缩略和上传分块.可同时上传多个文件. 2. The KillersAjax Upload 该插件使用 XHR 用于上传多个文件,支持上传进度显示,但不支持 IE 3. SWFUpload jQuery Plugin 4. php

基于HTML5 Ajax实现文件上传并显示进度条

本文实例讲解了ajax上传文件及进度条的实现方法,分享给大家供大家参考,具体内容如下 效果图: html5上传是同步上传的方式,所以能够实现进度条的显示. 1.上传文件: 首先我们用ajax来取得<input type="file" id="file_upload">的file对象: var file = null; var input = $("#file_upload"); //文件域选择文件时, 执行readFile函数 inp

php中实现图片文件上传程序代码

我们先来看一下项目结构图与数据库结构图吧 项目结构: 运行效果:   up.html 简单的上传表单文件  代码如下 复制代码 <form action="up.php" enctype="multipart/form-data" method="post"      name="uploadfile">上传文件:<input type="file" name="upfile&q

实现ASP.NET多文件上传程序代码_实用技巧

upload.aspx 复制代码 代码如下: <%@ Page language="c#" Codebehind="UpLoad.aspx.cs" AutoEventWireup="false" Inherits="WebPortal.Upload" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

php无刷新文件上传程序代码

在一个网站项目中,为了得到更好的用户体验,很多地方都要处理成异步无刷新的效果.此文件上传范例是利用一个隐藏的框架iframe做桥梁,实现主体页面上传文件无刷新的效果. 其实本范例也不是真正的异步效果,但是也是可以达到无刷新效果的.而且这种方法是实现php无刷新上传文件最快捷,最简单的方法,所以何乐而不为呢. html代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.

php 实现多文件上传程序代码

最简单的实例如下  代码如下 复制代码 <form action="" method="post" enctype="multipart/form-data"> <p>Pictures: <input type="file" name="pictures[]" /> <input type="file" name="pictures[]

struts2 文件上传程序代码

简介 由于今天做个一个项目有关文件上传内容,业务逻辑是这样的,选择一个文件上传到服务器,然后把文件名称,上传时间,上传用户以及备注存储到服务器,并且提供一个web页面进行管理. 开发环境: 架构:使用struts2+mybatis框架,以及前台使用easyUI框架进行开发. 数据库:mysql 前台html代码: 注意: 在编写form 表单的时候一定要注意加上这句话 ==enctype="multipart/form-data"==,声明此表单可以上传文件,在开发中很容易遗漏,以至于

jQuery插件AjaxFileUpload文件上传实现Javascript多文件上传功能

Ajax file upload plugin是一个功能强大的文件上传jQuery插件,可自定义链接.或其它元素庖代传统的file表单上传结果,可实现Ajax动态提示文件上传 过程,同时支撑多文件上传,AjaxFileUpload文件上传插件功能斗劲稳定,今朝应用也斗劲多,简单应用实例如下: 查看AjaxFileUpload相干jQuery官方文档介绍 AjaxFileUpload JS库文件 查看AjaxFileUpload演示 一,创建一个ajax upload按钮元素(button),