-1

I am trying to upload a file to a directory, ad rename it in the process. The file is uploaded to the directory, but without the file extension, i.e. 123456789. is uploaded.

if ( 0 < $_FILES['file']['error'] ) {
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}


else{
$temp = explode(".",$_FILES["file_to_upload"]["name"]);
$newfilename = sha1(uniqid(mt_rand(), true)) . '.' .end($temp);
move_uploaded_file($_FILES["file_to_upload"]["tmp_name"], "uploads/" . 
$newfilename);  
}

if (move_uploaded_file($_FILES['file']['tmp_name'], 'files/' . 
$newfilename));{
echo "File uploaded successfully!!";
}

I have also tried the following to no avail:

if ( 0 < $_FILES['file']['error'] ) {
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}



else{
$temp = explode(".",$_FILES["file_to_upload"]["name"]);
$extension = pathinfo($temp, PATHINFO_EXTENSION);
$newfilename = sha1(uniqid(mt_rand(), true)) . '.' .$extension;
move_uploaded_file($_FILES["file_to_upload"]["tmp_name"], "uploads/" . 
$newfilename);  
}



if (move_uploaded_file($_FILES['file']['tmp_name'], 'files/' . 
$newfilename));{
echo "File uploaded successfully!!";
}

Can anyone see where i'm going wrong? Everything else works.

spbrad
  • 33
  • 7

3 Answers3

3

pathinfo() takes a string as its first parameter, you are passing it an array. Try changing:

$extension = pathinfo($temp, PATHINFO_EXTENSION);

to

$extension = pathinfo($_FILES["file_to_upload"]["name"], PATHINFO_EXTENSION);
TheGentleman
  • 2,324
  • 13
  • 17
  • Unfortunately this didn't work for me. I am still getting a file without an extension. I really dont think it is the case, but could it be something to do with the following code on the button press: – spbrad Apr 19 '17 at 20:23
  • function uploadFile() { if ($("#file_to_upload").val() != "") { var file_data = $('#file_to_upload').prop('files')[0]; var form_data = new FormData(); form_data.append('file', file_data); and then ajax call.... – spbrad Apr 19 '17 at 20:24
0

Your problem is here:

$temp = explode(".",$_FILES["file_to_upload"]["name"]);
$extension = pathinfo($temp, PATHINFO_EXTENSION);

$temp becomes an array and pathinfo() expects a string. You should see this in your error log. Instead, do this:

$extension = pathinfo($_FILES["file_to_upload"]["name"], PATHINFO_EXTENSION);
Walker Boh
  • 750
  • 6
  • 13
0

I had decalred the directory as "uploads" in one part of the code, and "files" in another. Same with "file" and "file to upload". Silly mistake.

I also removed explode and added line of code suggested by GentlemanMax.

Solution:

if ( 0 < $_FILES['file']['error'] ) {
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}


else{  
$ext = pathinfo(($_FILES["file"]["name"]), PATHINFO_EXTENSION);
$newfilename = sha1(uniqid(mt_rand(), true)).'.'.$ext;
move_uploaded_file($_FILES["file"]["tmp_name"], "files/" . 
$newfilename);  
}


if (move_uploaded_file($_FILES['file']['tmp_name'], 'files/' . 
$newfilename));{
echo "File uploaded successfully!!";
}
spbrad
  • 33
  • 7