Upload program code based on jQuery Ajax PHP File

Source: Internet
Author: User
Tags tmp file

Upload program code based on jQuery Ajax PHP file this is a jQuery-based PHP multi-File Upload program instance. It uses the ajaxmultifileupoad plug-in to upload instance files with upload progress bars.

File Upload program code based on jquery ajax php tutorial
This is a jquery-based php multi-File Upload program instance. It uses the ajaxmultifileupoad plug-in to upload files to the instance, with an upload progress bar.

<! 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.bKjia. c0m </title>

<Link href = "css tutorial/style.css" rel = "stylesheet" type = "text/css"/>
<Script type = "text/Webpage effects" 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 code

<? 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 )){
$ Pstutorial 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 encoded 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 ['original']);
// 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 ['name name']. "</span> deleted .";
Else echo "problem deleting file". $ _ file .".";
}



/*
File

Http://down.bKjia. c0m/php/2010/0901/20569.html
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.