Link upload files to a MySQL database and then link files
Posted: Tue Aug 07, 2012 9:15 pm
I would like to upload files and assign them to a database field and then query that database with links to those uploaded files. This is as much as I have programmed and now lost. Any help getting this to work would be great. (Thanks to all those who have helped me so far and to YouTube for helping me find sample videos.) I visually have the drop down menu and the upload on the same page. I have the index.php file that has the drop down in php and the form in html. I also have a upload.php wich I would like to have manage the form uploading the file to documents folder and the file name uploaded to the operation_manuals table field manual_name and the spec next to it in the table being the spec chosen from the drop down.
Index.php :
Index.php :
<?php // index.php // create a droptdown menu and upload form // connect upload file name to the dropdown menu field. // Chris Anthony MA 8/6/2012 // // Connect to MySQL Database include('../../conect.php'); //dropdown box of spec see // http://www.learnphp.co/creating-an-html ... using-php/ // for example code. // query the database for the spec. $sql = "SELECT * FROM `canthony`.`spec` AS `spec`"; $query = mysql_query($sql); while ( $results[] = mysql_fetch_object ( $query ) ); array_pop ( $results ); // print_r_html($results); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <title>Uploading files</title> </head> <body> <h1>Uploading files</h1> <hr> <!-- Makes a Horizontal line accross the page --> <!-- Form for selecting spec and file to upload to the operation_manuals table spec to spec and upload to manual_name field. --> <form action='upload.php' method='post' enctype='multipart/form-data'> File: <input type='file' name='upload'> <!-- Uploads file --> <!-- Drop down menu --> <select name="spec_drop"> <?php foreach ( $results as $option ) : ?> <option value="<?php echo $option->spec; ?>"><?php echo $option->spec ; ?></option> <?php endforeach; ?> </select> <input type='submit' name='submit' value='Upload File Now'> <!-- Submit button --> </form> </body> </html>upload.php :
<?php //upload.php // Takes file from index.html form and adds the upload date to the file // and places the file into the documents folder. // The documents folder must already exist. // Chris Anthony MA 8/2/2012 // // // Connect to MySQL Database include('../conect.php'); // Pull upload form index.html if($_POST['submit']){ // Assign uploaded file information to Variables. // Upload listed in this array is the same name as the input name in the form. $name = $_FILES['upload']['name']; $temp = $_FILES['upload']['tmp_name']; $type = $_FILES['upload']['type']; $size = $_FILES['upload']['size']; // Validate variables for error checking echo "$name<br />$temp<br />$type<br />$size"; // File types allowed to be uploaded (.pdf, doc, .docx, .xls, .xtsx, .jpg, and .jpeg) if(($type == 'application/pdf') || ($type == 'application/msword') || ($type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') || ($type == 'application/vnd.ms-excel') || ($type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') || ($type == 'image/jpg') || ($type == 'image/jpeg')){ // Limit file size to 300mb $size is the max file size alloud. if($size <= 314572800){ // echo "The file: $name size is $size"; // Todays date mm_dd_yyyy_ $todaysDate = date("m_j_Y_"); //add the date to the begining of the file name. $newfile = $todaysDate.$name; // echo $newfile; // Move file from memory to documents folder located in the same location as this file. move_uploaded_file($temp,"documents/$newfile"); // echo "<img src='$newfile'>"; // Places the upload as a link into a variable $link = "<a href='documents/$newfile'>$newfile</a>"; // Make Link or way to test $link variable echo $link; // Place write to MySQL. Write $newfile to a database field. // query database to pull spec from Chris Anthony $sql = "SELECT `contacts`.`first_name` AS `Chris`, `contacts`.`last_name` AS `Anthony`, `spec`.`spec` FROM `canthony`.`projectcontact` AS `projectcontact`, `canthony`.`contacts` AS `contacts`, `canthony`.`project_spec` AS `project_spec`, `canthony`.`spec` AS `spec`, `canthony`.`project` AS `project` WHERE `projectcontact`.`Contacts_ID` = `contacts`.`ID` AND `project_spec`.`spec` = `spec`.`spec` AND `project_spec`.`Project_ID` = `project`.`Project_ID` AND `projectcontact`.`Project_ID` = `project`.`Project_ID`"; $result = mysql_query($sql); }else{ // File is bigger the 300mb warning echo "The file: $name is to big....<br />The size is $size and need to be less than 300mb"; } }else{ // File type not allowed warning echo "This type $type is not allowed"; } }else{ // forces back to index.html when trying to open upload.php from browser URL header("Location: index.html"); } ?>