PHP sample code for uploading multiple files and images
-
- $ Uptypes = array (
- // The ContentType format of the uploaded file
- 'Image/jpg ',
- 'Image/jpeg ',
- 'Image/png ',
- 'Image/pjpeg ',
- 'Image/GIF ',
- 'Image/bmp ',
- 'Image/x-png ',
- 'Application/msword', // doc
- 'Application/vnd.openxmlformats-officedocument.wordprocessingml.doc ument', // docx
- 'Application/vnd. openxmlformats-officedocument.presentationml.presentation ', // pptx
- 'Application/vnd. openxmlformats-officedocument.spreadsheetml.sheet ', // xlsx
- 'Text/plain'
- );
- /**
- * Php multi-file and multi-image Upload
- * By bbs.it-home.org
- */
- $ Max_file_size = 2000000; // size limit of uploaded files, in bytes
- $ Dir = "upload/"; // file upload path
- If ($ _ SERVER ['request _ method'] = 'post ')
- {
- $ File = $ _ FILES ['upfile'] ['name'];
- Foreach ($ file as $ key => $ item ){
- If ($ item! = ''){
- If (! Is_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name'] [$ key]) // check whether a file exists
- {
- Echo "the image does not exist! ";
- Exit;
- }
- If ($ max_file_size <$ _ FILES ['upfile'] ['size'] [$ key]) // check the file size
- {
- Echo "the file is too large! ";
- Exit;
- }
- If (! File_exists ($ dir ))
- {
- Mkdir ($ dir );
- }
- $ Filename = $ _ FILES ['upfile'] ['tmp _ name'] [$ key];
- $ Image_size = getimagesize ($ filename );
- $ Pinfo = pathinfo ($ file [$ key]);
-
- $ Ftype = $ pinfo ['extension'];
- $ Destination = $ dir. time (). $ file [$ key];
- If (file_exists ($ destination) & $ overwrite! = True)
- {
- Echo "a file with the same name already exists ";
- Exit;
- }
-
- If (! Move_uploaded_file ($ filename, $ destination ))
- {
- Echo "an error occurred while moving the file ";
- Exit;
- }
- $ Pinfo = pathinfo ($ destination );
- $ Fname = $ pinfo ['basename'];
- Echo "uploaded successfully
File name: ". $ dir. $ fname ." ";
- Echo "width:". $ image_size [0];
- Echo "length:". $ image_size [1];
- Echo"
Size: ". $ _ FILES ['upfile'] ['size']." bytes ";
-
- }
-
- Echo"
Image preview: ";
- Echo "echo" alt = \ "image preview: \ r File name:". $ destination. "\ r Upload Time: \"> ";
- Echo"
";
- }
- }
-
- ?>
-
Note: You need to upload two files during Upload. Otherwise, an error is reported. The code is not perfect. it just provides an idea for your reference. |