Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple PHP large upload test script
upload_max_filesize = 2048M
post_max_size = 2048M
max_input_time = 3600
<?php
session_start();
/** @var array $settings list of settings to get. */
$settings = [
[ 'name' => 'upload_max_filesize', 'eg' => 'eg. 10M or 5G' ],
[ 'name' => 'post_max_size', 'eg' => 'eg. 10M or 5G' ],
[ 'name' => 'max_execution_time', 'eg' => 'in seconds' ],
[ 'name' => 'memory_limit', 'eg' => 'eg. 512M' ],
[ 'name' => 'max_input_time', 'eg' => 'in seconds' ],
];
$errors = [];
$data = [];
/**
* Try to set using session / get current values
*/
foreach($settings as $v ) {
// $_POST
if( isset( $_POST[$v['name']] ) && !empty( $_POST[$v['name']] ) ) {
$_SESSION[$v['name']] = $_POST[$v['name']];
}
// $_SESSION
if( isset( $_SESSION[$v['name']] ) ) {
if( ini_set( $v['name'], $_SESSION[$v['name']] ) === FALSE ) {
$errors[] = 'Unable to set ' . $v['name'] . ' setting with value ' . $_SESSION[$v['name']] . '<br><small>(this setting might be not available using ini_set, please use .htacces or .user.ini)</small>';
}
}
$data[$v['name']] = ini_get($v['name']);
}
/**
* Handle upload
*/
$currentDir = getcwd();
$uploadDirectory = ".";
$fileExtensions = ['test']; // Get all the file extensions
$fileName = 'test.test';
$fileSize = $_FILES['myfile']['size'];
$fileTmpName = $_FILES['myfile']['tmp_name'];
$fileType = $_FILES['myfile']['type'];
$fileExtension = strtolower(end(explode('.',$fileName)));
$uploadPath = $currentDir . DIRECTORY_SEPARATOR . $uploadDirectory;
/**
* Handle upload
*/
if( !touch( $uploadPath . DIRECTORY_SEPARATOR . $fileName ) ) {
$errors[] = 'Directory is not writable for server!';
}
if (isset($_POST['submit'])) {
if( $_FILES['myfile']['error'] == UPLOAD_ERR_OK ) {
$didUpload = move_uploaded_file($fileTmpName, $uploadPath . DIRECTORY_SEPARATOR . $fileName );
if ($didUpload) {
$_SESSION['uploaded_file'] = $fileName;
$_SESSION['file_info'] = implode(' | ', $_FILES['myfile'] );
} else {
$errors[] = "An error occurred somewhere. Try again or contact the admin";
}
} else {
$errors[] = 'There was an error with your upload.';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Upload tester</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- Leave those next 4 lines if you care about users using IE8 -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body style="padding: 64px 0;">
<div class="container">
<h1 class="page-header">Large file upload test <small>by Realhe.ro</small></h1>
<!-- Errors -->
<?php if( !empty( $errors ) ): ?>
<div class="alert alert-danger" role="alert">
<h4>Errors:</h4>
<ul>
<?php foreach($errors as $v): ?>
<li><?= $v ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<!-- Current settings list -->
<h3>Current server settings:</h3>
<hr>
<table class="table table-striped">
<tr>
<th>Name:</th>
<th>Value:</th>
</tr>
<?php foreach($data as $k=>$v): ?>
<tr>
<td><?= $k ?></td>
<td><?= $v ?></td>
</tr>
<?php endforeach; ?>
</table>
<!-- upload form -->
<form method="post" enctype="multipart/form-data">
<h3>Try to override default settings:</h3>
<div class="alert alert-success" role="alert">These settings will be used to upload your file.</div>
<!-- Dynamic settings form -->
<?php foreach( $settings as $v ): ?>
<div class="form-group">
<label for="<?= $v['name']; ?>"><?= $v['name']; ?></label>
<input type="text" class="form-control" id="<?= $v['name']; ?>" name="<?= $v['name']; ?>" placeholder="<?= $v['eg']; ?>">
</div>
<?php endforeach; ?>
<div class="form-group" style="position:relative;">
<a class='btn btn-primary' href='javascript:;'>
Choose File...
<input type="file" name="myfile" style='position:absolute;z-index:2;top:0;left:0;filter: alpha(opacity=0);-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";opacity:0;background-color:transparent;color:transparent;' name="file_source" size="40" onchange='$("#upload-file-info").html($(this).val());'>
</a>
&nbsp;
<span class='label label-info' id="upload-file-info"></span>
</div>
<button type="submit" name="submit" class="btn btn-default">Submit</button>
</form>
<!-- uploaded file info -->
<?php if( !empty( $_SESSION['uploaded_file'] ) ): ?>
<h2>Uploaded file info:</h2>
<p>You can download your file here: <a target="_blank" href="<?= $_SESSION['uploaded_file'] ?>">download</a></p>
<p>Upload info: <?= $_SESSION['file_info'] ?></p>
<?php endif; ?>
</div>
<!-- Including Bootstrap JS (with its jQuery dependency) so that dynamic components work -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment