upload files and insert names into mysql
Posted: Wed Mar 24, 2004 10:23 am
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:
Is the name in the db when it displays 'Array' and I just need to use another function or string to extract it? TIA
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>