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 ();
}
?>