0
galley.
I am trying to upload multiple files that accept all types of files and save with the correct extension, but in the files . txt it saved with extension . Plain, I tried to change this extension doing a check but it was in vain.
I’m a beginner in PHP, someone can help me Please?
Follow my current code:
<?php
if(isset($_FILES['file'])){
if(count($_FILES['file']['tmp_name']) > 0){
for($q=0;$q<count($_FILES['file']['tmp_name']);$q++){
$extension = $_FILES['file']['name'];
$ext = pathinfo($extension, PATHINFO_EXTENSION);
if ($ext == 'Text'){
$ext = 'txt';
}
$filename = md5($_FILES['file']['name'][$q].time().rand(0,9999)).$ext;
move_uploaded_file($_FILES['file']['tmp_name'][$q], 'files/'.$filename);
}
}
}
?>
Had tried otherwise too:
if(isset($_FILES['file'])){
if(count($_FILES['file']['tmp_name']) > 0){
for($q=0; $q<count($_FILES['file']['tmp_name']); $q++){
$extension = $_FILES['file']['name'];
$extension = explode('.', $extension);
$extension[1];
$filename = md5($_FILES['file']['name'][$q].time().rand(0,9999)).'.'.$extension[1];
move_uploaded_file($_FILES['file']['tmp_name'][$q], 'files/'.$filename);
}
}
}
If I do the same procedure for single file upload it saves properly as . txt:
$file = $_FILES['file'];
if(isset($file['tmp_name']) && !empty($file['tmp_name'])){
$extension = $_FILES['file']['name'];
$extension = explode('.', $extension);
$extension[1];
$filename = md5(time().rand(0,9999)).'.'.$extension[1];
move_uploaded_file($file['tmp_name'], 'files/'.$filename);
}
Any hints as to why it’s going wrong? Thanks in advance.
What if the guy puts "." (dot) in the middle of the file name!?
– rbz