IF statement for photos vs quicktimes

Discussion in 'PHP' started by goneinsane, Jul 22, 2010.

  1. #1
    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.
     
    goneinsane, Jul 22, 2010 IP
  2. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #2
    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:
     
    ssmm987, Jul 22, 2010 IP
  3. goneinsane

    goneinsane Well-Known Member

    Messages:
    303
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    #3

    The extension is in the hash. Doesn't work. I think there's an extra ' in there somewhere or one missing.
     
    goneinsane, Jul 22, 2010 IP
  4. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #4
    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 :)
     
    ssmm987, Jul 22, 2010 IP
  5. goneinsane

    goneinsane Well-Known Member

    Messages:
    303
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    #5
    It's not working. It went with the first option when it was a quicktime and that should have been the second option.
     
    goneinsane, Jul 22, 2010 IP
  6. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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).
     
    Deacalion, Jul 22, 2010 IP
  7. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #7
    Change the single = in the if statement with ==, that should work.
     
    ssmm987, Jul 22, 2010 IP
  8. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    Deacalion, Jul 22, 2010 IP
  9. goneinsane

    goneinsane Well-Known Member

    Messages:
    303
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    #9
    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:
     
    goneinsane, Jul 22, 2010 IP
  10. goneinsane

    goneinsane Well-Known Member

    Messages:
    303
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    #10
    Should this f be after the bracket like that?
    if(f$ext
     
    goneinsane, Jul 22, 2010 IP
  11. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #11
    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.
     
    ssmm987, Jul 22, 2010 IP
  12. goneinsane

    goneinsane Well-Known Member

    Messages:
    303
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    #12
    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.
     
    goneinsane, Jul 22, 2010 IP
  13. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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:
     
    Deacalion, Jul 22, 2010 IP
  14. goneinsane

    goneinsane Well-Known Member

    Messages:
    303
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    #14
    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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="uploads/'.$upload['hash'].'">View: '.$upload['name'].'</a><br>'. ($i++ +2 == $message['uploads'] ? '' : ($i != $message['uploads'] ? ' ' : ' '));
    			echo '</strong>';
    PHP:
     
    goneinsane, Jul 22, 2010 IP
  15. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Hmm... that code worked ok for me. $uploads must be different to what I thought, could you do a print_r()?
     
    Deacalion, Jul 22, 2010 IP