Php image upload and preview

Source: Internet
Author: User

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.