I need help with bulk export of files from database to folder. I have upgraded a system that i made and now instead of storing files to database i store it in folder and save path to database. Now i have a problem because there are around 1500 parts with attached certificates in database. I need to extract them with a script to folder on the server with name rowid.pdf so i know latter which certificate goes where. I have this for now:

$basedir = 'C:\wamp\www\...\\';
$host = 'localhost';
$user = '***'; 
$password= '***'; 
$dbname = '***';

$type = 'pdf';
$blob_id = 'system_batch_number';
$blob_data = 'certificate';
$blob_table = 'parts';

// ************************************************** ***************

$db = mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
mysql_select_db($dbname, $db);

$sql = "SELECT $blob_id, $blob_data FROM $blob_table";
echo $sql;
$result_files = mysql_unbuffered_query($sql);

header('Content-Type: application/pdf');

while ($row = mysql_fetch_array($result_files, MYSQL_ASSOC)) {
$id = $row["$blob_id"];
$data = $row["$blob_data"];
$file = "$basedir$id.$type";
echo "Looking for File: $file<br>";
if (!file_exists($file)) {
echo "Opening File: $file<br>";
echo "Writing File: $file<br>";
echo "Closeing File: $file<br>";
} else {
echo "Skipping File: $file<br>";


It exports everythng like i need only all files are empty and unsopported when i try to open them...

Thank you!