md5 error when renaming array of upload php file

1

I have the following code that uploads multiple files:

<?php


if(isset($_FILES['files'])){
    $errors = array();

    foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size = $_FILES['files']['size'][$key];
        $file_tmp = $_FILES['files']['tmp_name'][$key];
        $file_type = $_FILES['files']['type'][$key];        
        $file_parts = pathinfo($file_name);     
        $extensions = array("jpeg","jpg","png");            

    if(in_array ($file_parts['extension'],$extensions)){

        //renomea o arquivo 
        $file_parts = ".".$file_parts['extension'];
        $file_name = time().uniqid(md5()).$file_parts;

        if($file_size > 2097152){

            $errors[] = 'Tamanho do arquivo de ser menor que 2MB';

        }//if($file_size > 2097152){        

            if(empty($errors)==true){

            move_uploaded_file($file_tmp, "user_data/".$file_name); 

            }else{
                print_r($errors); 
            }//if(empty($errors)==true){

        }else{

            $errors [] = 'Extensão não permitida';

        }//if(in_array ($file_parts['extension'],$extensions))

    if(empty($errors)){

        print_r("<br/>".$file_name."<br/>");
        echo "Sucesso";

    }//if(empty($errors))

    }//foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name)


}

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Upload</title>
</head>
<body>
    <form action="" method="POST" enctype="multipart/form-data">
        <input type="file" name="files[]" multiple>
        <input type="submit" value="Enviar">
    </form>
</body>
</html>

He is uploading the files normally and renaming, they are all in the file and renamed, but he has the following error when uploading:

Warning: md5 () expects at least 1 parameter, 0 given in C: \ xampp \ htdocs \ upload \ index.php on line 17

147293915357cb449171b91.jpg

Success

Warning: md5 () expects at least 1 parameter, 0 given in C: \ xampp \ htdocs \ upload \ index.php on line 17

147293915357cb449171f7a.jpg

Success

Warning: md5 () expects at least 1 parameter, 0 given in C: \ xampp \ htdocs \ upload \ index.php on line 17

147293915357cb449172362.jpg

Success

Does anyone have an idea of what it can be?

    
asked by anonymous 03.09.2016 / 23:58

1 answer

3

As commented on by William, there was no telling what you are generating md5() .

As in any case, in this case the md5 is not very useful, a possible solution is this:

$file_name = uniqid().base_convert(mt_rand(),10,36).$file_parts;

If you prefer to invert, these two syntaxes give the same:

$file_name = uniqid(base_convert(mt_rand(),10,36)).$file_parts;
$file_name = base_convert(mt_rand(),10,36).uniqid().$file_parts;

For almost every case, the most elegant is simply this:

$file_name = uniqid().$file_parts;

But as you process multiple files, you have these considerations:

  • uniqid() is already coding system time in a slightly more compact form, so it makes no sense to concatenate with time() ;

  • Normally uniqid() will never be repeated because it is based on clock + date + microseconds, but if it is a case where you have a loop, it will process several things at the same time, base_convert(mt_rand()) adds an extra randomizer component;

  • md5(uniqid()) is a common thing to see, and is even in the PHP manual, but it's absurd in real code. md5() actually reduces the quality of uniqid() and gives margin for collisions;

Now, if you're going to use this in situations that will index images in the DB, it's smart to grab the column ID, with something like mysqli_insert_id , and generate the name based on it:

$id = mysqli_insert_id( $con );
$file_name = base_convert( $id, 10, 36 ).$file_parts;

In all cases, we are using base_convert primarily to shorten numeric values. I used base 36 because it is the maximum PHP accepts in these cases.

I would prefer base64, but PHP only converts strings to b64, not numbers, and is less advantageous in that case (treating numbers as string would take up more space instead of saving).

Manual:

  

link

    
04.09.2016 / 00:48