Php image upload and preview results _ PHP Tutorial

Source: Internet
Author: User
Upload and preview php images. Php image upload and preview effect this article is an image upload code that allows you to preview and modify the image uploaded to the server. if you want to, you can save it, this article is an image upload code. you can preview and modify the image uploaded to the server, if you want it, you can save it, instead of deleting it and uploading it again.

Php Tutorial image upload and preview
This article is an image upload code. you can preview and modify the image uploaded to the server. if you want to save it, you cannot delete the file and upload it again.
*/
If (! Isset ($ _ session) session_start ();

/** 2010-6-22
* $ The data array type contains the following variables:
-------------------------------
* $ Sub_type submit type (upload: upload button delete: delete button), default upload
* $ File the $ _ files ['filename'] array obtained through the form
* $ Img_tag_id id of the preview image
* $ Img_dir directory for uploading images
* $ Max_file_size unit: m (MB); default value: 1 m
* $ Type_array: Supported image types (default: image/pjpeg, image/jpeg, image/gif)
**/
Function upload ($ data ){
If (! $ Data ['file']) {
Echo 'script alert ("file cannot be blank! "); Script '; exit;
}
If (! $ Data ['IMG _ tag_id ']) {
Echo 'script alert ("preview image tag id cannot be blank! "); Script '; exit;
}
If (! $ Data ['IMG _ dir']) {
Echo 'script alert ("the image upload directory cannot be blank! "); Script '; exit;
}
If (! Isset ($ data ['max _ file_size ']) {
$ Data ['max _ file_size '] = 1024*1024;
} Else {
$ Data ['max _ file_size '] = $ data ['max _ file_size'] * 1024*1024;
}
If (! Isset ($ data ['type _ array']) {
$ Data ['type _ array'] = array ('image/pjpeg ', 'image/jpeg', 'image/GIF ');
}
If (! Isset ($ data ['sub _ type']) {
$ Data ['sub _ type'] = 'upload ';
}

$ Imgpath = '';
If (isset ($ data ['sub _ type']) & $ data ['sub _ type'] = 'delete '){
If (isset ($ _ session ['name']) & $ _ session ['name']) {
If (is_file ($ _ session ['imgpath']) {
$ B = unlink ($ _ session ['imgpath']);
}
Unset ($ _ session ['name'], $ _ session ['imgpath']);
If (! Isset ($ _ session ['name']) {
Echo 'script alert ("deleted successfully! "); Script ';
Echo '{script}parent.doc ument. getelementbyid ("'. $ data ['IMG _ tag_id']. '"). style. display = "none"; script';
} Else {
Echo 'script alert ("deletion failed! "); Script ';
}
} Else {
Echo 'script alert ("no manuscript! "); Script ';
} Exit;
}

If (isset ($ _ session ['imgpath']) & $ _ session ['imgpath']) {
Echo 'script alert ("The manuscript already exists. to upload it again, delete the original manuscript! "); Script '; exit;
}
If (! In_array ($ data ['file'] ['type'], $ data ['type _ array']) {
Echo 'script alert ("There are no matching manuscripts. please Upload .jpg).gif and .png images! "); Script '; exit;
}
If ($ data ['file'] ['size']> $ data ['max _ file_size ']) {
Echo 'script alert ("The manuscript you uploaded is too large. please upload an image below 2 MB! "); Script '; exit;
}

If (! Is_dir ($ data ['IMG _ dir']) {
@ Mkdir ($ data ['IMG _ dir'], 0777, true );
}
$ Imgpath = $ data ['IMG _ dir']. '/'. date ('his ', time ()). rand (100,999 ). $ data ['file'] ['name'];
$ Isupload = move_uploaded_file ($ data ['file'] ['tmp _ name'], $ imgpath );
If (! $ Isupload ){
Echo 'script alert ("manuscript Upload failed. please try again! "); Script '; exit;
} Else {
Echo 'script alert ("manuscript uploaded successfully! "); Script ';
}
$ _ Session ['name'] = $ data ['file'] ['name'];
$ _ Session ['imgpath'] = $ imgpath;

Return $ imgpath;
}

/* Test_start */
$ Sub_type = '';
If (isset ($ _ post ['submit _ upload']) $ sub_type = 'upload ';
If (isset ($ _ post ['submit _ delete']) $ sub_type = 'delete ';
If ($ sub_type) {// echo 'script alert ("'. $ sub_type.'"); script '; exit;
$ Data = array ('sub _ type' => $ sub_type,
'File' => $ _ files ['file'],
'IMG _ tag_id '=> 'picview ',
'IMG _ dir' => 'upload _ img ',
);
$ Imgpath = upload ($ data );
} Else {
$ Imgpath = isset ($ _ session ['imgpath'])? $ _ Session ['imgpath']: '';
}
?>

Script
If (" "){
Parent.doc ument. getelementbyid ("picview"). src =" ";
Parent.doc ument. getelementbyid ("picview"). style. display = "block ";
}
Script


Http://www.bkjia.com/PHPjc/444841.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444841.htmlTechArticlephp picture upload and preview effect this article is an image upload code, he can upload to the server picture in the preview modification, if you want you can save, not feasible...

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.