Skip to content

Instantly share code, notes, and snippets.

@Alfrick
Forked from alyssacodes/87-UploadingFiles.php
Created June 1, 2017 09:54
Show Gist options
  • Save Alfrick/3232bc837e737dec5c920084d2877a49 to your computer and use it in GitHub Desktop.
Save Alfrick/3232bc837e737dec5c920084d2877a49 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: Alyssa
* Date: 1/16/2016
* Time: 1:12 PM
*/
$max_size = 30000; //30KB
$location = 'uploads/'; //where the file is going
if (isset($_POST['submit'])) { //checking for anythiing will break the code
$name = $_FILES['file']['name']; //file name
$size = $_FILES['file']['size']; //file size
$type = $_FILES['file']['type']; //file type
$tmp_name = $_FILES['file']['tmp_name']; //temp location on server
if(checkType($name, $type) && checkSize($size, $max_size)){
if (isset($name)) {
save_file($tmp_name, $name, $location); //call my function
}
}
} else {
echo 'Please select a file:';
}
function checkType($name, $type){
//$extension = strtolower(substr($name, strpos($name, '.') + 1)); //get the extension
$extension = pathinfo($name, PATHINFO_EXTENSION); //better way to get extension
if (!empty($name)) {
if (($extension == 'jpg' || $extension == 'png') && ($type == 'image/jpeg' || $type == 'image/png')) {
return true;
} else{
echo 'That is not a jpg or png';
return false;
}
}
}
function checkSize($size, $max_size){
if($size <= $max_size){
return true;
} else{
echo 'File is too large. Max size in 30KB.';
return false;
}
}
function fileExists($name){
$filename = rand(1000,9999).md5($name).rand(1000, 9999);
echo $filename;
return false;
}
function save_file($tmp_name, $name, $location){
$og_name = $name;
//so long as the name is in existance - loop to check new name after it is generated
while (file_exists('uploads/' . $name)) {
echo 'File already exists. Generating name.<br/>';
$rand = rand(10000, 99999);
$name = $rand . '.' . pathinfo($name, PATHINFO_EXTENSION); //create new name
}
if (move_uploaded_file($tmp_name, $location . $name)) {
echo 'Success! ' . $og_name . ' was uploaded';
if(!($og_name==$name)){ //if original name != name
echo ' and renamed to '.$name.'.<br/>';
} else{
echo '.';
}
} else {
echo 'ERROR!';
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" name="submit" value="Submit"/>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment