Jump to content

Uploading files as part of a form


gazfocus

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/137412-uploading-files-as-part-of-a-form/
Share on other sites

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>

 

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>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.