PHP - Multiple upload files not saved with . txt extension

Asked

Viewed 33 times

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!?

1 answer

0

Missed per file index like you did on md5:

Of:

$extension = $_FILES['file']['name'];

To:

$extension = $_FILES['file']['name'][$q];
  • What if the guy puts "." (dot) in the middle of the file name!?

  • I tested the code you reported as current implementation, which has not explode and yes to pathinfo. With pathinfo if the guy puts "name.do.file.com.point.no.meio.txt" will return only "txt"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.