Figuring out content of the file from the JSON

I have merged the POST and FILE contents of a multipart form data and I am able to generate the following valid JSON:

id and username are POST variables and others are file variables.

	"id": "123",
	"username": "abc",
	"profileImg": {
		"name": "Screen Shot 2015-10-08 at 12.13.08 PM.png",
		"type": "image\/png",
		"tmp_name": "\/Applications\/MAMP\/tmp\/php\/php1iPCTI",
		"error": 0,
		"size": 93494
	"displayImg": {
		"name": "Screen Shot 2015-12-10 at 12.47.24 PM.png",
		"type": "image\/png",
		"tmp_name": "\/Applications\/MAMP\/tmp\/php\/phpGUBjOe",
		"error": 0,
		"size": 28849

If I have to insert this info into a database, is it possible to retrieve the contents of the file using above information? I am wondering how would someone retrieve the file from the database ?

Wouldn’t there need to be some kind of “path” (to the files location) value?

Not really, I was actually able to get the content of the file like the following :

$str2 = file_get_contents($_FILES["displayImg"]["tmp_name"]);
echo $str2;

This is good at PHP level. I can see the content of txt file using this and some messed up characters in case of PDF and Word doc. I am wondering if I have to store this information into a database. Would it be a good idea to send this string into the database? I wonder how the database person would be able to retrieve it back.

tmp-files are deleted after the request. So the information is pretty useless, because the file does not exist anymore. The “database person” would receive this information by connecting and querying, just like any other database operation.

So you mean to say, it’s okay to send the binary string of Word and PDF files into the database?

I was concerned how the database person would be able to convert the binary string files back to normal readable content?

You can save files in the database, this way has multiple pros and cons you can read in seveal online ressources. Many databases have a special “binary” column type for that, and you can read/write them with the default filesystem functions. But in most situations it is more efficient to store files within the filesystem and just save the path in the database.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.