Php Image Upload and preview

Source: Internet
Author: User

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, you cannot delete the file and upload 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 ("the source is not matched. Upload the. jpg).gif and. PNG images! "); </Script> '; exit;
}
If ($ data ['file'] ['SIZE']> $ data ['max _ file_size ']) {
Echo '<script> alert ("your uploaded manuscript is too large. Please select an image below 2 MB to upload! "); </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 ("An error occurred while uploading the manuscript. please upload it again! "); </Script> '; exit;
} Else {
Echo '<script> alert ("the manuscript is 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']: '';
}
?>

<Form action = "ad. php" method = "post" enctype = "multipart/form-data" target = "frame">
<Input type = "file" name = "file" class = "input">
<Input type = "submit" name = "submit_upload" value = "Upload">
<Input type = "submit" name = "submit_delete" value = "delete">
<Iframe id = "frame" name = "frame" width = "0" height = "0" marginwidth = "0" frameborder = "0" src = "about: blank "> </iframe>
</Form>

<Script>
If ("<? Php echo $ imgpath;?> "){
Parent.doc ument. getelementbyid ("picview"). src = "<? Php echo $ imgpath;?> ";
Parent.doc ument. getelementbyid ("picview"). style. display = "block ";
}
</Script>


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.