To upload FILES in php, use the ultra-Global Array $ _ FILES. in the form, add the sentence enctype = & quot; multipart/form-data & quot, otherwise, $ _ FILES cannot be set. To upload FILES in php, use the ultra-Global Array $ _ FILES. you must add enctype = "multipart/form-data" to the form, otherwise, $ _ FILES cannot be set.
The $ _ FILES array information is as follows:
$ _ FILES ['upload _ file'] ['name'] // Obtain the file name.
$ _ FILES ['upload _ file'] ['tmp _ name'] // get the temporary storage location
$ _ FILES ['upload _ file'] ['size'] // get the file size
$ _ FILES ['upload _ file'] ['type'] // Obtain the object MIME type.
The following is the instance code:
Php file Upload instance
$ Upload_file = $ _ FILES ['upload _ file'] ['tmp _ name']; $ Upload_file_name = $ _ FILES ['upload _ file'] ['name']; If ($ upload_file ){ $ File_size_max = 1000000; // 1 MB limit the maximum file upload capacity (bytes) $ Store_dir = "E:/"; // storage location of the uploaded file $ Accept_overwrite = 1; // whether to overwrite the same file // Check the file size If ($ upload_file_size> $ file_size_max ){ Echo "Sorry, your file capacity exceeds the limit "; Exit; } // Check the read/write file If (file_exists ($ store_dir. $ upload_file_name )&&! $ Accept_overwrite ){ Echo "files with the same file name exist "; Exit; } // Copy the file to the specified directory If (! Move_uploaded_file ($ upload_file, $ store_dir. $ upload_file_name )){ Echo "failed to copy the file "; Exit; } } Echo"You uploaded the file :"; Echo $ _ FILES ['upload _ file'] ['name']; Echo" "; // The original name of the client machine file. Echo "the MIME type of the file is :"; Echo $ _ FILES ['upload _ file'] ['type']; // MIME type of the file, which must be supported by the browser, for example, "image/gif ". Echo" "; Echo "Upload file size :"; Echo $ _ FILES ['upload _ file'] ['size']; // Size of the uploaded file, in bytes. Echo" "; Echo "the file is temporarily stored after being uploaded :"; Echo $ _ FILES ['upload _ file'] ['tmp _ name']; // Temporary file name stored on the server after the file is uploaded. Echo" "; $ Erroe = $ _ FILES ['upload _ file'] ['error']; Switch ($ Erroe ){ Case 0: Echo "uploaded successfully"; break; Case 1: Echo "the uploaded file exceeds the limit of the upload_max_filesize option in php. ini."; break; Case 2: Echo "the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. "; Break; Case 3: Echo "only part of the file is uploaded"; break; Case 4: Echo "no file is uploaded"; break; } ?> |