Encoding uploaded file name

My current site allows user file uploads, so, to avoid collisions i need to encode stored file names in some way. But i'd like to be able to retrieve original file names for downloads. So, i'm trying to encode file name to be

  • unique
  • decodable

As of now, i've tried to use following code:

    $hash = $file->hashName();      $fileName = base64_encode(json_encode([         'hash' => $hash,         'name' => $file->getClientOriginalName()     ]));            return $fileName; 

But base64_encode produces longer than 256 symbols strings, especially when file name is using unicode characters (like cyrillic). So, is there any other way to encode file names?


I think it would simpler to do mapping. For saving files on disk you pick some unique name. In database you keep ref about that uploaded file with original file name, and file name on disk and when someone wants to download that file you set original filename in Content-Disposition header.

header('Content-Disposition: filename="originalFilename.pdf"');

Category: php Time: 2016-07-31 Views: 14
Tags: php encode

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.910 (s). 12 q(s)