Error md5 when renaming upload php file array

Asked

Viewed 259 times

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>

It is uploading the files normally and renaming, they are all file in the folder and renamed, but it presents 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 any idea what it might be?

  • 1

    The function md5() expects something to encrypt. Enter the criteria datetime that is using within the function.

  • 1

    Thank you @Williamnovak was that, I did the following $file_name = uniqid(md5(time())).$file_parts;

  • 2

    I see quite a case where the person renames images to MD5. There must be some crazy blog or tutorials teaching this somewhere. Wherever it is, I suggest you don’t take it seriously, because it’s no advantage. Any gains you can imagine are achieved with simpler solutions and fewer risks. It would be much better, for example, to use a simple sequential numbering or a uniqid(), which has no risk of collision. And in cases where some data is stored in DB, the column ID itself already serves as a single reference.

  • I was reading about it and staff was talking to put the MD5, but really not a good option put it. Thanks for the tip @Bacco.

  • In fact, I went to get the link of uniqid() for you: http://php.net/manual/en/function.uniqid.php and I came across something funny. The PHP site uses a uniqid() MD5, which is terrible. But in the comments of the page itself had user explaining that it is bad, and proposing better alternative. However, for filename, uniqid() works well. And since it is clock-based, it never repeats. It gets a little bigger the name of the file, but ensures peace of mind about collisions.

  • Now, when using DB, remember that column ID is the best thing for the image name. If you want to "shorten" it, you can use hexadecimal, or even Base64 (with characters suitable for URL). Note: Base64 is another thing that is rarely used well, its only purpose is to represent bytes in a more limited set of characters. Not to use for storing information on file, much less in place of "encryption" :)

Show 1 more comment

1 answer

3


Like already commented by William, failed to say what it is that you are generating the md5().

As anyway, in your case the md5 is of little use, one possible solution is this:

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

If you prefer to invert, these two syntax give in 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 how you process multiple files has these considerations:

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

  • usually the uniqid() will never repeat itself by being based on the clock + date + microseconds, but if it is a case where you have a loop, and will process several things at virtually the same time, the base_convert(mt_rand()) adds an extra randomizer component;

  • md5(uniqid()) is a common thing to see, and it’s even in the PHP manual, but it’s absurd in real code. The md5() in fact reduces the quality of uniqid() and allows for collisions;

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

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

In all cases, we are using the base_convert basically to shorten numerical values. I used base 36 for being the most that PHP accepts in these cases.

I would prefer Base64, but PHP only converts strings to B64, not numbers, being less advantageous in this case (treating numbers as string would take up more space, rather than saving).

Handbook:

http://php.net/manual/en/function.base-convert.php

Browser other questions tagged

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