Is it possible to make a PHP script that would upload a file, that would take information (Name, Description, Creator) and make an html document that displays the information with a link for download of the file? If so will someone make one and help me incorporate it into my site for $Frih?
Uploading...
Sure, here is the script to upload the file:
This is a code I use a lot in my webs because it is fairly easy to customize...
If you wanted to store information about the author, create a table and INSERT it where the comment says 'Put here the sql code to store author information'
Now the file 'next.php':
If you have any problem ask
| Code: |
| <?php
session_start(); /*=========================================\ Author : Mohammed Ahmed(M@@king) Version : 1.1 Date Created: Oct 11 2004 --------------------------- Edited by: Alejandro U Alvarez (http://kdams.es) Version: 1.2 Date edited: Jan 06 2008 ---------------------------- Last Update: August 21 2005 ---------------------------- Country : Palestine City : Gaza E-mail : m@maaking.com MSN : m@maaking.com AOL-IM : maa2pal WWW : http://www.maaking.com Mobile/SMS : 00972-599-622235 \\ ===========================================\ ---Description ----------------------------------------------------- The Super Global Variable $_FILES is used in PHP 4.x.x. $_FILES['filetoupload']['size'] ==> Get the Size of the File in Bytes. $_FILES['filetoupload']['tmp_name'] ==> Returns the Temporary Name of the File. $_FILES['filetoupload']['name'] ==> Returns the Actual Name of the File. $_FILES['filetoupload']['type'] ==> Returns the Type of the File. So if I uploaded the file 'test.doc', the $_FILES['filetoupload']['name'] would be 'phptut.doc' and $_FILES['filetoupload']['type'] would be 'application/msword'. ---------------------------------------------------------------------*/ //**********************************************************************// // $_FILES['filetoupload'] is the value of // // file field from the form. <input type="file" name="filetoupload"> // //**********************************************************************// ################################################################################ ##---------------------------1 - Setup error_reporting(0); //this function gets the extension of uploaded file. function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } if (!ini_get("register_globals")) { import_request_variables('GPC'); } $phpver = phpversion(); if ($phpver < '4.1.0') { $_GET = $HTTP_GET_VARS; $_POST = $HTTP_POST_VARS; $_SERVER = $HTTP_SERVER_VARS; } $phpver = explode(".", $phpver); $phpver = "$phpver[0]$phpver[1]"; if ($phpver >= 41) { $PHP_SELF = $_SERVER['PHP_SELF']; } ################################################################################ // this is the upload dir where files will go. //Don't remove the / //Chmod it (777) $upload_dir = "uploads/"; //change to whatever you want. //51200 bytes = 50KB $size_bytes = 512000; //File Size in bytes (change this value to fit your need) $extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no) $limitedext = array(".doc",".rtf",".txt",".zip"); //Extensions you want files uploaded limited to. also you can use: //array(".gif",".jpg",".jpeg",".png",".txt",".nfo",".doc",".rtf",".htm",".dmg",".zip",".rar",".gz",".exe"); ################################################################################ ##---------------------------2 - check for directory and writable ################################################################################ //check if the directory exists or not. if (!is_dir("$upload_dir")) { die('Empty directory'); } //check if the directory is writable. if (!is_writeable("$upload_dir")){ die('Non-readable directory'); } ################################################################################ ##---------------------------3-1 - code begins here ################################################################################ if(isset($_POST['uploadform'])){ // if you clicked the (Upload File) button. "If you submitted the form" then upload the file. //begin of uploadform. // $filename will hold the value of the file name submetted from the form. $file_tmp = $_FILES['filetoupload']['tmp_name']; $file_name = $_FILES['filetoupload']['name']; //get extension of file $ext = findexts ($_FILES['filetoupload']['name']); //now I will create a new name for the file //This line assigns a random number to a variable. You could also use a timestamp here if you prefer. $ran = rand () ; //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended. $ran2 = $ran."."; //This assigns the subdirectory you want to save into... make sure it exists! $target = $upload_dir; //This combines the directory, the random file name, and the extension $new_file_name = $target . $ran2.$ext; //check if no file selected. if (!is_uploaded_file($file_tmp)){ die('No file was uploaded'); } //Get the Size of the File $file_size = $_FILES['filetoupload']['size']; //Make sure that file size is correct if ($file_size > $size_bytes){ die('File is too big'); } //check file extension $ext = strrchr($file_name,'.'); if (($extlimit == "yes") && (!in_array(strtolower($ext),$limitedext))) { die('Unsupported file extension'); } // Check if file is Already EXISTS. if(file_exists($upload_dir.$file_name)){ die('File already exists'); } //to remove spaces from file name we have to replace it with "_". $file_name = str_replace(' ', '_', $file_name); $file_name = str_replace('%20', '_', $file_name); $file_name = str_replace('20%', '_', $file_name); //Move the File to the Directory of your choice //move_uploaded_file('filename','destination') Moves afile to a new location. if (move_uploaded_file($file_tmp,$new_file_name)) { //chmod("images/$file_name", 0644); //create session vars so that useredit can process database stuff $_SESSION['upsuccess'] = 'true'; $_SESSION['file_name'] = $new_file_name; //use this to place a link in html //Put here the sql code to store author information header("Location: nextPage.php"); exit(); }else{ // print error if there was a problem moving file. die('Error occured'); } } ?> |
This is a code I use a lot in my webs because it is fairly easy to customize...
If you wanted to store information about the author, create a table and INSERT it where the comment says 'Put here the sql code to store author information'
Now the file 'next.php':
| Code: |
| <?php
//process all other variables you want up here //for example the author... //example var: $author = 'John'; //replace John with results of a database query ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Next page to uploading:</title> </head> <body> <?php if($_SESSION['upsuccess'] == 'true'){ ?> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="93">Name of file:</td> <td width="407"><?php echo $_SESSION['file_name']; ?></td> </tr> <tr> <td>Link to the file:</td> <td><a href="uploads/<?php echo $_SESSION['file_name']; ?>">The link to the file</a></td> </tr> <tr> <td>Other variables...</td> <td><?php echo $author; //use this to display other variables such as author.. ?></td> </tr> <tr> <td>...</td> <td> </td> </tr> </table> <?php }else{ ?> <p>No file was uploaded</p> <?php } ?> </body> </html> |
If you have any problem ask
Thanks. I don't get the sql code part? I'm sorry I'm new
The way I'm understanding this it creates a temporary page for the file? Is it, or is it a persistent page?
The way I'm understanding this it creates a temporary page for the file? Is it, or is it a persistent page?
Sorry I should have explained it more deeply:
What it does is it first 'uploads' a temporary file, then it checks whether it is in the allowed formats (there is an array already set with .doc, .rtf... then it checks for maximum size and all the other requirements there might be.
Once it has passed this validation, it will move it from the temporary folder to wherever you want. In this case it is set to uploads/
What you wanted to do (Also store author, name, description...) needs to be done with a sql query in a database (or in a flat file, but the database is preferable)
For this to work you need to insert this in an html page:
Now that is where the user inputs the file, name, description and author. (You can edit/modify whatever you want here, this is an example)
It then sends the info to processUpload.php Which is the file containing the php code I posted in my first reply.
I will now show you what you need to add to the initial code and where:
Right after:
Insert:
Now I'm suposing the table structure is id | file_name | name | description | creator
Where id is set to primary, auto_increment (that's why it is empty) and the rest are up to you (either varchar, text...)
So to be able to use this and customize it you need to know a bit of MySQL and php. The hard part is already done, now you need to do the rest
Try it, upload it to your server, follow the instructions and paste a link here if it doesn't work, I did it now on the fly so there might be some syntax errors
What it does is it first 'uploads' a temporary file, then it checks whether it is in the allowed formats (there is an array already set with .doc, .rtf... then it checks for maximum size and all the other requirements there might be.
Once it has passed this validation, it will move it from the temporary folder to wherever you want. In this case it is set to uploads/
What you wanted to do (Also store author, name, description...) needs to be done with a sql query in a database (or in a flat file, but the database is preferable)
For this to work you need to insert this in an html page:
| Code: |
| <form action="processUpload.php" method="post" enctype="multipart/form-data" name="upload" id="upload">
<input type="file" name="filetoupload" id="filetoupload" /> <br /> <label> <input type="text" name="name" id="name" /> Name</label> <br /> <label> <input type="text" name="description" id="description" /> Description</label> <br /> <label> <input type="text" name="creator" id="creator" /> Creator</label> <p> </p> <p> <input type="submit" name="uploadform" id="uploadform" value="Upload" /> <br /> </p> </form> |
Now that is where the user inputs the file, name, description and author. (You can edit/modify whatever you want here, this is an example)
It then sends the info to processUpload.php Which is the file containing the php code I posted in my first reply.
I will now show you what you need to add to the initial code and where:
Right after:
| Code: |
| //Put here the sql code to store author information |
Insert:
| Code: |
| function clean($string) {
$string = stripslashes($string); $string = htmlentities($string); $string = strip_tags($string); return $string; } mysql_connect('localhost', 'username', 'password') or die(mysql_error()); //connect to database or throw error mysql_select_db('database_name') or die(mysql_error()); //select database or throw error $sql = "INSERT INTO uploads_table VALUES ('',$new_file_name,clean($_POST['name']),clean($_POST['desription']),clean($_POST['creator']))"; $result = mysql_query($sql); |
Now I'm suposing the table structure is id | file_name | name | description | creator
Where id is set to primary, auto_increment (that's why it is empty) and the rest are up to you (either varchar, text...)
So to be able to use this and customize it you need to know a bit of MySQL and php. The hard part is already done, now you need to do the rest
Try it, upload it to your server, follow the instructions and paste a link here if it doesn't work, I did it now on the fly so there might be some syntax errors
Related topics
