Php file Upload Sample code
- Upload images
- $ Max_file_szie = 2 * pow (); // the uploaded file is smaller than 2 MB.
- $ Destination_folder = 'uploadimg/'; // upload the file storage path
- If ($ _ SERVER ['request _ method'] = 'post '){
- If (! Is_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name']) {
- Echo 'the image does not exist! ';
- Exit;
- }
- If ($ max_file_szie <$ _ FILES ['upfile'] ['size']) {
- Echo: The file is too large! ';
- Exit;
- }
- If (! In_array ($ _ FILES ['upfile'] ['type'], $ uptypes )){
- Echo 'file type does not match! '. $ _ FILES ['upfile'] ['type'];
- Exit;
- }
- If (! File_exists ($ destination_folder )){
- Mkdir ($ destination_folder );
- }
- $ Filename = $ _ FILES ['upfile'] ['tmp _ name'];
- $ Image_size = getimagesize ($ filename );
- $ Pinfo = pathinfo ($ _ FILES ['upfile'] ['name']); // file path information
- $ Ftype = $ pinfo ['extension']; // suffix of the old file
- $ Destination = $ destination_folder.time (). ".". $ ftype; // new file name
- If (file_exists ($ destination) & $ voerwrie! = True ){
- Echo 'file with the same name already exists! ';
- Exit;
- }
- // Move the uploaded file from the temporary folder to the specified directory
- 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:
- ". $ Destination_folder. $ fname ."
";
- Echo 'width: '. $ image_size [0];
- Echo 'height: '. $ image_size [1];
- Echo'
Size: '. $ _ FILES ['upfile'] ['size']. "bytes ";
- }
- ?>
|
File upload, php