0

Prior to this link:What is the best way to store media files on a database?

The answer stated:

( Every system I know of that stores large numbers of big files stores them externally to the database. You store all of the queryable data for the file (title, artist, length, etc) in the database, along with a partial path to the file. When it's time to retrieve the file, you extract the file's path, prepend some file root (or URL) to it, and return that. )

My questions are:

a)How do you store the partial path of the file?

b)How do you extract the file's path?

c)How do you prepend some file root and return it?

(Sorry I am very new and this bit I don't quite get. Any input or examples would be lovely.)

Btw, these are my codes for uploading the file, I just don't know the retrieve bit.

<?php
if(isset($_FILES['uploaded_file'])) {   

if($_FILES['uploaded_file']['error'] == 0) {
    // Connect to the database
    $dbLink = new mysqli('localhost', 'root', '', 'spellingbee');
    if(mysqli_connect_errno()) {
        die("MySQL connection failed: ". mysqli_connect_error());
    }

    // Gather all required data
    $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
    $location = $dbLink->real_escape_string($_FILES['uploaded_file']['location']);
    $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
    $size = intval($_FILES['uploaded_file']['size']);

    // Create the SQL query
    $query = "
        INSERT INTO `file` (
            `name`, `location`, `size`, `data`, `created`
        )
        VALUES (
            '{$name}', '{$location}', {$size}, '{$data}', NOW()
        )";

    // Execute the query
    $result = $dbLink->query($query);

    // Check if it was successfull
    if($result) {
        echo 'Success! Your file was successfully added!';
    }
    else {
        echo 'Error! Failed to insert the file'
           . "<pre>{$dbLink->error}</pre>";
    }
}
else {
    echo 'An error accured while the file was being uploaded. '
       . 'Error code: '. intval($_FILES['uploaded_file']['error']);
}

// Close the mysql connection
$dbLink->close();
 }
 else {
echo 'Error! A file was not sent!';
 }

 // Echo a link back to the main page
  echo '<p>Click <a href="form.html">here</a> to go back</p>';
 ?>
Community
  • 1
  • 1
Jialx Ems
  • 27
  • 1
  • 5

1 Answers1

0

As far as I understand your problem you want to upload a audio file and save its name to database and then You want to retrieve it.

To do so just after your all validations (I am writing this code as if want to create a directory)

if(is_dir("audio")
  {}
else
{mkdir("audio");}

$path = "http://www.domain.com/audio/".$_FILES['file']['name']; //prepend any path

// insert in database
move_uploaded_file($_FILES['file']['tmp_name'],$path);

And to retrive it:

just fetch the value of path from database.

Fetch From DB:

  $q = mysql_query("select * from tablename");

  while($r = mysql_fetch_array($q))
  {
     $path = $r['columnNameofPathinDatabase'];
     echo $path;
  }
Rahul
  • 5,594
  • 7
  • 38
  • 92
  • Hi @RahulMishra , I am able to upload , what I need are the codes to fetch the value of path from database. Is it okay if you provide me with a line of codes or so? Thanks! – Jialx Ems Jul 10 '15 at 01:59
  • see i have upadated my answer – Rahul Jul 10 '15 at 06:40