gavenf Posted April 9, 2007 Share Posted April 9, 2007 Hi guys, I have written a form that accepts information and a file. It currently writes the file to the database table also but I would like it to write the file to a seperate folder but keep the relationship with the other information so it can be searched and retreived later. Also how would I go about allowing for mulitple files to be uploaded and linked to the table information? Here is the scrip I wrote for the upload to happen: <?php mysql_connect("localhost","teacher","slider1234"); mysql_select_db("planitteacher"); $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); $result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype,title,subject,class,web1,email) ". "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type','$form_title','$form_subject','$form_class','$form_web1','$form_email')"); $id= mysql_insert_id(); print "<p>File ID: <b>$id</b><br>"; print "<p>File Name: <b>$form_data_name</b><br>"; print "<p>File Size: <b>$form_data_size</b><br>"; print "<p>File Type: <b>$form_data_type</b><p>"; print "To upload another file <a href=http://www.careerposition.org> Click Here</a>"; ?> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 9, 2007 Share Posted April 9, 2007 either add a field for the path or just change script so the filename so it includes the path.. thats code isn't much help as its only the insert into the database.. your need to include the upload routine.. (which in this case is the key as you need to handle folders) Quote Link to comment Share on other sites More sharing options...
gavenf Posted April 9, 2007 Author Share Posted April 9, 2007 That being said I dont know how to do that. Thats what I am saying. I have tried but cant get it to work. Any help would be great as I have only learnt PHP for the last two weeks. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 9, 2007 Share Posted April 9, 2007 can you post the code for uploading the files then.. Quote Link to comment Share on other sites More sharing options...
gavenf Posted April 9, 2007 Author Share Posted April 9, 2007 here is the form I use: I hope its what you want: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>Description</title> </head> <body> <table border="1" width="550" height="124"> <tr> <td height="47" width="301"> </td> <td height="47" width="233"><form method="post" action="upload4.php" enctype="multipart/form-data"> <p>Title:<br> <input type="text" name="form_title" size="40"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> </p> <p>Description:</p> <p> <input type="text" name="form_description" size="40"></p> <p> Curriculum:</p> <p> <font face="Arial" size="2"> <select name="form_class" size="1"> <option value="Pre-School">PreSchool</option> <option value="Prep">Prep</option> <option value="Grade 1">Grade 1</option> <option value="Grade 2">Grade 2</option> <option value="Grade 3">Grade 3</option> <option value="Grade 4">Grade 4</option> <option value="Grade 5">Grade 5</option> <option value="Grade 6">Grade 6</option> <option value="Year 7">Year 7</option> <option value="Year 8">Year 8</option> <option value="Year 9">Year 9</option> <option value="Year 10">Year 10</option> <option value="Year 11">Year 11</option> <option value="Year 12">Year 12</option> <option value="Special Needs">Special Needs</option> <option value="Other">Other</option> </select></font></p> <p> Subject Area:</p> <p> <font face="Arial" size="2"> <select name="form_subject" size="1"> <c="Aboriginal Studies ">Aboriginal Studies </option> <option value="Accounting ">Accounting </option> <option value="Agriculture ">Agriculture </option> <option value="Biology ">Biology </option> <option value="Business Services ">Business Services </option> <option value="Business Studies ">Business Studies </option> <option value="Ceramics ">Ceramics </option> <option value="Chemistry ">Chemistry </option> <option value="Computing Applications ">Computing Applications </option> <option value="Community & Family Studies ">Community & Family Studies </option> <option value="Construction ">Construction </option> <option value="Dance ">Dance </option> <option value="Design & Technology ">Design & Technology </option> <option value="Drama ">Drama </option> <option value="Entertainment ">Entertainment </option> <option value="Exploring Early Childhood ">Exploring Early Childhood </option> <option value="Earth and Environmental Science ">Earth and Environmental Science </option> <option value="Economics ">Economics </option> <option value="Electronics Technology ">Electronics Technology </option> <option value="Engineering Studies ">Engineering Studies </option> <option value="English ">English </option> <option value="Food Technology ">Food Technology </option> <option value="Furnishing ">Furnishing </option> <option value="Geography ">Geography </option> <option value="History ">History </option> <option value="Hospitality ">Hospitality </option> <option value="Industrial Technology ">Industrial Technology </option> <option value="Information Processes and Technology ">Information Processes and Technology </option> <option value="Information Technology ">Information Technology </option> <option value="Languages ">Languages </option> <option value="Legal Studies ">Legal Studies </option> <option value="Life Skills Courses - Special Program of Study ">Life Skills Courses - Special Program of Study </option> <option value="Marine Studies ">Marine Studies </option> <option value="Mathematics ">Mathematics </option> <option value="Metal & Engineering ">Metal & Engineering </option> <option value="Music ">Music </option> <option value="Personal Development, Health and Physical Education (PDHPE) ">Personal Development, Health and Physical Education (PDHPE) </option> <option value="Photography, Video and Digital Imaging ">Photography, Video and Digital Imaging </option> <option value="Physics ">Physics </option> <option value="Primary Industries ">Primary Industries </option> <option value="Retail ">Retail </option> <option value="Senior Science ">Senior Science </option> <option value="Society and Culture ">Society and Culture </option> <option value="Software Design & Development ">Software Design & Development </option> <option value="Sport, Lifestyle and Recreation ">Sport, Lifestyle and Recreation </option> <option value="Studies of Religion ">Studies of Religion </option> <option value="Textiles & Design ">Textiles & Design </option> <option value="Tourism ">Tourism </option> <option value="Visual Arts ">Visual Arts </option> <option value="Visual Design ">Visual Design </option> <option value="Work Studies ">Work Studies </option> </select></font></p> <p> Web Link:</p> <p> <input type="text" name="form_web1" size="40"></p> <p> Email:</p> <p> <input type="text" name="form_email" size="40"><br> </p> <p> File to upload:<br> <input type="file" name="form_data" size="40"> </p> <p><input type="submit" name="submit" value="submit"> </form> </td> </tr> <tr> <td height="67" width="301"> </td> <td height="67" width="233"> </td> </tr> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 9, 2007 Share Posted April 9, 2007 Ahhhh next time please post code within the [ code] tags and erm.. the file needed is upload4.php Quote Link to comment Share on other sites More sharing options...
gavenf Posted April 9, 2007 Author Share Posted April 9, 2007 Ahhhh next time please post code within the [ code] tags and erm.. the file needed is upload4.php <?php mysql_connect("localhost","teacher","slider1234"); mysql_select_db("planitteacher"); $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); $result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype,title,subject,class,web1,email) ". "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type','$form_title','$form_subject','$form_class','$form_web1','$form_email')"); $id= mysql_insert_id(); print "<p>File ID: <b>$id</b><br>"; print "<p>File Name: <b>$form_data_name</b><br>"; print "<p>File Size: <b>$form_data_size</b><br>"; print "<p>File Type: <b>$form_data_type</b><p>"; print "To upload another file <a href=http://www.careerposition.org> Click Here</a>"; ?> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 9, 2007 Share Posted April 9, 2007 your need to add a few more to the form and <input type="file" name="form_data" size="40"> repeat this statement for multiple uploads <?php $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); $result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype,title,subject,class,web1,email) ". "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type','$form_title','$form_subject','$form_class','$form_web1','$form_email')"); ?> ie <input type="file" name="form_data" size="40"> <input type="file" name="form_data2" size="40"> <input type="file" name="form_data3" size="40"> <?php $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); $data2 = addslashes(fread(fopen($form_data2, "r"), filesize($form_data2))); $data3 = addslashes(fread(fopen($form_data3, "r"), filesize($form_data3))); $result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype,title,subject,class,web1,email) ". "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type','$form_title','$form_subject','$form_class','$form_web1','$form_email')"); $result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype,title,subject,class,web1,email) ". "VALUES ('$form_description','$data2','$form_data_name2','$form_data_size2','$form_data_type2','$form_title','$form_subject','$form_class','$form_web1','$form_email')"); $result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype,title,subject,class,web1,email) ". "VALUES ('$form_description','$data3','$form_data_name3','$form_data_size3','$form_data_type3','$form_title','$form_subject','$form_class','$form_web1','$form_email')"); ?> BUT i think you may want to make another table for holding the files and use a referance to link to the user as a note your writing to the database and not folders so your need another field to handle the folder name... i am not sure what your goal is but personally i don't like storing files in a database but thats just me personally Quote Link to comment Share on other sites More sharing options...
gavenf Posted April 9, 2007 Author Share Posted April 9, 2007 Thanks I got that working but how would I write the code to make the actual files I was uploading be saved in another table or file? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 9, 2007 Share Posted April 9, 2007 for another table just create the table and change the statment something like $result=MYSQL_QUERY("INSERT INTO newtable (data,filename,filesize,filetype) ". "VALUES ('$data','$form_data_name','$form_data_size','$form_data_type')"); for uploading the file to a folder your need to use the function move_uploaded_file. if i have i'll writeup a basic how to Quote Link to comment Share on other sites More sharing options...
gavenf Posted April 9, 2007 Author Share Posted April 9, 2007 YOU ROCK. Thnaks for your help. If I could have a "how to" that woul dbe great. I will try to use what you have said. Looks complicated but I am determined to learn PHP. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 9, 2007 Share Posted April 9, 2007 OK i haven't tested this but heres a basic idea of what you want (without folders) save the following to 1 file ie upload.php create a table called 'images' (for example, it must have a string field called 'File' now in the same place as this script create a folder called 'upload' this must be writeable.. i hope it works <?php //databse settings $db = "database name"; $host = "localhost"; $user = "dbuser"; $pass = "dbpass"; $dbTable = "images"; // <-- must have a field called File (string) //check that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && ($_FILES["uploaded_file"]["size"] < 350000)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/upload/'.$filename; //<--- NEEDS TO BE WRITEABLE //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { echo "It's done! The file has been saved as: ".$newname; $id = @mysql_pconnect($host, $user, $pass) or MySQL_ErrorMsg("Unable to connect to MySQL server: $host : '$SERVER_NAME'"); @mysql_select_db($db, $id) or MySQL_ErrorMsg ("Unable to select database: $db"); $query = "INSERT INTO $dbTable (`File`) VALUES ('$filename') ") $result = @mysql_query($query, $id) or MySQL_ErrorMsg ("Unable to perform insert: $query"); } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only .jpg images under 350Kb are accepted for upload"; } } else { echo "Error: No file uploaded"; } ?> <html> <body> <form enctype="multipart/form-data" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> Choose a file to upload: <input name="uploaded_file" type="file" /> <input type="submit" value="Upload" /> </form> </body> </html> sorry i don't have time to test and add folders but i have a dead line .. Quote Link to comment Share on other sites More sharing options...
gavenf Posted April 9, 2007 Author Share Posted April 9, 2007 Thanks. Not sure what the code is as it only lest me upload images. Also the form is totally different and now only has the file upload box. I will have a look through it though and try to sort it out. thanks again. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 9, 2007 Share Posted April 9, 2007 you said you was learning i commented the whole thing you should beable to work it out <?php $ext = substr($filename, strrpos($filename, '.') + 1); if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && ($_FILES["uploaded_file"]["size"] < 350000)) { ?> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 9, 2007 Share Posted April 9, 2007 Oh if you want time light reading check this out http://www.phpfreaks.com/tutorials/85/0.php Quote Link to comment Share on other sites More sharing options...
gavenf Posted April 9, 2007 Author Share Posted April 9, 2007 Very cool. Thanks I will read it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.