Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I handle file metadata on the database and retrive the files with a UUID. What i do is:</p> <ol> <li><strong>Content based identification</strong> <ol> <li>MD5 from file's content</li> <li>Namespaced UUID:v5 to generate unique identifier based on user's uuid and file's md5. </li> <li>Custom function to generate path based on 'realname'.</li> <li>Save on the database: uuid, originalname (the uploaded name), realname (the generated name), filesize, and mime. (optional dateAdded, and md5)</li> </ol></li> <li><strong>File retrival.</strong> <ol> <li>UUID to retrive metadata.</li> <li>regenerate filepath based on realname.</li> <li>Originalname is used to show a familiar name to the user that downloads the file.</li> </ol></li> </ol> <p>I process the file's name assigning it a namespaced UUID as the database primary key, and Generate the path based on User and filename. The precondition is that your user has a uuid assigned to him. The following code will help you avoid id collisions on the database, and help you identify files by its contents (If you ever need to have a way to spot duplicate content and not necesarily filenames).</p> <pre><code>$fileInfo = pathinfo($_FILE['file']['name']); $extension = (isset($fileInfo['extension']))?".".$fileInfo['extension']:""; $md5Name = md5_file($_FILE['file']['tmp_name']); //you could use other hash algorithms if you are so inclined. $realName = UUID::v5($user-&gt;uuid, $md5Name) . $extension; //UUID::v5(namespace, value). </code></pre> <p>I use a function to generate the filepath based on some custom parameteres, you could use $username and $realname. This is helpful if you implement a distributed folder structure which you might have partitioned on file naming scheme, or any custom scheme.</p> <pre><code>function generateBasePath($realname, $customArgsArray){ //Process Args as your requirements. //might as well be "$FirstThreeCharsFromRealname/" //or a checksum that helps you decide which drive/volume/mountpoint to use. //like some files on the local disk and some other from an Amazon::S3 mountpoint. return $mountpoint.'/'.$generatedPath; } </code></pre> <p>As an added bonus this also:</p> <ol> <li>helps you maintain a versioned file repository if you add an attribute on the file's record of which file (uuid) it has replaced.</li> <li>create a application Access Control List if you add an attributes of 'owner' and/or 'group'</li> <li>also works on a single folder structure.</li> </ol> <p>Note: I used php's $_FILE as an example of the file source based on this question's tags. It can be from any file source or generated content.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload