O
O
olka132021-07-14 23:52:39
PHP
olka13, 2021-07-14 23:52:39

How to determine the file format on the site and change the layout for it?

Hello, tell me a script that changes the code depending on the file extension.
For example, an image and video, or a sound file and an archive.
Something like

if ext .mp3
<audio src="name.mp3" controls></audio>
else
<a href="name.zip" download>Скачать файл</a>


There are files of different formats in a pile and I would like to make an individual html output code for them, but I don’t know which file can appear on which page. Is there any solution? Preferably in js if possible.
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Localhost, 2021-07-15
@olka13

$filename = 'file.mp3'; //сам файл

$format = pathinfo($filename, PATHINFO_EXTENSION); //определяем формат

//условия
if($format == 'mp3'){
echo '<audio src="'.$filename.'"></audio>'
}else if($format == 'mp4'){
echo '<video><source src="'.$filename.'" type="video/mp4"></video>' 
}else if($format == 'jpg'){
echo '<img src="'.$filename.'">'
}else{
echo 'формат не найден'
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question