VB icon

Upload file, make name unique, save in MySql DB

Email
Submitted on: 1/1/2015 9:21:00 AM
By: Jor (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: PHP 4.0
Views: 1024
 
     This code will upload a file from a client computer to your server, give the file a new, unique name and save the new filename into a MySql database. It also checks the size of the file so you can give a maximum size for all files that will be uploaded.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Upload file, make name unique, save in MySql DB
// Description:This code will upload a file from a client computer to your server, 
give the file a new, unique name
and save the new filename into a MySql database.
It also checks the size of the file so you can give a maximum size for all files that will be uploaded.
// By: Jor (from psc cd)
//**************************************

***upload.html***
<html>
<head>
</title></title>
</head>
<body>
<form name=form1 action=upload.php method=post enctype=multipart/form-data>
<input type=file name=FileToUpload>
<input type=hidden name=MaxFileSize value=64000>
</form>
</body>
</html>
***upload.php***
<?php
if ($FileToUpload_type == 'image/gif') {
$type = '.gif';
}
if ($FileToUpload_type == 'image/pjpeg') {
$type = '.jpg';
}
if ($FileToUpload_type == 'image/x-png') {
$type = '.jpg';
}
$newfile = substr($FileToUpload, -9);
if($FileToUpload_name = '') {
print("No file was selected!");
}
elseif($FileToUpload_size > $MaxFileSize) {
print("The file to upload is too big");
}
else {
$global_db = mysql_connect('localhost', 'User', 'Password');
mysql_select_db('DBName', $global_db) or die("Connection error");
$query = "INSERT INTO Filetable (File) VALUES ('$newfile$type')";
$result = mysql_query($query) or die("ERROR");
move_uploaded_file($FileToUpload, "/full/path/on/your/server/images/$newfile$type");
}
?>


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.