Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

View instead of download?

edited May 2011 in Vanilla 2.0 - 2.8
Hi, I could use some advice.

I'm trying to get the file upload plugin to display attachments instead of downloading them when you click on their link. I'm working with mp3's & images mostly.

1) I hade to make some changes in the Vanilla core files. I changed 'attachment' to 'inline' on line 294 of core/class.filesystem.php:
public static function ServeFile($File, $Name = '', $MimeType = '', $ServeMode = 'inline')

I also fixed/added some mime-types around line 330:
"jpeg"=> "image/jpeg", // Edited line; added e to jpeg. "jpg" => "image/jpeg", // Edited line; added e to jpeg. "mp3" => "audio/mpeg" // Added line.

I now have the images working, and mp3s look like they are going to load, but I just get a Quicktime question mark icon. When I force download an mp3, it works, but Safari displays ? for file size until it finishes.

I tried adding this around line 360, but seems to have no effect:
header('Content-Length: '.filesize($File));

Is there a better way to go about this? Editing a core php file to support a plugin featue feels fragile. Any ideas for getting the mp3 to play? I know Safari is a little touchy with file headers sometimes, but it's able to play most mp3s in browser.


  • Options
    I'd also appreciate help figuring out if there's a better way to go about doing this.
  • Options
    ToddTodd Chief Product Officer Vanilla Staff
    I've uploaded a new version of the FileUpload that does this.
  • Options
    This new version doesn't work for me at all (with It's possible to upload files, but they won't show up.
  • Options
    RaizeRaize vancouver ✭✭
    Works great for me on

    Did you wait for the file to upload fully before clicking post?
  • Options
    After uploading the new version, updating from 1.4.0 to 1.4.4, none of my old attachments work. Images just vanish entirely, and other attachments (like pdfs) seem to appear, but go to a page not found link when clicked.
  • Options
    Not working for me on 2.0.18b3 either -- same issue as @shanert.
  • Options
    edited June 2011
    Looks like uploads are happening (with no visible feedback), and the html is being written into the comment body, but it's being set to display:none, presumably by the js.

    .AttachmentFileContainer is visible, .Attachment and all contained elements are hidden.

    I am astonished that functionality that is so basic and fundamental to any kind of forum is still not working.

    Edit: further messing around with Firebug shows that there are UI elements after uploading files that are present but display:none in the form and the post, including info about the uploaded image, and the 'insert' link. The .Attachment:hover .FileHover doesn't work because the .Attachment div is hidden as well. Will investigate further, but it seems like the guts of the plugin are more or less working as they should, and the issue is with display.

    Changing that to .AttachFileContainer:hover .FileHover lets the insert/delete divs appear on hover as I assume they're intended, but there's no visible anchor. Guessing here, I assume there are supposed to be some generated thumbnails. But the URL for the thumbnails paths that're being generated look like [root]/utility/thumbnail/FileUpload/b8/4d3d9034c0b2eb7143a061a72814e3.jpg which is clearly wrong.

    That URL is generated in class.mediamodel.php by the ThumbnailUrl() function, but that's as far as I can get. Also, [root]/uploads/thumbnails/FileUpload/XX folders are being created, but are empty.

    Edit 2: Without any documentation I can find, I'm also having a hell of a time understanding what the intended user interaction/behaviour is, here, or what things are supposed to look like if they work properly. Very frustrating.
  • Options
    moensmoens New
    edited June 2011
    @nilok re: 1.4.0 --> 1.4.4

    It seems that older images that were uploaded but not "attached" (which were visible at the bottom of the posts in 1.4.0) are now gone once 1.4.4 is installed. Overall the new functionality is desirable, but the un-desirable functionality of the previous version results in the following artifact: missing images in some older posts.

    To fix this, I created this script to look through the GDN_Media table to identify all the places media was uploaded. Then I looked through those places (GDN_Discussions and GDN_Comments) and matched for the links indicated in the GDN_Media.Path column. For those that did not return matches, I added img tags for jpg, png and gif, and a simple link for anything else.

    Here is the php (run once, then uncomment the "update lines" once you have run the script and think you like the results... did I mention that you should backup your db before you do this? yeah...):

    <?php ## # Use with caution, this thing can screw up your db # backup your db first # create a php file in your web dir (same level as uploads, themes, etc) and paste this code into it # run the file on command line or web, and if the results look good, uncomment the lines that update the db # run the file again... # click through stuff in your forum... if things are whack, restore your db define('APPLICATION', 'APPLICATION'); require_once('conf/config.php'); $link = mysql_connect($Configuration['Database']['Host'], $Configuration['Database']['User'], $Configuration['Database']['Password']); if (!$link) { die('Could not connect: ' . mysql_error()); } else { echo 'Connected successfully'; mysql_select_db($Configuration['Database']['Name']); $query = 'SELECT Path, ForeignID, ForeignTable ' . 'FROM GDN_Media ' . 'WHERE ForeignID != "" ' . 'AND ForeignTable != ""'; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { if($row['ForeignTable'] == 'discussion') { $discussionList[] = $row['ForeignID']; $discussionMedia[$row['ForeignID']][] = $row['Path']; } if($row['ForeignTable'] == 'comment') { $commentList[] = $row['ForeignID']; $commentMedia[$row['ForeignID']][] = $row['Path']; } } $query = 'SELECT DiscussionID, Body ' . 'FROM GDN_Discussion ' . 'WHERE DiscussionID IN ('. implode(',', array_unique($discussionList)) . ')'; $result = mysql_query($query); if (!$result) { $message = mysql_error() . " --> " . $query . "\n"; die($message); } else { while($row = mysql_fetch_assoc($result)) { $append = ''; if(isset($discussionMedia[$row['DiscussionID']])) { foreach($discussionMedia[$row['DiscussionID']] as $mediaUrl) { if(strpos($row['Body'], $mediaUrl) === false) { if(preg_match('#(gif|png|jpg)$#i', $mediaUrl)) { $append .= '<br/><img alt="image" src="' . '/uploads/' . $mediaUrl . '"/>'; } else { $append .= '<br/><a href="' . '/uploads/' . $mediaUrl . '">' . substr(strrchr($mediaUrl, '/'), 1) . '</a>'; } } } $update = 'UPDATE GDN_Discussion ' . 'SET Body = "' . addslashes($row['Body'] . $append) . '" ' . 'WHERE DiscussionID = ' . $row['DiscussionID']; // $updateResult = mysql_query($update); echo $update . "\n"; } } } $query = 'SELECT CommentID, Body ' . 'FROM GDN_Comment ' . 'WHERE CommentID IN ('. implode(',', array_unique($commentList)) . ')'; $result = mysql_query($query); if (!$result) { $message = mysql_error() . " --> " . $query . "\n"; die($message); } else { while($row = mysql_fetch_assoc($result)) { $append = ''; if(isset($commentMedia[$row['CommentID']])) { foreach($commentMedia[$row['CommentID']] as $mediaUrl) { if(strpos($row['Body'], $mediaUrl) === false) { if(preg_match('#(gif|png|jpg)$#i', $mediaUrl)) { $append .= '<br/><img alt="image" src="' . '/uploads/' . $mediaUrl . '"/>'; } else { $append .= '<br/><a href="' . '/uploads/' . $mediaUrl . '">' . substr(strrchr($mediaUrl, '/'), 1) . '</a>'; } } } $update = 'UPDATE GDN_Comment ' . 'SET Body = "' . addslashes($row['Body'] . $append) . '" ' . 'WHERE CommentID = ' . $row['CommentID']; // $updateResult = mysql_query($update); echo $update . "\n"; } } } } mysql_close($link); ?>
  • Options
    hyphyhyphy New
    edited June 2011
    Can't you just do all of this with .htaccess ForceType?
Sign In or Register to comment.