Php generates sample code for automatically creating folders and uploading files _ PHP-php Tutorial

Source: Internet
Author: User
This article mainly introduces the sample code for php to automatically create folders and upload files. if you need some help, please refer to it. The code is as follows:
Session_start ();
If ($ _ SESSION ['Company'] = '')
{
// Exit ();
}
?>

$ 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 "the file does not exist! ";
Exit;
}

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

If (! In_array ($ file ["type"], $ uptypes ))
// Check the file type
{
Echo "only Image files or Flash files can be uploaded! ";
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 "the file with the same name already exists! ";
Exit;
}

If (! Move_uploaded_file ($ filename, $ destination ))
{
Echo "an error occurred while uploading files! ";
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.