peipst9lker Posted July 27, 2012 Share Posted July 27, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/266325-advanced-question-with-serialized-data/ Share on other sites More sharing options...
requinix Posted July 27, 2012 Share Posted July 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266325-advanced-question-with-serialized-data/#findComment-1364944 Share on other sites More sharing options...
ManiacDan Posted July 27, 2012 Share Posted July 27, 2012 Is there a reason you're doing this? Quote Link to comment https://forums.phpfreaks.com/topic/266325-advanced-question-with-serialized-data/#findComment-1364946 Share on other sites More sharing options...
xyph Posted July 27, 2012 Share Posted July 27, 2012 Some applications determine MIME simply by file extension. Unless you're serving data to a client, MIMEs aren't very important. Quote Link to comment https://forums.phpfreaks.com/topic/266325-advanced-question-with-serialized-data/#findComment-1364985 Share on other sites More sharing options...
peipst9lker Posted July 30, 2012 Author Share Posted July 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266325-advanced-question-with-serialized-data/#findComment-1365363 Share on other sites More sharing options...
ManiacDan Posted July 30, 2012 Share Posted July 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266325-advanced-question-with-serialized-data/#findComment-1365401 Share on other sites More sharing options...
peipst9lker Posted July 30, 2012 Author Share Posted July 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266325-advanced-question-with-serialized-data/#findComment-1365408 Share on other sites More sharing options...
ManiacDan Posted July 30, 2012 Share Posted July 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266325-advanced-question-with-serialized-data/#findComment-1365414 Share on other sites More sharing options...
peipst9lker Posted July 31, 2012 Author Share Posted July 31, 2012 Thousands a day? That's nothing. Seriously. I've worked on sites with hundreds of thousands of hits per second. are u woking at google? OT: I'll put it in a database now! Quote Link to comment https://forums.phpfreaks.com/topic/266325-advanced-question-with-serialized-data/#findComment-1365606 Share on other sites More sharing options...
ManiacDan Posted July 31, 2012 Share Posted July 31, 2012 Google gets millions, billions of simultaneous hits. I worked for a relatively small social network (5-10% the size of facebook) and peak usage would top 200,000 clicks per second. Quote Link to comment https://forums.phpfreaks.com/topic/266325-advanced-question-with-serialized-data/#findComment-1365663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.