Error opening file in PHP page

Asked

Viewed 257 times

0

I have a page in PHP that lists the files of a folder from a search. In the result has a link to the file in order to open it. However, it is not possible to open the listed files.

Code index.php

<form name="frmBusca" method="POST" action="buscar.php">

    <input type="text" name="buscar">
    <input type="submit" name="pesquisa" value="Buscar">

</form>

Code search.php

    <?php
    //Arquivo para buscar video dentro do explorer
    $video = $_POST['buscar'];

    $endereco = "C:/xampp/htdocs/www/";


    $iterator = new DirectoryIterator($endereco);

    foreach ( $iterator as $entry ) {

        if( ($entry->getFilename()) == $video){
            echo $entry->getFilename()," "; 
            $link = $endereco.$entry->getFilename(); 
            echo "<a href='$link'>".$entry->getFilename()."</a>";
            echo "<br>";
        }

    }

?>


<video width="320" height="240" controls>
  <source src='<?php echo $link; ?>' type="video/mp4">
</video>

apos listar conteudo

  • What happens if you click on the link? And what should this tag do video at the end of the code?

  • After clicking on the link nothing happens...!

  • And if you leave the variable $endereco empty?

  • Fatal error: Uncaught Runtimeexception: Directory name must not be Empty. in C: xampp htdocs www buscar.php:8 Stack trace: #0 C: xampp htdocs www buscar.php(8): Directoryiterator->__Construct(') #1 {main} thrown in C: xampp htdocs www buscar.php on line 8

  • Obvious kkk error mine. Return the variable value and just remove it from $link. Let $link only with the file name.

  • I can’t open the file and I can’t see it in the video tag!

  • $link with filename: OK

  • Perfect everything working out!

Show 3 more comments

1 answer

0


Fix the search.php file

 <?php
    //Arquivo para buscar video dentro do explorer
    $video = $_POST['buscar'];

    $endereco = "C:/xampp/htdocs/www/";


    $iterator = new DirectoryIterator($endereco);

    foreach ( $iterator as $entry ) {

        if( ($entry->getFilename()) == $video){
            echo $entry->getFilename()," "; 
            $link = $entry->getFilename(); 
            echo "<a href='$link' target='_blank'>".$entry->getFilename()."</a>";
            echo "<br>";
        }

    }


?>


<video width="320" height="240" controls>
  <source src='<?php echo $link; ?>' type="video/mp4">
</video>

exibicao de video

Browser other questions tagged

You are not signed in. Login or sign up in order to post.