Based on jquery Ajax php file upload program code

Source: Internet
Author: User
Tags php file php file upload tmp file

<!doctype HTML Public "-//W3C//DTD XHTML 1.0 transitional//en"
        Http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd ""
    <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/web 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>

<body>
<div id= "Maincol" ></div>
</body>

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)) {
$ps Tutorial 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 '], "re Name "=> $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. ".";
}



/*
File Download Address

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

Related Article

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.