This is the code that I currently have. if ($message['uploads'] > 0) { echo '<br /><br /><strong>Attachments: '; $uploads = $db->fetch_all_array("SELECT name, hash FROM ".DB_TABLE."uploads WHERE TICKET_ID='{$message['ID']}'"); $i=0; foreach ($uploads as $upload) echo '<a href="inc/download.php?hash='.$upload['hash'].'">'.$upload['name'].'</a>'. ($i++ +2 == $message['uploads'] ? ' and ' : ($i != $message['uploads'] ? ', ' : ' ')); echo '</strong>'; } echo '</div></div>'; PHP: This code makes a link where you can download attachments. I would like to make it so that when a person has attached either jpeg, gif or png, it will open up in the browser. For quicktime videos or anything else should still be downloaded. So if it's a photo, it should be: if ($message['uploads'] > 0) { echo '<br /><br /><strong>Attachments: '; $uploads = $db->fetch_all_array("SELECT name, hash FROM ".DB_TABLE."uploads WHERE TICKET_ID='{$message['ID']}'"); $i=0; foreach ($uploads as $upload) echo '<a href="uploads/'.$upload['hash'].'">'.$upload['name'].'</a>'. ($i++ +2 == $message['uploads'] ? ' and ' : ($i != $message['uploads'] ? ', ' : ' ')); echo '</strong>'; } echo '</div></div>'; PHP: Does anyone here know how I can make this IF statement? I'm just learning.
if the file extension is stored in either the name, hash, or somewhere else in the database, you could do this: foreach ($uploads as $upload){ if(f$ext=='jpg' or $ext='jpeg' or $ext='gif'' (etc.)) echo '<a href="uploads/'.$upload['hash'].'">'.$upload['name'].'</a>'. ($i++ +2 == $message['uploads'] ? ' and ' : ($i != $message['uploads'] ? ', ' : ' ')); else echo '<a href="inc/download.php?hash='.$upload['hash'].'">'.$upload['name'].'</a>'. ($i++ +2 == $message['uploads'] ? ' and ' : ($i != $message['uploads'] ? ', ' : ' ')); } PHP:
The extension is in the hash. Doesn't work. I think there's an extra ' in there somewhere or one missing.
There is indeed an ' to much foreach ($uploads as $upload){ if(f$ext=='jpg' or $ext='jpeg' or $ext='gif') echo '<a href="uploads/'.$upload['hash'].'">'.$upload['name'].'</a>'. ($i++ +2 == $message['uploads'] ? ' and ' : ($i != $message['uploads'] ? ', ' : ' ')); else echo '<a href="inc/download.php?hash='.$upload['hash'].'">'.$upload['name'].'</a>'. ($i++ +2 == $message['uploads'] ? ' and ' : ($i != $message['uploads'] ? ', ' : ' ')); } PHP: Fixed
It's not working. It went with the first option when it was a quicktime and that should have been the second option.
Does your inc/download.php script send the 'Content-Disposition' header along with the data? this is how you can tell the browser to treat it as an attachment instead of trying to play it within the window (in the case of a quicktime movie).
One more thing... the code posted before my last post is problematic. You have the assignment operator in place of the 'equal to' operator and the increment operator should never be used inside a conditional.
This is what the inc/download.php file says: <?php // Define the path to file $file = '../uploads/'.$_GET['hash']; if(!file || !file_exists($file)) { // File doesn't exist, output error die('file not found'); } else { // Set headers header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$file"); header("Content-Type: application/zip"); header("Content-Transfer-Encoding: binary"); // Read the file from disk readfile($file); } ?> PHP:
Nope, also that's a mistake I didn't check the script, it was actually meant to give an idea about how it should, it wasn't finished either. @Deacalion, I had already recognised the single ='s, my fault. The other part was just copied from the orginal, didn't check that.
This was my current code: if ($message['uploads'] > 0) { echo '<br /><br /><strong>Attachments: '; $uploads = $db->fetch_all_array("SELECT name, hash FROM ".DB_TABLE."uploads WHERE TICKET_ID='{$message['ID']}'"); $i=0; foreach ($uploads as $upload) echo '<a href="inc/download.php?hash='.$upload['hash'].'">'.$upload['name'].'</a>'. ($i++ +2 == $message['uploads'] ? ' and ' : ($i != $message['uploads'] ? ', ' : ' ')); echo '</strong>'; } echo '</div></div>'; PHP: This is your code: foreach ($uploads as $upload){ if($ext=='jpg' or $ext=='jpeg' or $ext=='gif') echo '<a href="uploads/'.$upload['hash'].'">'.$upload['name'].'</a>'. ($i++ +2 == $message['uploads'] ? ' and ' : ($i != $message['uploads'] ? ', ' : ' ')); else echo '<a href="inc/download.php?hash='.$upload['hash'].'">'.$upload['name'].'</a>'. ($i++ +2 == $message['uploads'] ? ' and ' : ($i != $message['uploads'] ? ', ' : ' ')); } PHP: And when I mesh them together I get this: if ($message['uploads'] > 0) { echo '<br /><br /><strong>Attachments: '; $uploads = $db->fetch_all_array("SELECT name, hash FROM ".DB_TABLE."uploads WHERE TICKET_ID='{$message['ID']}'"); $i=0; foreach ($uploads as $upload){ if($ext=='jpg' or $ext=='jpeg' or $ext=='gif') echo '<a href="uploads/'.$upload['hash'].'">'.$upload['name'].'</a>'. ($i++ +2 == $message['uploads'] ? ' and ' : ($i != $message['uploads'] ? ', ' : ' ')); else echo '<a href="inc/download.php?hash='.$upload['hash'].'">'.$upload['name'].'</a>'. ($i++ +2 == $message['uploads'] ? ' and ' : ($i != $message['uploads'] ? ', ' : ' ')); } echo '</strong>'; } echo '</div></div>'; PHP: It still doesn't work.
From your code I'm guessing $uploads look like this: Array ( [0] => Array ( [hash] => 'file.png' [name] => 'Pic One' ) [1] => Array ( [hash] => 'file.jpg' [name] => 'Pic Two' ) [2] => Array ( [hash] => 'file.exe' [name] => 'Program One' ) [3] => Array ( [hash] => 'movie.mov' [name] => 'My Movie' ) ) PHP: If so, this code should do what you're looking for: <?php if ($message['uploads'] > 0) { $output = '<br /><br /><strong>Attachments: '; $uploads = $db->fetch_all_assoc('SELECT name, hash FROM '.DB_TABLE.'uploads WHERE TICKET_ID="'.$message['ID'].'"'); $output .= generateLinks(array_keys($uploads)); $output .= '</strong>'; } $output .= '</div></div>'; echo $output; // the function that does it all function generateLinks($array) { global $uploads; foreach ($array as $k => $v) { $ext = strtolower(pathinfo($uploads[$v]['hash'], PATHINFO_EXTENSION)); switch ($ext) { case 'gif': // fall through case 'jpg': // fall through case 'jpeg': // fall through case 'png': $array[$k] = '<a href="uploads/'.$uploads[$v]['hash'].'">'.$uploads[$v]['name'].'</a>'; break; // every other file type default: $array[$k] = '<a href="inc/download.php?hash='.$uploads[$v]['hash'].'">'.$uploads[$v]['name'].'</a>'; break; } } if (!$array || !count($array)) return ''; $last = array_pop($array); if (!count($array)) return $last; return implode(', ', $array).' and '.$last; } ?> PHP:
It's still not working so I just took the easy way out for now. I made the option to either download it or view it. This will do for now until I figure it out. Thanks to everyone who replied. if ($message['uploads'] > 0) { echo '<br /><br /><strong>Attachments:<br>'; $uploads = $db->fetch_all_array("SELECT name, hash FROM ".DB_TABLE."uploads WHERE TICKET_ID='{$message['ID']}'"); $i=0; foreach ($uploads as $upload) echo '<a href="inc/download.php?hash='.$upload['hash'].'">Download: '.$upload['name'].'</a> OR <a href="uploads/'.$upload['hash'].'">View: '.$upload['name'].'</a><br>'. ($i++ +2 == $message['uploads'] ? '' : ($i != $message['uploads'] ? ' ' : ' ')); echo '</strong>'; PHP:
Hmm... that code worked ok for me. $uploads must be different to what I thought, could you do a print_r()?