There:
PHP Code:
<?php
session_start();
if (isset($_GET['file'])) {
header('location:uploads/'.$_GET['file']);
}
/* //For debugging purposes
if (file_exists('upload.phps')) { unlink('upload.phps'); }
copy('upload.php','upload.phps');
*/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Upload file</title>
</head>
<body>
<?php
//upload directory.
//change to fit your need eg. files, upload .... etc.
$upload_dir = "uploads/";
//number of files to upload.
$num_files = 1;
//the file size in bytes.
$size_bytes =2048000; //51200 bytes = 50KB.
//Extensions you want files uploaded limited to.
$limitedext = array(".gif",".jpg",".jpeg",".png",".mov",".mpeg","mpg",".wmv",".wma",".bmp",".mp3");
$error = '';
$missing=array();
/* //For debugging purposes
echo '<xmp>';
print_r($_POST);
echo '</xmp>';
echo '<xmp>';
print_r($_FILES);
echo '</xmp>';
*/
if (isset($_POST['Submit'])){
echo '<p>Uploading...</p>';
flush();
if (isset($_POST['file_name']) &&
isset($_POST['name']) &&
isset($_POST['email']) &&
isset($_POST['filedescr']) &&
isset($_POST['name']) &&
trim($_POST['file_name']) != '' &&
trim($_POST['name']) != '' &&
trim($_POST['email']) != '' &&
trim($_POST['filedescr']) != '' &&
trim($_POST['name']) != ''
) {
$file = $_FILES['file'];
$filesize = $file['size'];
$tmp = $file['tmp_name'];
$ext = strrchr($file['name'],'.');
$name = $_POST['file_name'].strtolower($ext);
if (isset($_SESSION['timer']) && time()-$_SESSION['timer'] < 30) {
echo '<h2>Please wait another '.(30-(time()-$_SESSION['timer'])).' seconds before uploading again.</h2>';
}
elseif (!in_array(strtolower($ext),$limitedext)) {
echo '<h2>Sorry, you can only upload images, movies and music files.</h2>';
uploadform();
}
elseif ($filesize > $size_bytes) {
echo '<h2>Sorry, the file you have chosen is too big.</h2>';
uploadForm();
}
elseif (file_exists($upload_dir.$name)) {
echo '<p><strong>Warning: </strong>File already exists. Renaming...</p>';
$arr_name = explode('.',$name);
unset($arr_name[count($arr_name)-1]);
$name = $arr_name[0].'_'.$_POST['email'].$ext;
if (move_uploaded_file($tmp,$upload_dir.$name)) {
echo '<h2>Upload completed.</h2>';
echo '<p>Your file has been uploaded. Click <a href="?">here</a> to return to the form.</p>';
//echo '<p>You can download the <a href="'.$_SERVER['PHP_SELF'].'?file='.urlencode($name).'>file here.</a></p>';
$_SESSION['timer'] = time();
}
}
else {
if (move_uploaded_file($tmp,$upload_dir.$name)) {
echo '<h2>Upload completed.</h2>';
echo '<p>Your file has been uploaded. Click <a href="?">here</a> to return to the form.</p>';
//echo '<p>You can download the <a href="'.$_SERVER['PHP_SELF'].'?file='.urlencode($name).'>file here.</a></p>';
$_SESSION['timer'] = time();
}
}
// sleep(3);
}
else {
echo '<h2>Fields Missing.</h2>';
uploadForm();
}
}
else {
uploadForm();
}
function get($field) {
echo isset($_POST[$field]) ? $_POST[$field] : '';
}
function uploadForm() {
global $error;
if ($error != '') {
echo '<h2>'.$error.'</h2>';
}
?>
<form name="uploader" action="" method="post" enctype="multipart/form-data">
<p>
<label>Name: <input type="text" name="name" value="<?php get('name'); ?>"></label>
</p>
<p>
<label>E-Mail Address: <input type="text" name="email" value="<?php get('email'); ?>"></label>
</p>
<p>
<label>File Description:<br><textarea name="filedescr" cols="50" rows="10"><?php get('filedescr'); ?></textarea></label>
</p>
<p>
<label>File: <input type="file" name="file" value="<?php get('file'); ?>"></label>
</p>
<p>
<label>File Name: <input type="text" name="file_name" value="<?php get('file_name'); ?>"></label>
</p>
<p>Maximum Size: 2MB</p>
<p>
<label><input type="submit" name="Submit" value="Upload!"></label>
</p>
</form>
<?php } ?>
</body>
</html>
Bookmarks