PHP - alternative to Base64 with shorter results?

kursus

I'm currently using base64 to encode a short string and decode it later, and wonder if a better (shorter) alternative is possible.

$string = '/path/to/img/image.jpg';
$convertedString = base64_encode($string);  

// New session, new user
$convertedString = 'L3BhdGgvdG8vaW1nL2ltYWdlLmpwZw=='; 
$originalString = base64_decode('L3BhdGgvdG8vaW1nL2ltYWdlLmpwZw==');

// Can $convertedString be shorter by any means ? 

Requirements :

  • Shorter result possible
  • Must be reversible any time in a different session (therefore unique)
  • No security needed (anyone can guess it)
  • Any kind of characters that can be used in a URL (except slashes)
  • Can be an external lib

Goal :

Get a clean unique id from a path file that is not the path file and can be used in a URL, without using a database.

I've searched and read a lot, looks like it doesn't exist but couldn't find a definitive answer.

Austin

Well since you're using these in a URL, why not use rawurlencode($string) and rawurldecode($encodedString)?

If you can reserve one character like - (i.e., ensure that - never appears in your file names), you can do even better by doing rawurlencode(str_replace('/', '-', $string)) and str_replace('-', '/', rawurldecode($encodedString)). Depending on the file names you pick, this will create IDs that are the same length as the original filename. (This won't work if your file names have multi-byte characters in them; you will need to use some mb_* functions for that case.)

You could try using compression functions, but for strings as short as file paths, compression usually makes the output larger than the input.

Ultimately, unless you are willing to use a database, disallow certain file names, or you know something about what kinds of file names will come up, the best you can hope for is IDs that are as short or almost as short as the original file names. Otherwise, this would be a universal compression function, which is impossible.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

base64 to hex inconsistent results

From Dev

Unusual results for Base64 decode NSString

From Dev

Odd results on base64 operations

From Dev

base64 encode is giving ambigious results

From Dev

base64 to hex inconsistent results

From Dev

sendgrid multipart/alternative image from base64

From Dev

Is there any shorter alternative to dispatch_once?

From Dev

Shorter alternative for CHAR(9) (tab) in Excel formula

From Dev

Shorter PHP Script

From Dev

Decode base64 string - php

From Dev

Base64 decode image PHP

From Dev

base64 Decode into PHP Array

From Dev

PHP base64 encode a pdf file

From Dev

android Convert images to base64 for php

From Dev

base64 Decode into PHP Array

From Dev

PHP Alter Base64 Encoding of image

From Dev

Converted base64 file not working in PHP

From Dev

PHP: Convert base64 to image

From Dev

BASE64 decode and ungzip data in PHP

From Dev

Swift 2 base64 Encoding Yield Different Results

From Dev

How to Convert Cordova Image Picker Results to Base64 format?

From Dev

The sqlx library gives weird base64 encoded looking results

From Dev

different results when I encode a character to base64

From Dev

Decode base64 encoded file and print results to the console

From Dev

Uploading base64 results in a outofmemory error in a Android app

From Dev

How to Convert Cordova Image Picker Results to Base64 format?

From Dev

Android encoding PNG to base64 string producing invalid results

From Dev

PHP base64_encode returning incorrect results

From Dev

Php base base64 encode and decode not working correctly