Skip to content

Instantly share code, notes, and snippets.

@agarzon
Last active January 23, 2017 23:00
Show Gist options
  • Save agarzon/3966824 to your computer and use it in GitHub Desktop.
Save agarzon/3966824 to your computer and use it in GitHub Desktop.
Basic upload file with PHP
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Upload file</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container jumbotron">
<h1>Upload file</h1>
<?php
clearstatcache();
$uploadFolder = __DIR__ . '/upload/';
if (!file_exists($uploadFolder)) {
mkdir($uploadFolder, 0777, true);
}
?>
<?php if (empty($_FILES["file"])) : ?>
<?php if (!is_writable($uploadFolder)) : ?>
<div class="alert alert-dismissible alert-danger">
<strong><?= $uploadFolder ?>/</strong> folder is not writable. Write permission is required.
</div>
<?php else: ?>
<form action="" enctype="multipart/form-data" method="post">
<input type="file" name="file">
<button type="submit" class="btn btn-primary">Upload</button>
</form>
<?php endif; ?>
<?php else: ?>
<?php if (@move_uploaded_file($_FILES["file"]["tmp_name"], $uploadFolder . $_FILES["file"]["name"])) : ?>
<div class="alert alert-dismissible alert-success">Good job! the file was uploaded !</div>
<?php else : ?>
<div class="alert alert-dismissible alert-danger">Error uploading file !</div>
<?php endif; ?>
<?php endif; ?>
<hr>
<p><a href="<?= basename(__FILE__) ?>" class="btn btn-info">Home</a></p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment