Skip to content

Instantly share code, notes, and snippets.

@lyoshenka
Created April 1, 2015 10:35
Show Gist options
  • Save lyoshenka/9f5be7a74c72158d29cf to your computer and use it in GitHub Desktop.
Save lyoshenka/9f5be7a74c72158d29cf to your computer and use it in GitHub Desktop.
A super simple single-file upload script
<?php
ini_set('file_uploads', 'On');
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$target_file = __DIR__ . '/' . basename($_FILES["fileToUpload"]["name"]);
// Check if file already exists
if (file_exists($target_file))
{
echo "Sorry, file already exists.";
return;
}
/*
// Check if image file is a actual image or fake image
if(isset($_POST["submit"]))
{
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
} else {
echo "File is not an image.";
return;
}
}
/*
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000)
{
echo "Sorry, your file is too large.";
return;
}
// Allow certain file formats
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )
{
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
return;
}
*/
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
}
else
{
echo "Sorry, there was an error uploading your file.";
}
return;
}
?>
<html>
<body>
<h1>Upload</h1>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment