Jump to content

Uploading help


Guest SaVaGe

Recommended Posts

Is a MySQL database able to hold files? I don't thinks so :)You should upload them to your directory, and store the link in a database table. I have a public upload program, shall I give it to you, so you can use it as an example?

<?php//#$#$ Settings that must be set$pass_default = "FL_l!v3s"; #the password in the formfield, must be specified$targetfolder = "./myuploads"; #upload targetfolder, compared to the current folder (./)$filetypes = array( #allowed MIME types (important!)"avi"=> "video/x-msvideo", #AVI files allowed"wmv"=> "video/x-ms-wmv", #WMV files allowed"gif"=> "image/gif"); #leave GIF type, for easy testing$max_bytes = "30000"; #maximum allowed filesize in bytes//#$#$ Optional settings$titlebar = "iUpload - Prof-Creations"; #text in the titlebar$fieldset = "Upload an image"; #text in the fieldset legend$password = ""; #spam preventing. this enables password protect $error_pass = "Access denied."; #message warning on wrong password (if enabled)$error_upload = "File not recieved!";$error_type = "Wrong filetype!";$error_size = "The file is too large.";$error_chmod = "Couldn't set permissions!";$error_server = "Uploading failed:";$error_move = "Couldn't move file!"; #if this error occurs, the targetfolder may be buggy$no_error = "Uploading was successful! Click here:"; #on successful upload, this text will link to the file$upload_button = "Upload!";$upload_name = "Image";//#$#$ After this line no edit please!$filetypes = "(".implode("|",$filetypes).")";$password = ($password == "") ?$pass_default :$password;echo "<!DOCTYPE html PUBLIC ","\"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">","\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">","\r\n<head>\r\n<title>$titlebar</title>","\r\n\r\n<!-- Characterset -->\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />","\r\n\r\n<!-- Stylesheet -->","\r\n<style>\r\n.text { border:inset 2px #c0c0c0;","\r\n background-color:#ffffff;","\r\n font-size:12px;","\r\n width:320px; padding:2px 0 1px;","\r\n text-align:left; color:#000080; }","\r\n.button { cursor:pointer;","\r\n border:outset 2px #008000;","\r\n background-color:#99ffcc;","\r\n font-size:12px;","\r\n width:70px; padding:0;","\r\n text-align:center; color:#000000; }\r\n</style>","\r\n<base target=\"_blank\" />\r\n</head>\r\n\r\n<body>";if (empty($_FILES['userfile']) == true){ echo "\r\n<form enctype=\"multipart/form-data\" action=\"{$_SERVER['PHP_SELF']}\" method=\"post\" target=\"_self\">","\r\n<fieldset style=\"width:400px; text-align:center\"><legend>$fieldset</legend>",($password != $pass_default)?("\r\Password: <input type=\"password\" class=\"text\" name=\"password\" style=\"width:200px\"". "\r\n onfocus='if (this.value==this.defaultValue) this.value=\"\"' value=\"{$pass_default}\" /><br />"):"\r\n<input type=\"hidden\" name=\"password\" value=\"{$pass_default}\" />","\r\n<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"{$max_bytes}\" />","\r\n<input type=\"file\" class=\"text\" name=\"userfile\" /><hr />","\r\n<button type=\"submit\" class=\"button\">{$upload_button}</button>","\r\n</fieldset></form>";}elseif (!ereg("^{$password}$",$_POST['password'])) { echo $error_pass; }elseif (is_uploaded_file($_FILES['userfile']['tmp_name']) == false) { echo $error_upload; }elseif (!ereg("^{$filetypes}$",$_FILES['userfile']['type'])) { echo $error_type; }elseif ($_FILES['userfile']['size'] > $max_bytes) { echo $error_size; }elseif ($_FILES['userfile']['error'] != 0) { echo "Error: ".$_FILES['userfile']['error']; }else{ if (move_uploaded_file($_FILES['userfile']['tmp_name'], "{$targetfolder}/{$_FILES['userfile']['name']}")){ if (!chmod("{$targetfolder}/{$_FILES['userfile']['name']}",0644)) { echo $error_chmod; }else { echo "{$no_error} <a href=\"{$targetfolder}/{$_FILES['userfile']['name']}\">{$upload_name}</a>"; }} else {echo "{$error_server} {$_FILES['userfile']['name']}<br />\r\n";if (file_exists("{$targetfolder}/{$_FILES['userfile']['name']}") == false) { echo $error_move; }else { print_r($_FILES); }}}echo "\r\n</body>\r\n</html>";?>
Copy the text from this codebox and paste it in notepad to safe the file.
Link to comment
Share on other sites

It is possible to store files in MySQL databases (like images), but it's not recommended, it's a little harder to backup and I think it would be much slower than using simple files on the server.

Link to comment
Share on other sites

The link to the uploaded file however, can be easily stored in the database.If you want this upload processor to do just that, then expand the following line at the bottom:

else { echo "{$no_error} <a href=\"{$targetfolder}/{$_FILES['userfile']['name']}\">{$upload_name}</a>"; }
To this:
else { mysql_connect($host, $user, $pass); mysql_select_db($database); mysql_query("MY SQL QUERY"); mysql_close(); echo "{$no_error} <a href=\"{$targetfolder}/{$_FILES['userfile']['name']}\">{$upload_name}</a>"; }
Or what- and however you are used to connect to your database and execute queries :)
Link to comment
Share on other sites

It is possible to store files in MySQL databases (like images), but it's not recommended, it's a little harder to backup and I think it would be much slower than using simple files on the server.
It's definately slower. When I was starting with PHP I wanted to learn how to store files in a database and how to use the GD library, so I built a photo gallery that resizes the images, creates a thumbnail, and stores them in the database. It takes a noticeable amount of time to pull all the thumbnails out of the database, and it obviously makes the database pretty big!The datatype for storing things like that is usually a blob (binary large object), and there are smallblob, mediumblob, and bigblob for MySQL. I think there is also just a binary data type, I'm not sure if it has a size restriction.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...