PHP+File uploading error

Asked

Viewed 15 times

-2

Good morning, everyone. I’m trying to do a simple PHP video upload. My HTML code is here:

<html>
    <head>
        <title>Aproveite o site</title>
    </head>

    <body>
        <form enctype="multipart/form-data" action="videosUpload.php" method="POST">
            <input type="text" name="texto">
            <input type="file" name="file">
            <input type="submit" value="Submit">
        </form>
        
        <a href=sair.php>sair</a>
    </body>
</html>

My PHP code is here:

<html>
    <body>
        <?php
            echo $_POST['texto'];

            //resto do código criado
        ?>
        <a href="videos.php">Voltar</a>
    </body>
</html>

The problem is: When I do not select a file to upload, echo shows the value of the field passed by the POST method, but if I select a video file, it shows the following error: Undefined index: text.

Some solution?

I’m using Uwamp on a Windows10 machine.

Thank you.

1 answer

0

This is happening because you are trying to print the value of $_POST['texto'] whether or not there is any value.

Try this, check first if the variable has started:

<?php

if (isset($_POST['texto'])) {
 // Resto do código aqui...
}

Reference: https://www.php.net/manual/en/function.isset.php

Browser other questions tagged

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