How to upload in php

0

Well, basically I'm trying to upload an mp3 file to a folder I created from the server, but I've done everything myself, and I always end up with the same error. Here's the code:

<?php
    include("connectsql.php");
    isset($_GET ['mp3file']) ? $_GET['mp3file'] : '';
    $songname = $_GET['songname'];
    $query = "SELECT id FROM songs WHERE Song = '$songname'";
    $result = mysqli_query($link, $query) or die(mysqli_error($link));
    $uploaddir = 'http://localhost/sitededownload/musicas/';
    $uploadfile = $uploaddir . basename($_FILES['mp3file']["name"]);
    if($result){
        $nomemusica = mysqli_fetch_assoc($result);
    }

    echo "<pre>";
        if (move_uploaded_file($_FILES['mp3file']["name"], $uploadfile)) {
            echo "Arquivo válido e enviado com sucesso.\n";
        }else {
            echo "Possível ataque de upload de arquivo!\n";
        }


        PRINT_R($_FILES);

    echo "</pre>";
?>

and this is the result of PRINT_R:

Possível ataque de upload de arquivo!
Array
(
    [mp3file] => Array
        (
            [name] => I Feel It Coming.mp3
            [type] => 
            [tmp_name] => 
            [error] => 1
            [size] => 0
        )

)

Any solution?

    
asked by anonymous 23.02.2018 / 03:17

1 answer

0

The 1 value in the variable error indicates the error UPLOAD_ERR_INI_SIZE , all constants are in this link: link

As a number should be compared to a constant, but let's talk about the UPLOAD_ERR_INI_SIZE error, this indicates that the file is larger than the limit allowed in the configuration file of your php.ini server if you have administrative permission to edit this file then look for the line that contains upload_max_filesize=

It should probably be 2M , which means that the upload limit is 2mb:

upload_max_filesize=2M

Change to something larger, such as 16M (higher than this only if you really need it, otherwise prefer not to use very large values as this consumes a lot of the server):

upload_max_filesize=16M

Save the file and restart your HTTP server (it should be Apache, Nginx or LightTTPD).

If it is not possible to increase by several possible factors, as some restriction of your hosting then I recommend as an alternative to split the file and to upload using the JavaScript APIs XmlHttpRequest and FileReader .

Another important detail mentioned by @RpgBoss is that this line is wrong:

 $uploaddir = 'http://localhost/sitededownload/musicas/';

The move_uploaded_file should be given a physical path, if windows should be something like:

 $uploaddir = 'c:/xampp/www/sitededownload/musicas/';

If it's unix-like maybe it's something like:

 $uploaddir = '/etc/www/sitededownload/musicas/';
    
23.02.2018 / 03:25