Php restricts the Upload file type and saves the uploaded file
The following code demonstrates how to obtain a user-uploaded file in php, limit the file type of a general image file, and save it to the server.
- $ AllowedExts = array ("gif", "jpeg", "jpg", "png ");
- $ Extension = end (explode (".", $ _ FILES ["file"] ["name"]);
- If ($ _ FILES ["file"] ["type"] = "image/gif ")
- | ($ _ FILES ["file"] ["type"] = "image/jpeg ")
- | ($ _ FILES ["file"] ["type"] = "image/jpg ")
- | ($ _ FILES ["file"] ["type"] = "image/pjpeg ")
- | ($ _ FILES ["file"] ["type"] = "image/x-png ")
- | ($ _ FILES ["file"] ["type"] = "image/png "))
- & ($ _ FILES ["file"] ["size"] <20000)
- & In_array ($ extension, $ allowedExts ))
- {
- 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 ";
- }
- ?>
|
Upload and save the file, php