MyBB Community Forums

Full Version: Also update filesizes on attachments on recount/rebuild
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey everyone,

First post, sorry if I'm in the wrong section or if there actually is an existing solution for this Smile
I'm busy setting up myBB for a new version of our dutch DeLorean owners club forum and this is my first time using myBB. My previous experience is mostly with phpbb2 but not for this club site.

Anyways, I'm populating the database from existing data from the old version of the site and I ran into a small snag along the way. I have an existing data set of attachments for posts that I was able to use to fill the mybb_attachment table. I'm filling the table simply by SQL queries which might not be the appropriate way to go but as I said, I'm new to mybbSmile

Anyways, I populated the table, I have all the attachment files in the right directory, and I did a recount/rebuild that marviously created all the thumbnails and all that jazz. The only thing it didn't do is update the filesize. Since in my source data the filesize of the files wasn't stored, and since it seems that you can't view the attachments if this isn't properly filled in, I got into a bit of a jam...

Anyways, I wrote a simply php script to fix the data, maybe nice to take it along with the recount/rebuild script. I could also imagine this would be a nice trick if someone cleans up the attachment folder and rescales any image that simply is taking up to much space.

(I removed all error checking for clearity of the code)
$root='../';
$db=@mysql_connect("localhost","user","password",true);
mysql_select_db("database",$db);

$sql="select * from mybb_attachments order by filename";
$result=@mysql_query($sql, $db);
$results=array();
while ($row=@mysql_fetch_array($result,MYSQL_ASSOC)) {
  $results[]=$row;
};

foreach($results as $key=>$row) {      
  $fullname = $root.'wwwroot/forum/uploads/'.$row['attachname'];
  if (file_exists($fullname)) {
    $stats = stat($fullname);
    if ($stats['size']!=$row['filesize']) {
      $sql = "update mybb_attachments set filesize = ".$stats['size']." where aid = ".$row['aid'];
      $result=@mysql_query($sql, $db);
    };
  };
};