The following code shows how to get the files uploaded by the user in PHP, and limit the file type of the general picture file, and finally save 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). The 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";
- }
- ?>
Copy Code
|
Upload files, and save, PHP