Skip to content

Instantly share code, notes, and snippets.

@calvinchoy
Created June 20, 2013 08:44
Show Gist options
  • Save calvinchoy/5821229 to your computer and use it in GitHub Desktop.
Save calvinchoy/5821229 to your computer and use it in GitHub Desktop.
PHP - Simple file upload
<?php
/*
|--------------------------------------------------------------------------
| Upload file
|--------------------------------------------------------------------------
| On submit upload file to folder and return excel array
|
*/
if($_POST){
//helper and config variables
$isUploaded = false;
$upload_path = "uploads/";
//allowed extensions
$allowedExts = array("xls", "xlsx");
$extension = end(explode(".", $_FILES["file"]["name"]));
//check for allowed extensions
if (in_array($extension, $allowedExts)){
//check for file errors
if ($_FILES["file"]["error"] > 0){
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else{
//no error, upload file to directory
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists($upload_path . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " already exists. ";
}
else{
move_uploaded_file($_FILES["file"]["tmp_name"], $upload_path . $_FILES["file"]["name"]);
echo "Stored in: " . $upload_path . $_FILES["file"]["name"];
$isUploaded = true;
}
}
}
//invalid file extension, not allowed
else{
echo "Invalid file";
}
//on upload success
if($isUploaded){
//path to uploaded file
$temp_path = $upload_path . $_FILES["file"]["name"];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment