Last active
September 26, 2018 19:12
-
-
Save samjaninf/bf02dce5d1aaeb073cb20cab2426af7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$formaction = $_SERVER['PHP_SELF']; //the URL the form should send users to on submit | |
$allowedfiletypes = array('csv'); //a comma separated list of allowed extensions | |
$uploadfolder = './'; //the folder where the uploaded file should be moved to | |
$uploadfilename = 'data.csv'; //the filename that the uploaded file should be renamed to | |
//check to see if the form has been submitted | |
if (array_key_exists('action',$_POST) && is_string($_POST['action']) && (strip_tags($_POST['action']) == 'upload')) { //the form has been submitted | |
echo '<p>Uploading file... '; | |
if (empty($_FILES['uploadfile']['name'])) { | |
echo '<strong>Error: File not uploaded!</strong></p>'; | |
} else { | |
$fileext = strtolower(substr($_FILES['uploadfile']['name'],strrpos($_FILES['uploadfile']['name'],'.')+1)); | |
if (!in_array($fileext,$allowedfiletypes)) { | |
echo '<strong>Error: Invalid file extension!</strong></p>'; | |
} else { //a file of the correct type was uploaded | |
$fulluploadfilename = $uploadfolder.$uploadfilename; | |
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $fulluploadfilename)) { | |
echo 'Your file has been uploaded succesfully:<br>'.$fulluploadfilename.'</p>'; | |
} else { | |
echo '<strong>Error: Could not save file:<br>'.$fulluploadfilename.'</strong></p>'; | |
} | |
} | |
} | |
} | |
echo '<form action="'.$formaction.'" method="post" enctype="multipart/form-data">'; | |
echo '<p>Your CSV file:<br><input type="file" name="uploadfile"></p>'; | |
echo '<p><input type="submit" class="button" value="Upload File"></p>'; | |
echo '<input type="hidden" name="action" value="upload">'; | |
echo '</form>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment