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?
The function
md5()
expects something to encrypt. Enter the criteriadatetime
that is using within the function.– William Novak
Thank you @Williamnovak was that, I did the following
$file_name = uniqid(md5(time())).$file_parts;
– Wagner Viana
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.
– Bacco
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.
– Wagner Viana
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.
– Bacco
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" :)
– Bacco