How to upload in php

Asked

Viewed 51 times

0

Well, basically I’m trying to upload an mp3 file to a folder I created from the server, but I’ve done everything, and I always end up with the same mistake. That is 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
        )

)

some solution?

1 answer

0

The value 1 in the variable error indicates the error UPLOAD_ERR_INI_SIZE, all constants are in this link: http://php.net/manual/en/features.file-upload.errors.php

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

He’s probably with 2 M, which means the upload limit is 2mb:

upload_max_filesize=2M

Change to something bigger, like 16M (bigger than this only if necessary, otherwise prefer not to use too big values, because this consumes too much of the server):

upload_max_filesize=16M

Save the file and restart your HTTP server (must be Apache, Nginx or Lightttpd).

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

Another important detail quoted by @Rpgboss is that this line is wrong:

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

The move_uploaded_file must receive a way physicist, 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/';
  • 1

    Is that line right? $uploaddir = 'http://localhost/sitededownload/musicas/';&#I thought it had to be something like this: $uploaddir = 'sitededownload/musicas/

  • @Rpgboss well noted, this is another error, note that the error value occurs before move_upload, as this function only moves from the server’s TMP folder to the desired folder, actually the upload already occurs and has failed before. + 1 pro your comment.

  • thanks for the answers guys, I will do all this and I come back here to tell the result, I thank from now!!

Browser other questions tagged

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