gazfocus Posted December 17, 2008 Share Posted December 17, 2008 I am building a website for my church and one of the pages needs to have an upload page where the sound engineer can upload the sermon mp3's and powerpoint slides. This needs to be done as part of a form submission so that the sound engineer can also add the name of the sermon, name of the speaker, and an automatically generated download link which will then be stored in a table on a MySQL database. Is this possible? What would the code be for the upload boxes (considering I need 2 upload boxes in the one form). Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/137412-uploading-files-as-part-of-a-form/ Share on other sites More sharing options...
twm Posted December 17, 2008 Share Posted December 17, 2008 there is a lot of tutorials online how to do it. Google php file upload and you will have plenty to choose from. from the php man pages: http://us3.php.net/features.file-upload Quote Link to comment https://forums.phpfreaks.com/topic/137412-uploading-files-as-part-of-a-form/#findComment-718070 Share on other sites More sharing options...
gazfocus Posted December 17, 2008 Author Share Posted December 17, 2008 I have got as far as getting the form made with the 2 upload buttons and once submitted I am at the testing stage so I am storing all the elements in variables and echoing them to make sure they've worked, however, anything to do with the $_FILES doesn't echo. Can anyone help? Here's my code... (I have also tried it with ' ' around the stuff in square brackets) <html> <head> <SCRIPT TYPE="text/javascript"> </SCRIPT> <link href="/joomla/templates/at_flexmen/css/template_css.css" rel="stylesheet" type="text/css"> </head> <body> <?php ////////////////////////////////////////// //// MySQL Database Connection /////////// ////////////////////////////////////////// $host = "xxxxx"; $user = "xxxxx"; $db_name= "xxxxx"; $pass= "xxxxx"; $conn = mysql_connect($host, $user, $pass) or die(mysql_error()); mysql_select_db($db_name, $conn) or die(mysql_error()); if ($_POST[viewed] != "yes") { echo "<form name=\"form1\" method=\"post\" action=\"$SERVER[php_SELF]\"> <fieldset class=\"input\"> <table width=\"200\" border=\"0\"> <tr> <td valign=\"top\"><input name=\"title\" type=\"text\" id=\"title\" tabindex=\"1\" class=\"inputbox\" style=\"font-size:12px\" value=\"Sermon Title\" size=\"40\" maxlength=\"50\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"></td> </tr> <tr> <td valign=\"top\"><input name=\"speaker\" type=\"text\" id=\"speaker\" tabindex=\"2\" class=\"inputbox\" style=\"font-size:12px\" value=\"Speaker\" size=\"40\" maxlength=\"50\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"></td> </tr> <tr> <td height=\"27\" valign=\"top\"> <input name=\"dd\" type=\"text\" id=\"dd\" tabindex=\"3\" class=\"inputbox\" style=\"font-size:12px\" value=\"dd\" size=\"5\" maxlength=\"2\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"> <input name=\"mm\" type=\"text\" id=\"mm\" tabindex=\"4\" class=\"inputbox\" style=\"font-size:12px\" value=\"mm\" size=\"5\" maxlength=\"2\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"> <input name=\"yyyy\" type=\"text\" id=\"yyyy\" tabindex=\"5\" class=\"inputbox\" style=\"font-size:12px\" value=\"yyyy\" size=\"10\" maxlength=\"4\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"></td> </tr> <tr> <td><br /><p style=\"font-size:12px; color:#b53a04; background-color:#e3e8eb\"><b>MP3 File</b></p><input name=\"mp3\" type=\"file\" id=\"mp3\" tabindex=\"6\"></td> </tr> <tr> <td><br /><p style=\"font-size:12px; color:#b53a04; background-color:#e3e8eb\"><b>Sermon Notes</b></p><input name=\"notes\" type=\"file\" id=\"notes\" tabindex=\"7\"></td> </tr> <tr> <td valine=\"top\"><br /><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Add Event\" class=\"button\" style=\"font-size:12px\" /> <input type=\"reset\" name=\"reset\" id=\"reset\" value=\"Reset\" class=\"button\" style=\"font-size:12px\" /><br /><br /><br /></td> </tr> <tr> <td><input name=\"viewed\" type=\"hidden\" id=\"viewed\" value=\"yes\"></td> </tr> </table> </form>"; } else { $title = $_POST[title]; $speaker = $_POST[speaker]; $sermonDate = $_POST[yyyy]."-".$_POST[mm]."-".$_POST[dd]; $mp3 = $_FILES[mp3]; $notes = $_FILES[notes]; $mp3Size = $_FILES[mp3][size]; $notesSize = $_FILES[notes][size]; $notesType = $_FILES[notes][type]; $uploadmp3 = 'files/mp3/'; $uploadnotes = 'files/notes'; echo $title . "<br />"; echo $speaker . "<br />"; echo $sermonDate . "<br />"; echo $mp3 . "<br />"; echo $notes . "<br />"; echo $mp3Size . "<br />"; echo $notesSize . "<br />"; echo $notesType . "<br />"; echo $uploadmp3 . "<br />"; echo $uploadnotes . "<br />"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/137412-uploading-files-as-part-of-a-form/#findComment-718238 Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 Add this to your <form tag enctype="multipart/form-data" Quote Link to comment https://forums.phpfreaks.com/topic/137412-uploading-files-as-part-of-a-form/#findComment-718255 Share on other sites More sharing options...
gazfocus Posted December 17, 2008 Author Share Posted December 17, 2008 Ok, I'm now trying to move the file to the directory I want, and I have put an IF function so that if the file uploads, it displays a message saying the file uploaded, but even though I'm getting that message, the file is not where it should be. Here's my code... <html> <head> <SCRIPT TYPE="text/javascript"> </SCRIPT> <link href="/joomla/templates/at_flexmen/css/template_css.css" rel="stylesheet" type="text/css"> </head> <body> <?php ////////////////////////////////////////// //// MySQL Database Connection /////////// ////////////////////////////////////////// $host = "xxxxx"; $user = "xxxxx"; $db_name= "xxxxx"; $pass= "xxxxx"; $conn = mysql_connect($host, $user, $pass) or die(mysql_error()); mysql_select_db($db_name, $conn) or die(mysql_error()); if ($_POST[viewed] != "yes") { echo "<form name=\"form1\" method=\"post\" enctype=\"multipart/form-data\" action=\"$SERVER[php_SELF]\"> <fieldset class=\"input\"> <table width=\"200\" border=\"0\"> <tr> <td valign=\"top\"><input name=\"title\" type=\"text\" id=\"title\" tabindex=\"1\" class=\"inputbox\" style=\"font-size:12px\" value=\"Sermon Title\" size=\"40\" maxlength=\"50\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"></td> </tr> <tr> <td valign=\"top\"><input name=\"speaker\" type=\"text\" id=\"speaker\" tabindex=\"2\" class=\"inputbox\" style=\"font-size:12px\" value=\"Speaker\" size=\"40\" maxlength=\"50\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"></td> </tr> <tr> <td height=\"27\" valign=\"top\"> <input name=\"dd\" type=\"text\" id=\"dd\" tabindex=\"3\" class=\"inputbox\" style=\"font-size:12px\" value=\"dd\" size=\"5\" maxlength=\"2\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"> <input name=\"mm\" type=\"text\" id=\"mm\" tabindex=\"4\" class=\"inputbox\" style=\"font-size:12px\" value=\"mm\" size=\"5\" maxlength=\"2\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"> <input name=\"yyyy\" type=\"text\" id=\"yyyy\" tabindex=\"5\" class=\"inputbox\" style=\"font-size:12px\" value=\"yyyy\" size=\"10\" maxlength=\"4\" onfocus=\"this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;\"></td> </tr> <tr> <td><br /><p style=\"font-size:12px; color:#b53a04; background-color:#e3e8eb\"><b>MP3 File</b></p><input name=\"mp3\" type=\"file\" id=\"mp3\" tabindex=\"6\"></td> </tr> <tr> <td><br /><p style=\"font-size:12px; color:#b53a04; background-color:#e3e8eb\"><b>Sermon Notes</b></p><input name=\"notes\" type=\"file\" id=\"notes\" tabindex=\"7\"></td> </tr> <tr> <td valine=\"top\"><br /><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Upload\" class=\"button\" style=\"font-size:12px\" /> <input type=\"reset\" name=\"reset\" id=\"reset\" value=\"Reset\" class=\"button\" style=\"font-size:12px\" /><br /><br /><br /></td> </tr> <tr> <td><input name=\"viewed\" type=\"hidden\" id=\"viewed\" value=\"yes\"></td> </tr> </table> </form>"; } else { $title = $_POST[title]; $speaker = $_POST[speaker]; $sermonDate = $_POST[yyyy]."-".$_POST[mm]."-".$_POST[dd]; $mp3 = $_FILES[mp3][name]; $notes = $_FILES[notes][name]; $mp3Size = $_FILES[mp3]; $notesSize = $_FILES[notes]; $notesType = $_FILES[notes][type]; $uploadmp3 = 'files/mp3/'; $uploadnotes = 'files/notes'; if (move_uploaded_file($_FILES['mp3']['tmp_name'],$uploadmp3.$mp3)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Possible file upload attack!\n"; } echo $title . "<br />"; echo $speaker . "<br />"; echo $sermonDate . "<br />"; echo $mp3 . "<br />"; echo $notes . "<br />"; echo $mp3Size . "<br />"; echo $notesSize . "<br />"; echo $notesType . "<br />"; echo $uploadmp3 . "<br />"; echo $uploadnotes . "<br />"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/137412-uploading-files-as-part-of-a-form/#findComment-718285 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.