Php generates sample code for automatically creating folders and uploading files

Source: Internet
Author: User

Copy codeThe Code is as follows:
<?
Session_start ();
If ($ _ SESSION ['company'] = '')
{
// Exit ();
}
?> <? Php // upload an image

$ Uptypes = array ('image/jpg ', 'image/jpeg', 'image/png ', 'image/pjpeg', 'image/gif ', 'image/bmp ', 'application/x-shockwave-flash', 'image/x-png ');
$ Max_file_size = 5000000; // size limit of uploaded files, in bytes


$ Addtime = date ("Ymd", time ());
$ Testdir = "./". $ addtime ."/";
If (file_exists ($ testdir )):
Else:
Mkdir ($ testdir, 0777 );
Endif;

$ Destination_folder = $ addtime. "/"; // Upload File Path
$ Imgpreview = 1; // whether to generate a preview image (1 is generated, others are not generated );
$ Imgpreviewsize = 1/2; // thumbnail Ratio

If ($ _ SERVER ['request _ method'] = 'post ')
{
If (! Is_uploaded_file ($ _ FILES ["Pic"] [tmp_name])
// Whether a file exists
{
Echo "<font color = 'red'> the file does not exist! </Font> ";
Exit;
}

$ File = $ _ FILES ["Pic"];
If ($ max_file_size <$ file ["size"])
// Check the file size
{
Echo "<font color = 'red'> the file is too large! </Font> ";
Exit;
}

If (! In_array ($ file ["type"], $ uptypes ))
// Check the file type
{
Echo "<font color = 'red'> only image files or Flash files can be uploaded! </Font> ";
Exit;
}

If (! File_exists ($ destination_folder ))
Mkdir ($ destination_folder );

$ Filename = $ file ["tmp_name"];
$ Image_size = getimagesize ($ filename );
$ Pinfo = pathinfo ($ file ["name"]);
$ Ftype = $ pinfo [extension];
$ PicName = time (). ".". $ ftype;
$ Destination = $ destination_folder. $ PicName;

If (file_exists ($ destination) & $ overwrite! = True)
{
Echo "<font color = 'red'> the file with the same name already exists! </A> ";
Exit;
}

If (! Move_uploaded_file ($ filename, $ destination ))
{
Echo "<font color = 'red'> An error occurred while uploading the file! </A> ";
Exit;
}

$ Pinfo = pathinfo ($ destination );
$ Fname = $ pinfo [basename];
}
?> <?

$ Path = dirname (_ FILE __);
Require_once ($ path. '/.../Module/Factory. php ');

$ Factory = new Factory ();
$ BLL_Trade = $ Factory-> FactoryTrade ();
Try {
$ Infor = new Infor ();
$ Infor-> Title =$ _ POST ['title'];
$ Infor-> Deposit = $ _ POST ['posit'];
$ Infor-> Hire =$ _ POST ['hire '];
$ Infor-> Location = $ _ POST ['location'];
$ Infor-> Pic = $ destination;
$ Infor-> Intro =$ _ POST ['intro'];
If ($ _ SESSION ['memberid'] = ''){
$ Infor-> Member-> ID = '';
} Else {
$ Infor-> Member-> ID = $ _ SESSION ['memberid'];}
If ($ _ POST ['goodsbarcode'] = ''){
$ Infor-> Goods-> BarCode = 0;
} Else {
$ Infor-> Goods-> BarCode =$ _ POST ['goodsbarcode'];}
$ Infor-> Class-> ID = 0; // modify later
$ Infor-> IssueTime = time ();
$ Infor-> ViewNum = 0;
$ Infor-> State = 1; // It is not fixed now and will be modified later
$ Infor-> Top = 0;
$ Infor-> Recommend = 0;
$ Infor-> BookMember-> ID = 0;
$ Infor-> BookTime = 0;
$ Infor-> BookRemark = 0;
$ BLL_Trade-> CreateInfor ($ Infor );
Echo 'Publish information successful! ';
}
Catch (Exception $ Err ){
Echo $ Err-> getMessage ();
}
?>

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.