PHP Encoded File From Database - php

I would like to know, if any of you had similar problem and, if yes, how did you fixed it?
The problem is about encoded file in the database base. I have to retrieve the file and save it as regular file in system storage.
But I don't really know, why, but I can't decode it. It looks like standard base64, but seems it's actually not.
So part of the file (maybe someone will find out which algorithm is used here):
0M8R4KGxGuFcMFwwXDBcMFwwXDBcMFwwXDBcMFwwXDBcMFwwXDBcMD5cMANcMP7/CVwwBlwwXDBcMFwwXDBcMFwwXDBcMFwwXDADXDBcMFwwPAFcMFwwXDBcMFwwXDBcMBBcMFwwPgFcMFwwAVwwXDBcMP7///9cMFwwXDBcMDkBXDBcMDoBXDBcMDsBXDBcMP/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////spcFcMH9gFQRcMFww8BK/XDBcMFwwXDBcMFwwEFwwXDBcMFwwXDAGXDBcMGrnAVwwDlwwYmpiauaH5odcMFwwXDBcMFwwXDBcMFwwXDBcMFwwXDBcMFwwXDBcMFwwXDAVBBZcMC7+AVwwhO1cMFwwhO1cMFwwEd9cMFwwXDBcMFwwXDAmXDBcMFwwXDBcMFwwXDBcMFwwXDBcMFwwXDBcMFwwXDBcMFwwXDBcMFwwXDBcMP//D1wwXDBcMFwwXDBcMFwwXDBcMP//D1wwXDBcMFwwXDBcMFwwXDBcMP//D1wwXDBcMFwwXDBcMFwwXDBcMFwwXDBcMFwwXDBcMFwwXDCkXDBcMFwwXDBcMGAEXDBcMFwwXDBcMFwwYARcMFwwYARcMFwwXDBcMFwwXDBgBFwwXDBcMFwwXDBcMGAEXDBcMFwwXDBcMFwwYARcMFwwXDBcMFwwXDBgBFwwXDAUXDBcMFwwXDBcMFwwXDBcMFwwXDBcMHQEXDBcMFwwXDBcMFwwPEBcMFwwXDBcMFwwXDA8QFwwXDBcMFwwXDBcMDxAXDBcM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˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙ěĽÁ\0`\0\0đż\0\0\0\0\0\0\0\0\0\0\0\0\0jç\0\0bjbjćć\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0.ţ\0í\0\0í\0\0ß\0\0\0\0\0\0&\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0˙˙\0\0\0\0\0\0\0\0\0˙˙\0\0\0\0\0\0\0\0\0˙˙\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0¤\0\0\0\0\0`\0\0\0\0\0\0`\0\0`\0\0\0\0\0\0`\0\0\0\0\0\0`\0\0\0\0\0\0`\0\0\0\0\0\0`\0\0\0\0\0\0\0\0\0\0\0\0\0t\0\0\0\0\0\0<#\0\0\0\0\0\0<#\0\0\0\0\0\0<#\0\08\0\0\0t#\0\0D\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system.
file_put_contents($path, $data);
Variables names, says all.
$path is just a path to place where the file will be saved and $data is just an variable with encoded data inside. (Of course I have mime type of the file, so if it is, let's say .doc, path will be "something/something/upload/file.doc")
But what I get is just file with base64's encoded string.
So PHP Community, I need your help!

Related

utf8_encode does not encode special characters ě/š/č/ř/ž/ý/á, etc

I have the following problem which seems to have no solution and I am absolutely disgusted.
I have Android application where users can upload file to my server and then they can access them. So if user opens his account, this application uses function scandir() and on my server I use method json_encode() to send data to my app to shows him his files and folders. And here is the problem:
If some user for example uploads file with special characters (Válcování stupHovitých vzorko za tepla.pptx) and this file is not utf-8 encoded, then I can't pass it via json_encode, because I get UTF-8 error. So I tried to use method uf8_encode() on each file name and it worked, BUT if there is some file or folder with special characters like č/š/ě/ř/ž/á/ý/í/é, etc. and use method utf8_encode() on it then I get some mess in my application and instead of getting folder with name č, I get name Ä.
I tried nearly everything from htmlspecialchars() to iconv(), but I can't find a method which returns me files and folders on my server with proper names.
Yes, it does not. The doc reads:
utf8_encode — Encodes an ISO-8859-1 string to UTF-8
Not sure what encoding it is, but it's definitely not ISO-8859-1.
You need to use mb-convert-encoding to convert between arbitrary encodings. E.g.
$utfStr = mb-convert-encoding('č/š/ě/ř/ž/á/ý/í/é', 'UTF-8', 'ISO-8859-15')
If you don't know client's encoding, you may need to use mb_detect_encoding, which may not always work, or be exactly accurate.
To avoid this mess, I would recommend to do it other way round and send utf-encoded file name from your android app, rather than convert it serverside.

Reading Encoded File in PHP

I have a text file that displays as below when I open it in Sublime Text:
But when I fread the file and echo each line in php, I get garbled characters like this:
I read Joel's post on encoding and understood the basics of encoding. Also, When I do mb_detect_encoding, it detects the string as UTF-8. Now, I do not understand what I have to do with this information of what encoding the string is in in order to display it, or convert it, to readable characters like in the first picture.
And why is it displaying garbled characters when it is already in UTF-8? Is php using different encoding type to read the file? Does php string have to be in UTF-8 or ASCII, or does it not matter as long as I specify what it is?
I would really appreciate if someone can help me understanding the idea! Thanks.
EDIT:
Pedro Lobito and Peter's suggestions worked.
$file = file_get_contents($bl_file);
$content = unpack("H*", $file);
But if someone could explain why I have to do this way, that'd still help me understanding it!
But if someone could explain why I have to do this way, that'd still help me understanding it!
Because it's a binary file. Sublime shows you hex human-readable representation of raw binary values.
When you do file_get_contents, you read a file into a string of '0101010', raw bits and bytes.
When you do unpack (H* /* H is for HEX */, $file ), you're telling PHP, you want to see your binary data as a human-readable hex encoded representation of your byte-stream. (You can tell it's a hex when you see letters A-F)
HEX encoding is far more readable than binary, that's why Sublime uses it. Also, I once saw a man who can code in raw binary. I was scared.
When you echo them, you're just streaming raw binary values to console, they have no special meaning, so console(or browser) shows them as a control characters and other garbage, which has now meaning for a human eye.
So, if you open this file with another text editor, it will:
a) show garbage (mcedit)
b) show garbage and tell you that it's a binary file (vim, gedit)
Sublime tricked you into thinking it's a text file by being too friendly.
If you'll echo binary files to your command prompt / shell it can kill your data. Never do this. Because shell can interpret raw binary data as a command and run it.
If you'll echo some binary file having something like this inside:
rm -rf ~/[bytecode_For_NewLine_Here],
you can delete your home folder contents in linux

PHP file_put_contents() filename contains accents

I want to call function file_put_contents() and everything runs correctly, except for the filename which contains accents (diacritics). It does save the file, but the filename is not encoded the right way.
What should I do? Is any special encoding required in file_put_contents()?

read file in php until a specified string is found

I have a file that I wish to read in php and split into smaller files.
The file is base64encoded but each section is delimited in the file with a (unecoded) tilde followed by the original filename of the base64 encoded data followed by another tilde.
As a silly example, the file could look like :
NbAYnnBBA~file1.txt~NbAYnnBBANbAYnnBBANbAYnnBBANbAYnnBBA~file2.txt~
I don't want to use file_get_contents as the files could be huge and I don't want to hit memory limited.
Can anyone think of a way of doing it without having to use fgetc to do it a char at a time ?
There are no line breaks in the file by the way - it is one continous block.

special characters in url filename cause problems

I have the following www.mywebsite.com/upload/server/php/files/foto/test/Aston_Martin_DBS_V12_coupé_(rear)_b-w.jpg
This file is uploaded trough a script. The file exists on the server.
However, because the special character in the url (é), I am experiencing some problems.
The filename on the server is Aston_Martin_DBS_V12_coup%C3%A9_(rear)_b-w.jpg, which is correct. However somehow my browser (Chrome) requests this page as ISO-8859-1 instead of UTF-8.
Therefore, I get a 404.
I am using jQuery file upload plugin.
I deleted my answer from here and i wrote new:
Usually websites does not contain files with non-standard characters. Files usually have removed non standard characters, sometimes that characters are replaced by similar standard chars (Polish ą to a, ś to s). For example - im renaming files manually, or when i have a lot of files - i just use bash or php script that removes/replaces that characters in filenames on server.
Anyway, if you HAVE TO use original filenames - you have to decode them from ISO and encode them to UTF8.
Take look at that php code fragment here:
how to serve HTTP files with special characters
Some special Charater make problem in url for filename
like
+ ,#,%,&
For those file which are accessing through url make file which not contain above letters
forex
str_replace(array(" ","&","'","+","#","%"),"-","filename")
it will works fine
If the filename contains the % character codes, you will need to encode those in your URL. Try accessing Aston_Martin_DBS_V12_coup%25C3%25E9_(rear)_b-w.jpg

Categories