1

I make a form through i can upload images and videos, the file's are stor in some folder and the path is stored in mysql table, now when i select the image file then i would show properly, my image selection code is...

$smt=$conn->prepare('SELECT * FROM post');
$smt->execute();
 <?php while($gdata=$smt->fetch(PDO::FETCH_OBJ)):?>
        <a href="#" class="media-left col-md-4 clearfix"><img src="posts/<?php echo  $gdata->Post_Path; ?>" alt="image" class="post-image"/></a>
        <div class="media-body col-md-8 post-image-space pull-left">
            <div class="post-overview">
                <ul>
                    <li class="post-category"><?php echo $gdata->Category;?></li>
                    <li class="post-timestemp">Post on <?php echo $gdata->Post_Date;?></li>
                </ul>


            <a href="post-description.php?id=<?php echo $gdata->Id?>"><h4 class="media-heading h4"><?php echo $gdata->Title;?></h4></a>
            <p class="post-text"><?php echo $gdata->Post;?></p>
          </div>


</div>
<?php endwhile;?>

now the problem is that through the above way i can access the video file but it can't be shown? How can i check that the file is image or video?

In my database the path will store like, example.jpg/mypic.png, test.mp4, how can i check the last extension of file?

my updated code is...

<?php
include 'conn.php';
$smt=$conn->prepare('SELECT * FROM post');
$smt->execute();
$row=$smt->fetch(PDO::FETCH_OBJ);
$row->Post_Path;
$ext=pathinfo($row,PATHINFO_EXTENSION);

?>

<?php if($ext=='mp4')

{?>

    <?php while($gdata=$smt->fetch(PDO::FETCH_OBJ)):?>
    <a href="#" class="media-left col-md-4 clearfix"><video class="post-image" controls>
            <source src="posts/<?php echo $gdata->$ext;?>" type="video/mp4">
        </video></a>
    <div class="media-body col-md-8 post-image-space pull-left">
        <div class="post-overview">
            <ul>
                <li class="post-category"><?php echo $gdata->Category;?></li>
                <li class="post-timestemp">Post on <?php echo $gdata->Post_Date;?></li>
            </ul>


            <a href="post-description.php?id=<?php echo $gdata->Id?>"><h4 class="media-heading h4"><?php echo $gdata->Title;?></h4></a>
            <p class="post-text"><?php echo $gdata->Post;?></p>
        </div>


    </div>
    <?php endwhile;
}

elseif($ext=='jpg||jpeg||png')
{

    ?>

    <?php while ($gdata = $smt->fetch(PDO::FETCH_OBJ)): ?>
        <a href="#" class="media-left col-md-4 clearfix"><img src="posts/<?php echo $gdata->Post_Path; ?>" alt="image"
                                                              class="post-image"/></a>
        <div class="media-body col-md-8 post-image-space pull-left">
            <div class="post-overview">
                <ul>
                    <li class="post-category"><?php echo $gdata->Category; ?></li>
                    <li class="post-timestemp">Post on <?php echo $gdata->Post_Date; ?></li>
                </ul>


                <a href="post-description.php?id=<?php echo $gdata->Id ?>"><h4
                        class="media-heading h4"><?php echo $gdata->Title; ?></h4></a>

                <p class="post-text"><?php echo $gdata->Post; ?></p>
            </div>


        </div>

    <?php endwhile;
}?>

I am not able to show video in my index page, how to show it, however i am able to show the image..

Any help would be appreciated.

  • 2
    You can check your extension using pathinfo() function. check this link :http://php.net/manual/en/function.pathinfo.php – Hardik Solanki Nov 27 '14 at 07:00
  • Duplicate? http://stackoverflow.com/questions/7563658/php-check-file-extension – dubbe Nov 27 '14 at 07:01
  • @stackovr To show video you either have to use html5 (http://www.w3schools.com/html/html5_video.asp) or possible flash or javascript (https://flowplayer.org/). – dubbe Nov 27 '14 at 07:22

4 Answers4

3
$info = new SplFileInfo('testing.jpg');
var_dump($info->getExtension());

Reference

Before someone comes in and says this requires extra installations

This extension is available and compiled by default in PHP 5.0.0.

Reference

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1

Check this one. which may helps you..

For PHP < 5.3 use mime_content_type()

For PHP >= 5.3 use finfo_fopen()

$smt=$conn->prepare('SELECT * FROM post');
$smt->execute();
 <?php while($gdata=$smt->fetch(PDO::FETCH_OBJ)):

$mime = mime_content_type($gdata->Post_Path);
if(strstr($mime, "video/")){
    // this code for video
$file='';
}else if(strstr($mime, "image/")){
    // this code for image

$file='<img src="posts/'.$gdata->Post_Path.'" alt="image" class="post-image"/>';
}

?>
        <a href="#" class="media-left col-md-4 clearfix"><?php echo $file?></a>
        <div class="media-body col-md-8 post-image-space pull-left">
            <div class="post-overview">
                <ul>
                    <li class="post-category"><?php echo $gdata->Category;?></li>
                    <li class="post-timestemp">Post on <?php echo $gdata->Post_Date;?></li>
                </ul>


            <a href="post-description.php?id=<?php echo $gdata->Id?>"><h4 class="media-heading h4"><?php echo $gdata->Title;?></h4></a>
            <p class="post-text"><?php echo $gdata->Post;?></p>
          </div>


</div>
<?php endwhile;?>

mime_content_type Should work for most file extentions.

Mahendra Jella
  • 5,450
  • 1
  • 33
  • 38
  • 1
    But mime_content_type is depricated since Fileinfo does the same thing. http://php.net/manual/en/function.mime-content-type.php – dubbe Nov 27 '14 at 07:04
  • Using fileinfo should look something like: $finfo = new finfo(FILEINFO_MIME); $mime = $finfo->file($gdata->Post_Path); – dubbe Nov 27 '14 at 07:10
0

Try this:

$fileInfo = pathinfo($gdata->Post_Path);

if($fileInfo['extension'] == 'png')
{
    echo "It's an PNG";
}
elseif($fileInfo['extension'] == 'mp4')
{
    echo "It's an MP4";
}
S.Pols
  • 3,414
  • 2
  • 21
  • 42
  • You can remove all that if elseif and just do `echo "It's an ".strtoupper($fileInfo['extension']);`: Both are equivalent. – Hanky Panky Nov 27 '14 at 07:04
  • Yes, but i think his intension is to do different things whenever it's an image or a video file. I just ment that he can implement the logic instead of the `echo` on that position. – S.Pols Nov 27 '14 at 07:07
0
$file_parts = pathinfo($filename);

switch($file_parts['extension'])
{
    case "jpg":
    break;

    case "exe":
    break;

    case "": // Handle file extension for files ending in '.'
    case NULL: // Handle no file extension
    break;
}

may be it's help you.try it

Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38