Jump to content

Advanced question with serialized data


peipst9lker

Recommended Posts

Hi, hopefully someone has a deeper insight into serialization in PHP.

The situation is like this: I serialize an object and save the data into a file (fopen, fwrite, fclose).

Depending on the input the mime type is different

 

$x = array(5,6,7);		// text/plain
$y = new stdClass();		// text/plain
$z = new someCustomClass();	// text/plain
$a = new anotherClass();	// application/octet-stream

 

Does someone know why this is happening? Another thing is can I enforce the stream mimetype when writing the file?

Link to comment
Share on other sites

MIME type is determined according to file contents. It is magical and you don't really have any control over it.

 

But using that on serialize()d data is nonsensical. And while the data looks textual, application/octet-stream is a more appropriate type, so you're actually getting the "wrong" result more often - yet another reason why you shouldn't do it.

Link to comment
Share on other sites

Is there a reason you're doing this?

Yes, in my application you can install plugins (in form of xml-files) the parsed XML data will be saved saved into an object which will be saved serialized.

Works around 240% faster then parsing the XML-file over and over. Also it only extracts important data so the serialized data is even smaller.

 

Some applications determine MIME simply by file extension. Unless you're serving data to a client, MIMEs aren't very important.

I'm using .cache extension on everything, MIME-type still differs.

 

 

It would be better if the MIME-type is always octet-stream so no one can simply open the cache with an editor and edit it. (Ofc you can with other tools)

I may use a checksum for the cache file to check if its being edited. Hopefully it doesn't crush my speed advantage I get from those serialization method.

 

 

Link to comment
Share on other sites

You really should be using a database for this.  Also, serialize() is not meant for this.  The format changes between PHP versions.  Serialize() is meant for very short term message passing, not long-term storage.  Databases would be more reliable.

 

As for the actual question: the mime type doesn't matter.  It's being determined dynamically and is not actually part of the file.  Some of your files look like strings, some look like binary data.  Regardless, the file is the file and opening it in a plaintext editor will allow it to be edited.  Just ignore this and stop checking the mime type.

Link to comment
Share on other sites

Thanks for your reply ManiacDan,

I've considered storing those information in a database, the problem is that the database is managing everything, lots of data and lots of querys are being executed in a short period of time, later one there will be around thousands of pageviews a day (hundreds concurent users) which will cause much load on the database. I don't want to overload the mysql server. Talking about reliability there will be a checksum check if it differs the system will automatically rebuild the cache file if possible, so this should not be a problem. Thanks for the info on mime types I'll ignore it.

Link to comment
Share on other sites

Thousands a day?  That's nothing.  Seriously.  I've worked on sites with hundreds of thousands of hits per second.  MySQL can handle it.  Use a database, that's what it's for.  If it gets too slow, split this data off onto another database.  Flat files are the wrong solution.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.