Simple example of php file Upload
- /**
- * Upload files in php
- * Edit: bbs.it-home.org
- */
- 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"]); // comment
- Echo "Stored in:". "upload/". $ _ FILES ["file"] ["name"];
- }
- }
- }
- Else
- {
- Echo "Invalid file ";
- }
- ?>
Code description: When a php file is uploaded, a temporary folder exists. if the file is not moved, the temporary file will be automatically deleted after the script is complete. The above line of code is to save the uploaded file. The saved directory is the upload folder under the current directory. |