Php classic secure file upload code-PHP source code

Source: Internet
Author: User
Tags ereg upload php
Ec (2); & lt ;? Php ** & nbsp; * Upload File & nbsp; * to: uploadBoroughThumb | borough | picture & nbsp; * to: function name | directory category | image type & nbsp; * & nbsp; * require (path. inc. php); $ to $ _ GET [& quot; to & quot;]; $ action $ _ GET [script ec (2); script

/**
* Upload a file
* To: uploadBoroughThumb | borough | picture
* To: function name | directory type | image type
*
*/
Require ('path. inc. php ');
$ To = $ _ GET ["to"];
$ Action = $ _ GET ['action'];
If ($ action = ""){
$ Action = "form ";
}
If ($ action = "doupload "){
Echo'';
Echo'';
Echo'Uploaded';
Echo" ";
Echo'';

$ Store_info = explode ('|', $ );
$ Js_func = $ store_info [0];

/* Determine special characters */
If ($ store_info [1]) {
If (! Ereg ("^ [A-Za-z] + $", $ store_info [1]) {
Exit;
}
}
If ($ store_info [2]) {
If (! Ereg ("^ [A-Za-z] + $", $ store_info [2]) {
Exit;
}
}

$ Upload_conf = require ($ cfg ['path'] ['conf']. 'upload. cfg. php ');

$ This_config = (array) $ upload_conf [$ store_info [1] [$ store_info [2];
If (empty ($ this_config )){
Exit;
}
$ Upload = new UploadFile (); // instantiate the upload object
// Set the types of files that can be uploaded
$ Upload-> setAllowFileType ($ this_config ['allowtype']);
Foreach ($ _ FILES as $ a_file ){
If ($ a_file ['error']! = UPLOAD_ERR_NO_FILE ){
Try {
$ FileName = $ upload-> upload ($ a_file, $ cfg ['path'] ['root']. 'upfile /'. $ this_config ['originalpath'], 1 );
$ F_path ['url'] = $ this_config ['originalpath']. $ fileName;
$ F_path ['name'] = $ a_file ['name'];
$ Attach_file [] = $ f_path;
If (in_array (strtolower (FileSystem: fileExt ($ f_path ['name']), array ('gif', 'jpeg ', 'jpg', 'png ')) &&! $ This_config ['noresize']) {
// Scale down to the specified size
$ Image = new Image ($ cfg ['path'] ['root']. 'upfile/'. $ this_config ['originalpath']. $ fileName );
$ Image-> resizeImage ($ this_config ['width'], $ this_config ['height'], $ this_config ['resizetype ']);
$ Image-> save ();
// Add a watermark
If ($ this_config ['watermark']) {
$ Image = new Image ($ cfg ['path'] ['root']. 'upfile/'. $ this_config ['originalpath']. $ fileName );
$ Image-> waterMark ($ this_config ['watermarkpic '], $ this_config ['watermarkpos']);
$ Image-> save ();
}
// Generate a thumbnail if necessary
If ($ this_config ['thumb']) {
$ Image = new Image ($ cfg ['path'] ['root']. 'upfile/'. $ this_config ['originalpath']. $ fileName );
$ Image-> resizeImage ($ this_config ['thumbwidth'], $ this_config ['thumbheight'], $ this_config ['thumbresizetype ']);
If ($ this_config ['originalpath'] = $ this_config ['thumbdir']) {
// Prevent the storage directory from overwriting the original image at the same time. If you do not store the thumbnail, set the thumb attribute to null.
$ Image-> save (2, $ cfg ['path'] ['root']. 'upfile/'. $ this_config ['thumbdir'],' _ thumb ');
$ Thumb_path = $ this_config ['thumbdir']. FileSystem: getBasicName ($ fileName, false). '_ thumb'. FileSystem: fileExt ($ fileName, true );
} Else {
$ Image-> save (1, $ cfg ['path'] ['root']. 'upfile/'. $ this_config ['thumbdir']);
$ Thumb_path = $ this_config ['thumbdir']. $ fileName;
}
}
}
// Return parameters
Echo "script"
Var parentForm;
If (window. opener ){
ParentForm = window. opener;
} Else {
ParentForm = window. parent;
}
ParentForm. ". $ js_func. "('". $ f_path ['url']. "','". $ f_path ['name']. "','". $ thumb_path. "');
Script ";

} Catch (Exception $ e ){
$ Page-> back ($ e-> getMessage ());
}
} Else {
Echo "script"
Alert ('browse the file first and then click upload php ');
History. back ();
Script ";
Exit;
}
Echo "script"
/* If (window. opener ){
Window. close ();
} Else {
History. back ();
}*/
History. back ();
Script ";
}
Echo'';
Echo'';
} Elseif ($ action = "form "){
Echo'';
Echo'';
Echo'Upload files';
Echo" ";
Echo'';
Echo"";
Echo"






";Echo "";Echo"Echo"";Echo'';}?>

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.