Answer the question
In order to leave comments, you need to log in
Please take a look at my code and tell me where I might be wrong?
Please take a look at my code and tell me where I might be wrong.
In the code, I'm trying to load the user's ava, maybe I made a mistake somewhere or the possibility of a workaround.
index.php
<form method="POST" enctype="multipart/form-data">
<input type="file" name="avatar">
<button type="submit" name="send">Send</button>
</form>
<?php
require_once 'func.php';
if( isset($date['send']))
{
if(isset($file))
{
debug( $file['avatar'] ) ;
img_save( $file['avatar']['name'],$file['avatar']['tmp_name'],$file['avatar']['error'],$file['avatar']['size'] );
}
}
?>
<?php
$date = $_POST;
$file = $_FILES;
function debug($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}
function img_save($name,$path,$error,$size)
{
$error_report = [];
$format = [];
if($size > 250000 )
{
$error_report[] = "Размер файла слишком большой!";
}
if($error >0)
{
$error_report[] = "Файл поврежден!";
}
if( exif_imagetype($path) == IMAGETYPE_JPEG )
{
$format[] = ".jpg";
}
elseif( exif_imagetype($path) == IMAGETYPE_PNG )
{
$format[] = ".png";
}
else
{
$error_report[] = "Формат файла не подходит используйте .png или .jpeg !";
}
if(count($error_report) > 0 )
{
echo "Ошибка: " . array_shift($error_report);
die();
}
else
{
$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
move_uploaded_file($path,"files/" . substr(str_shuffle($permitted_chars), 0, 16).array_shift($format));
}
}
Answer the question
In order to leave comments, you need to log in
<input type="file" name="avatar" accept="image/gif, image/jpeg">
makes it easier for a person to choose the right format, if(isset($file))
does not guarantee that the file has loaded, so you also need to check the error before passing non-existent properties to the function$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
finally tin, have not heard about the hash? Moreover, they also name files in different registers ... Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question