Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading file using $_FILES and PHP

Tags:

file

php

upload

Today I tried for the first time to upload files to my website, but it seems that it doesn't work. I hope someone can help me, and I hope my code is readable :)

The problem is when I choose file and SUBMIT form it is not accepted because $_FILES is NULL

if (move_uploaded_file($_FILES['obrazok']['tmp_name'], $uploadfile))

UPDATED Error

Warning: move_uploaded_file(www_nekonomagic/res/img/anime/uploads/793392545Untitled.jpg): failed to open stream: No such file or directory in /home/gj020900/www_nekonomagic/admin/admin-nove-anime.php on line 107

Warning: move_uploaded_file(): Unable to move '/home/gj020900/tmp/php9izgwb' to 'www_nekonomagic/res/img/anime/uploads/793392545Untitled.jpg' in /home/gj020900/www_nekonomagic/admin/admin-nove-anime.php on line 107

Shorted form:

<form enctype="multipart/form-data" action="../admin/admin-nove-anime.php" method="post" role="form">
    <div class="form-group col-md-6">
        <!-- MAX_FILE_SIZE must precede the file input field -->
        <input type="hidden" name="MAX_FILE_SIZE" value="3145728" />
        <label for="obrazok" class=""> Obrázok anime (Max. 3MB):</label>
        <input type="file" name="obrazok" id="obrazok" class="form-control" required>
    </div>
    <div class="form-group col-md-12">
        <input type="submit" class="btn btn-success" name="pridaj_anime_submit" value="Pridať anime" style="width: 100%;">
    </div>
</form>

PHP part:

if (isset($_POST['pridaj_anime_submit'])) {
    // skontroluje sa ci sa subor ulozil ak ano pokracuje ak nie napise chybu
    $uploaddir = '/www_nekonomagic/res/img/anime/uploads/';
    $uploadfile = $uploaddir . basename($_FILES['obrazok']['name']);
    if (move_uploaded_file($_FILES['obrazok']['tmp_name'], $uploadfile)) {
        // prva cast formy
        $nazov = mysqli_real_escape_string($connect_to_db, $_POST['nazov']);
        $rok = mysqli_real_escape_string($connect_to_db, $_POST['rok']);
        $pocet = mysqli_real_escape_string($connect_to_db, $_POST['pocet']);
        $preklad = mysqli_real_escape_string($connect_to_db, $_POST['preklad']);
        $stav = mysqli_real_escape_string($connect_to_db, $_POST['stav']);

        $sql_vloz_anime = "INSERT INTO anime (a_name, a_year, a_translated_min, a_translated_max, a_rate_min, a_rate_max, a_edit, a_condition)
                                  VALUES ('$nazov', '$rok', '0', '$pocet', '8', '10', '$preklad', '$stav')";
        mysqli_query($connect_to_db, $sql_vloz_anime);
        $sql_ziskaj_a_id_pridaneho_anime = "SELECT * FROM anime WHERE a_name = '$nazov'";
        $run_sql_ziskaj_a_id_pridaneho_anime = mysqli_query($connect_to_db, $sql_ziskaj_a_id_pridaneho_anime);
        $a_id_ziskane = "";

        while ($db_data = mysqli_fetch_assoc($run_sql_ziskaj_a_id_pridaneho_anime)) {
            $a_id_ziskane = $db_data['a_id'];
        }

        // druha cast formy
        $obrazok = mysqli_real_escape_string($connect_to_db, $uploaddir.$_FILES['obrazok']['name']);
        $zaciatok = mysqli_real_escape_string($connect_to_db, $_POST['zaciatok']);
        $koniec = mysqli_real_escape_string($connect_to_db, $_POST['koniec']);
        $cas = mysqli_real_escape_string($connect_to_db, $_POST['cas']);
        $akihabara = mysqli_real_escape_string($connect_to_db, $_POST['akihabara']);

        $sql_vloz_anime_info = "INSERT INTO anime_info (a_id, a_img, a_start, a_stop, a_time_ep, a_akihabara)
                                            VALUES ('$a_id_ziskane', '$obrazok', '$zaciatok', '$koniec', '$cas', '$akihabara')";
        mysqli_query($connect_to_db, $sql_vloz_anime_info);
    } 
    else {
        echo "
            <div class=\"alert alert-success alert-info\" style=\"margin: 10px 0\">
                <a href=\"#\" class=\"close\" data-dismiss=\"alert\" aria-label=\"close\">&times;</a>
                <strong>POZOR!</strong> Súbor sa nepodarilo nahrať.
            </div>
        ";
    }
}
like image 547
Patrik Horváth Avatar asked Oct 17 '22 09:10

Patrik Horváth


1 Answers

MAX_FILE_SIZE is in bytes to limit your upload file size to 3M, convert your size to bytes.

1024 * (1024 * 3) // 3145728

Then change your hidden input to the following

<!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="3145728" />
like image 120
Junius L. Avatar answered Oct 21 '22 00:10

Junius L.