So, when I upload files, i want to check which file extension it is:
$files = $_FILES['files'];
$files_name = $files['name'];
Shows: `"img.jpg"'
$files_name_explode = end(explode('.', $files_name));
Getting null
.
What am I doing wrong?
Thanks
Use below code :
$path = $_FILES['files']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
Hope it will help you :)
Instead of explode
you can use pathinfo
.
Use
$files_name_explode = pathinfo($filename, PATHINFO_EXTENSION);
pathinfo()
can give you other information, such as canonical path, depending on the constant you pass to it.
try this :full code to validate using in_array($file_extension, $validextensions)
if(isset($_FILES["file"]["type"]))
{
$validextensions = array("jpeg", "jpg", "png","JPG");
$temporary = explode(".", $_FILES["file"]["name"]);
$file_extension = end($temporary);
if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg")
) && ($_FILES["file"]["size"] < 1000000)//Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if ($_FILES["file"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>";
}
else
{
$file_name=$_FILES["file"]["name"];
$rand_file_name=rand()."_".$file_name;
$sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
$targetPath = "../upload/". $rand_file_name; // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath); // Moving Uploaded file
}
}
else
{
//echo "<span id='invalid'>***Invalid file Size or Type***<span>";
}
}