I. basic knowledge By using the Global Array $ _ FILES of PHP, you can upload FILES from the client computer to a remote server. The first parameter is the input name of the form, and the second subscript can be "name", "type", "size", "tmp_name" or "error ". Like this: $ _ FILES ["file"] ["name"]-name of the uploaded file $ _ FILES ["file"] ["type"]-type of the file to be uploaded $ _ FILES ["file"] ["size"]-size of the uploaded file, in bytes $ _ FILES ["file"] ["tmp_name"]-name of the temporary copy of the file stored on the server $ _ FILES ["file"] ["error"]-error code caused by file upload This is a very simple file Upload method. Based on security considerations, you should add restrictions on which users have the right to upload files. II. common uploads
- If ($ _ FILES ["file"] ["type"] = "image/gif ")
- | ($ _ FILES ["file"] ["type"] = "image/jpeg ")
- | ($ _ FILES ["file"] ["type"] = "image/pjpeg "))
- & ($ _ FILES ["file"] ["size"] <20000 ))
- {
- If ($ _ FILES ["file"] ["error"]> 0)
- {
- Echo "Return Code:". $ _ FILES ["file"] ["error"]."
";
- }
- Else
- {
- Echo "Upload:". $ _ FILES ["file"] ["name"]."
";
- Echo "Type:". $ _ FILES ["file"] ["type"]."
";
- Echo "Size:". ($ _ FILES ["file"] ["size"]/1024). "Kb
";
- Echo "Temp file:". $ _ FILES ["file"] ["tmp_name"]."
";
- If (file_exists ("upload/". $ _ FILES ["file"] ["name"])
- {
- Echo $ _ FILES ["file"] ["name"]. "already exists .";
- }
- Else
- {
- Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"],
- "Upload/". $ _ FILES ["file"] ["name"]);
- Echo "Stored in:". "upload/". $ _ FILES ["file"] ["name"];
- }
- }
- }
- Else
- {
- Echo "Invalid file ";
- }
- ?>
-
3. php reads and saves base64 encoded image content
- If ($ _ POST ['submit ']) {
- $ Image_info = getimagesize ($ _ FILES ['file'] ['tmp _ name']); // $ _ FILES ['file'] ['tmp _ name'] indicates the file path.
- $ Base64_image_content = "data: {$ image_info ['Mime ']}; base64 ,". chunk_split (base64_encode (file_get_contents ($ _ FILES ['file'] ['tmp _ name']);
- Echo $ base64_image_content; die;
- If (preg_match ('/^ (data: \ s * image \/(\ w +); base64,)/', $ base64_image_content, $ result )){
- $ Type = $ result [2];
- $ New_file = "./test. {$ type }";
- If (file_put_contents ($ new_file, base64_decode (str_replace ($ result [1], '', $ base64_image_content )))){
- Echo 'new file saved successfully: ', $ new_file;
- }
- }
- }
- ?>
-
|