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>
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
Tuesday, 9 June 2020
uploading files restrictiions and size
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment