upload files and insert names into mysql

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
hawkmultimedia
Forum Newbie
Posts: 2
Joined: Wed Mar 24, 2004 10:23 am

upload files and insert names into mysql

Post by hawkmultimedia »

I'm not a programmer by any means, but a designer cutting my teeth on coding, so I've piece-milled this script together. It's almost there, but doesn't quite get it. There are title, description, image and audio fields. I'm having problems getting it to upload the image and audio files while inserting the filenames into the db. It seems it will only do one or the other, depending on whether I try the array brackets on the insert form. If I do use the brackets, it will upload but only display "array" in the db instead of the filenames. If I don't, it will insert the filenames, but won't upload. I've been going back and forth for many hours now... :-( Any help would be most appreciated! Here's the code:

Code: Select all

<?php error_reporting(E_ALL & ~E_NOTICE); 
if($HTTP_POST_VARS['Upload'] == "Upload"){
}

function createDBConnection(){

$dbHost = "localhost";
$dbName = "dbname";
$dbUser = "root";
$dbPass = "password"; 

$dbLink = mysql_connect($dbHost, $dbUser, $dbPass);
if (!$dbLink)
die ("Database Error: Couldn`t connect to mySQL Server");
mysql_select_db($dbName, $dbLink) 
or die ("Database Error: Couldn`t open Database: " . $dbName);

return $dbLink;
}

function closeDBConnection($dbLink){
mysql_close($dbLink);
}

if($_POST['Upload'] == "Upload"){

$originalImageFileName = $_FILES['AudioImage']['name'];
$tempImageFileName = $_FILES['AudioImage']['tmp_name'];
$originalAudioFileName = $_FILES['AudioFilename']['name'];
$tempAudioFileName = $_FILES['AudioFilename']['tmp_name'];

$siteRoot = $_SERVER["DOCUMENT_ROOT"];
$uploadDir = $siteRoot . "/data/";
$newImagePath = "/var/www/html/dsp/images/" . $originalImageFileName;
$newAudioPath = "/var/www/html/dsp/audio/" . $originalAudioFileName;

umask(0);
move_uploaded_file($tempImageFileName, $newImagePath);
move_uploaded_file($tempAudioFileName, $newAudioPath);
system("chmod 755 $newPath");

$dbLink = createDBConnection();
$query = "INSERT INTO audio (AudioTitle, AudioDescription, AudioImage, AudioFilename) 
VALUES (' " . $_POST['AudioTitle'] . " ', ' " . $_POST['AudioDescription'] . " ', ' " . $_FILES['AudioImage']['name'] . " ', ' " . $_FILES['AudioFilename']['name'] . " ')";
$dbResult = mysql_query($query, $dbLink)
or die ("Database Error: " . mysql_error() );
$lastInserted = mysql_insert_id();
closeDBConnection($dbLink);
} ?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link href="Library.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="50%"  border="0" cellpadding="0" cellspacing="0" class="main">
  <tr>
    <td><form action="<?php echo $_SERVER['SCRIPT_URI']; ?>" method="post" enctype="multipart/form-data" name="Upload" id="Upload">
      <p align="right"> Title:
          <input name="AudioTitle" type="text" id="AudioTitle" size="40">
      </p>
      <p align="right"> Description:
          <textarea name="AudioDescription" cols="36" id="AudioDescription"></textarea>
          <input name="MAX_FILE_SIZE" type="hidden" id="MAX_FILE_SIZE" value="6000000">
      </p>
      <p align="right"> Image:
          <input name="AudioImage" type="file" id="AudioImage" size="28">
          <br>
    Audio File:
    <input name="AudioFilename" type="file" id="AudioFilename" size="28">
      </p>
      <p align="right">
        <input name="Upload" type="submit" id="Upload" value="Upload">
      </p>
    </form></td>
  </tr>
</table>

</body>
</html>
Is the name in the db when it displays 'Array' and I just need to use another function or string to extract it? TIA
compound_eye
Forum Newbie
Posts: 15
Joined: Wed Mar 17, 2004 8:42 pm

Post by compound_eye »

hello, i'm trying to work this out, could you show the version of the code that will upload, and the version that will save the name?
User avatar
hawkmultimedia
Forum Newbie
Posts: 2
Joined: Wed Mar 24, 2004 10:23 am

Post by hawkmultimedia »

Actually, the script I posted was correct and is working fine now. The incorrect one used:

<td><form action="<?php echo $_SERVER['SCRIPT_URI']; ?>" method="post" enctype="multipart/form-data" name="Upload" id="Upload">
<p align="right"> Title:
<input name="AudioTitle" type="text" id="AudioTitle" size="40">
</p>
<p align="right"> Description:
<textarea name="AudioDescription" cols="36" id="AudioDescription"></textarea>
<input name="MAX_FILE_SIZE" type="hidden" id="MAX_FILE_SIZE" value="6000000">
</p>
<p align="right"> Image:
<input name="AudioImage[]" type="file" id="AudioImage" size="28">
<br>
Audio File:
<input name="AudioFilename[]" type="file" id="AudioFilename" size="28">
</p>
<p align="right">
<input name="Upload" type="submit" id="Upload" value="Upload">
</p>
</form></td>

Thanks
Post Reply