Tuesday, 9 June 2020

uploading files restrictiions and size

uploading files restrictiions and size
how to put restriction to upload image only
The PHP strpos() function searches for a specific text within a string. If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.
$x = 'apple.jpg';
echo strpos($x, '.');
6

echo substr($x, 6);
jpg
$type = $files['userfile']['type'];
we can know the extension of this file
echo $extension = substr($name, strpos($name, '.')+1);
ist it caliculate the dot positon after that add the one value , after that get the data from dot+1,
brackets of brackts , ist caliculate inner side and go to outer side
to convert highre case to lower case use strtolower
echo $extension = strtolower(substr($name, strpos($name, '.')+1));
if (($extension == 'jpg' || $extension == 'jpeg') && $type == 'image/jpeg'){
echo basename("/testweb/home.php")
basename($_FILES["fileToUpload"]["name"]);
how to upload the file with size , format matched
<?php
if(isset($_FILES['userfile']['name'])){
after selecting a file user is submitted or not
$name = $_FILES['userfile']['name'];
if submitted, it get the image name with extension
$tmp_name = $_FILES['userfile']['tmp_name'];
moved to temp location
$location = 'upload/';
$new_name = $location.$name;
concatenate
 $size = $_FILES['userfile']['size'];
$type = $files['userfile']['type'];
echo images/jpeg, findout whats the type
$extension = substr($name, strpos($name, '.')+1);
get the format string
if (($extension == 'jpg' || $extension == 'jpeg') && $type == 'image/jpeg'){
compared it is jpeg or jpg and images/jpeg only, it will go to next statement otherwise false
if(!empty($name)){
if it is really uploaded
if ($size <= 51200){
checks size is this size or not
if (move_uploaded_file($tmp_name, $new_name)){
move to some location
echo 'uploaded !';
uploaded
}
} else { echo ' file size must be less than 2 mb';}
conditon not satisfied , size is not matched
} else {
echo 'please select jpg/jpeg file only!';
if it is not jpeg
}
} else {
echo 'please select  a file';
if they not select filw then submit
}} ?>
<form action="upload.php" method="POST" enctype ="multipart/form-data">
<input type ="file" name ="userfile">
<input type="submit" value="upload">
</form>
strpos
 $files['userfile']['type'];
substr
strpos
strtolower
comparision
basename
isset
 $_FILES['userfile']['name'];
$_FILES['file']['name'] − the actual name of the uploaded file.
 $_FILES['userfile']['tmp_name'];
concatenation
$_FILES['userfile']['size'];
substr
strpos
if if ifelse else else
size <= 51200
move_uploaded_file
post
enctype
type=file
type=submit
how to uload the file if the file is image with size restriction
how to check image is submit or nit
how to get the image name
how to store to temporary location
is it automatically stores that image to temporaty
how to get the file size
how to get the type of file
how to get the extension
how to get the position of required
how to cut the string upto required
how to compare with size
how to change the position
how to chcke if size is matched
how to check extension is matched

No comments:

Post a Comment