Jump to content

Uploading files to MySQL using PHP


AndrewJ1313

Recommended Posts

Good day. I am attempting to create a script to upload MP3 files to a remote MySQL 4.1.22 database. I wasn't quite sure how to do this so I googled it and came across a few tuturials and have tried following them. Everything appears to work fine, but after the web page has reloaded, the file was not uploaded to the database or anywhere for that matter. I am using PHP 5 and MySQL 4.1.22. Here is the code that I am using in my *.php file:

 

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

$query = "INSERT INTO tbl_sermons (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed'); 

echo "<br>File $fileName uploaded<br>";
} 
?>

    <form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr> 
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="4000000">
<input name="userfile" type="file" id="userfile"> 
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>

 

Again, this is code I learned from an online tutorial I came across, if anyone can see what I might have missed or has another suggestion about how to accomplish this I would greatly appreciate it.

 

Cheers,

Andrew

Link to comment
https://forums.phpfreaks.com/topic/113340-uploading-files-to-mysql-using-php/
Share on other sites

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.