Go Back   Random Stuff Forums > Technical > Programming

Programming Discuss any type of programming here.

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 14-07-2009, 11:57 PM
Matt's Avatar
(Offline)
Administrator
 
Join Date: Jan 2007
Location: United States - Ohio
Posts: 3,820
Matt is an unknown quantity at this point
Default [PHP][HTML] Simple File Upload

Dusting off my PHP skills gentlemen. Here is a simple file upload script written in PHP in conjunction with an HTML form.

The features of the script include:
  • Limit file upload size
  • Limit file types for uploaded files
  • Prevent file overwrites by checking if a file already exists
  • Display the link once the upload is finished

Here is the HTML form:
<html>
<head></head>
<body>
<form enctype="multipart/form-data" action="up.php" method="POST">
Choose a file to upload: <input name="file_up" type="file" /><br />
<input type="submit" value="Upload!" />
</form><br />
<strong>The max file size allowed is 2MB</strong>
</body>
</html>
Here is the PHP script (this would be called "up.php" because that is what the "action" property of the HTML form is set to)
Please note that the folder (in this case "uploads/") to which files are being uploaded would have to be CHMODDED to 777
<?php
/*
		 This is a simple PHP Upload script written in full by Matt from RSForums.org
*/
$max_file_size = 2097152; // Set the max file size (in bytes) - DEFAULT = 2MB
$allowed_types = array('image/jpeg', 'image/gif', 'image/png'); // Set the allowed file types
/* 
	Here are some more common file types that you may want to include in this array:
	.bmp 	image/bmp
	.css 	text/css
	.exe 	application/octet-stream
	.htm 	text/html
	.html 	text/html
	.htmls 	text/html
	.psd 	application/octet-stream
	.txt 	text/plain
	.zip 	application/x-compressed
	.zip 	application/x-zip-compressed
	.zip 	application/zip
	.zip 	multipart/x-zip	
	A comprehensive list of MIME Types can be found here: http://www.webmaster-toolkit.com/mime-types.shtml	
*/
$link_front = 'http://yoursite.com/uploads/'; // Set the URL down to the directory, starting with http://
$dir = '/home/www/uploads'; // Set the directory to which files will be uploaded from the home or root (WILL NOT start with http://)

//DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING!!!
if (in_array($_FILES['file_up']['type'], $allowed_types)) { //Check if the file is of an allowed file type

if ($_FILES['file_up']['size'] > $max_file_size){ // Check if the file exceeds the maximum size

	echo 'Your file was too large.  The max size is $max_file_size bytes.';
	
} else{

$file = $dir . basename($_FILES['file_up']['name']);

while (file_exists($file)): // Ensure no files get overwritten & attach a random number to the front of the filename if it already exists
	$_FILES['file_up']['name'] = rand(0,99) . $_FILES['file_up']['name'];
	$file = $dir . basename($_FILES['file_up']['name']);
endwhile;

if (move_uploaded_file($_FILES['file_up']['tmp_name'], $file)) { // Check if sucess

	echo 'Your file was uploaded! <br />';
	echo 'Here is the link to your file: ';
	echo $link_front . $_FILES['file_up']['name']; // Display the complete link to the uploaded file
	
} else {
	echo "There was a problem uploading your file.  Please try again later.";	
}
}
} else {
	echo 'You may not upload this type of file!';
}
?>
Obviously this can be modified to fit your needs. I think this is a fairly comprehensive script that demonstrates most of the features that you would want in an upload script. If nothing else, it is a good start to be built upon. Enjoy
__________________
"Facts do not cease to exist because they are ignored."

Last edited by Matt; 15-07-2009 at 05:03 PM.. Reason: Updated PHP
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 03:53 AM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
Copyright © 2006 - 2010, Rsforums.org