Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/74d31ad02487d9b7054bb0b77dfc3a6f to your computer and use it in GitHub Desktop.
Save code-boxx/74d31ad02487d9b7054bb0b77dfc3a6f to your computer and use it in GitHub Desktop.
PHP Very Simple File Upload

SIMPLE PHP FILE UPLOAD

https://code-boxx.com/simple-php-file-upload/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Simple PHP Upload Demo</title>
</head>
<body>
<form action="2-upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="upfile" required>
<input type="submit" value="Upload">
</form>
</body>
</html>
<?php
// (A) MOVE UPLOADED FILE TO DESTINATION
$source = $_FILES["upfile"]["tmp_name"];
$destination = $_FILES["upfile"]["name"];
$pass = move_uploaded_file($source, $destination);
/* (B) DONE - WHAT'S NEXT?
if ($pass) {
// REDIRECT TO ANOTHER PAGE?
header("Location: http://my-site.com/somewhere/");
exit();
// SHOW AN "UPLOAD OK PAGE"?
// require "ok.html";
}
// (C) DEAL WITH ERRORS?
else {
// SHOW ERROR MESSAGE - GOOD FOR DEBUGGING
// BUT NOT ON A LIVE SERVER (SECURITY ISSUES)
// print_r(error_get_last());
// JUST SHOW GENERIC ERROR MESSAGE?
exit("UPLOAD ERROR");
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment