Hello guys!

Anyone that has an idea on how to store audio files in any format in MySQL database and downloading them when clicked during the retrieval or display of the said audio file, please help me. I've been looking it in the internet but mostly just storing images and retrieving them from the database. No storing and downloading of audio files. If you have an idea please help or please do give me a copy of your code if you have.
Thanks a lot.

Cheers to everyone! :)

Dani AI

Generated

This thread asks how to keep audio files and let users download them. advocated keeping binaries out of the DB and pointed at PHP upload helpers. Both are common starting points. Below are practical choices, a concise DB-based example (when you must store audio in MySQL), and key warnings so the solution will actually work in production.

A minimal table for storing audio as a BLOB:

CREATE TABLE audio (
  id INT AUTO_INCREMENT PRIMARY KEY,
  filename VARCHAR(255),
  mime VARCHAR(100),
  size INT UNSIGNED,
  data LONGBLOB,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Upload (PDO, stream file into a LONGBLOB without copying the whole file into PHP memory):

$fp = fopen($_FILES['file']['tmp_name'], 'rb');
$stmt = $pdo->prepare("INSERT INTO audio (filename,mime,size,data) VALUES (?, ?, ?, ?)");
$stmt->bindParam(1, $_FILES['file']['name'], PDO::PARAM_STR);
$stmt->bindParam(2, $_FILES['file']['type'], PDO::PARAM_STR);
$stmt->bindParam(3, $_FILES['file']['size'], PDO::PARAM_INT);
$stmt->bindParam(4, $fp, PDO::PARAM_LOB);
$stmt->execute();
fclose($fp);

Serve/download (simple example — add Range support for in-browser seeking if you need streaming):

$stmt = $pdo->prepare("SELECT filename,mime,size,data FROM audio WHERE id=?");
$stmt->execute([$id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
header('Content-Type: ' . $row['mime']);
header('Content-Length: ' . $row['size']);
header('Content-Disposition: attachment; filename="' . basename($row['filename']) . '"');
echo $row['data'];

Important notes and troubleshooting

  • Use LONGBLOB for large audio; MEDIUMBLOB and BLOB have much smaller limits (16 MB and 64 KB roughly).
  • Tune PHP (upload_max_filesize, post_max_size, memory_limit) and MySQL (max_allowed_packet) for big uploads. “Packet too large” errors mean max_allowed_packet needs raising.
  • Security and correctness: validate file type (finfo), limit size, sanitize filenames, and use prepared statements.
  • For many or large files prefer external/object storage (S3 or similar) and store only metadata and URLs in MySQL — simpler backups, better performance, and easier CDN integration.

If storing in DB is required, the snippets above will work after adjusting configs; otherwise use external storage for scalability and reliability.

Recommended Answers

All 4 Replies

It's better to store audio files in dir on your server and put path in MySQL field.


- Mitko Kostov

you can use move_uploaded_file

Thanks for your response. I'll try to implement the said function. Thank you very much. If anyone does have a different idea please help. :)

how to store music files in a database and retireve dem???

commented: If you are going to bump a 2 year old thread, at least make a contribution... +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.