1

I have this code

<?php //  display  file  upload  form
if  (!isset($_POST['submit']))  { ?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']?>" method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="8000000" /> Select file:

<input type="file" name="data" />

<input type="submit" name="submit" value="Upload  File" /></form>

<?php
}  else  {
//  check  uploaded  file  size
if  ($_FILES['data']['size']  ==  0)  {
die("ERROR:  Zero  byte  file  upload");
} //  check  if  this  is  a  valid  upload
if  (!is_uploaded_file($_FILES['data']['tmp_name']))   {
die("ERROR:  Not  a  valid  file  upload"); } //  set  the  name  of  the  target  directory
$uploadDir  =  "./uploads/"; //  copy  the  uploaded  file  to  the  directory
move_uploaded_file($_FILES['data']['tmp_name'],  $uploadDir  .  $_FILES['data']['name'])  or  die("Cannot  copy  uploaded  file"); //  display  success  message
echo  '<p style="text-align: center">
Soubor byl úspěšně nahrán na server. <br>
<a href="index.php" class="button1">Zpět</a>
</p>

' ; } ?>

And I have a problem - it works, uploads the file to server but if there is a break in the file name, I cannot download it. e.g I want to upload "my salary únor.pdf", but the server keeps looking for file "my". Is there a way to modify this code to change the breaks in the file name to "_"? Also I would like the server to be in utf-8 letter (as the server changes the file name to silly names e.g. u?nor) So if there is a option for that too that would be great. (or just let php change the file name inty "my_salary_unor" Thank you all for your help ad sugestions.

Makyen
  • 31,849
  • 12
  • 86
  • 121
Martin Švejda
  • 83
  • 3
  • 11
  • Welcome to Stack Overflow! Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. – Mogsdad Jan 25 '18 at 17:27

1 Answers1

1

Firstly, store you filename in a variable and trim it.

$filename = trim($_FILES['data']['name']);

Then use string replace function to replace the whitespace with _ (underscore).

$filename = str_replace(' ','_',$filename);

Now the filename which you get will be white space free string.

Hope this can help you.

BEingprabhU
  • 1,618
  • 2
  • 21
  • 28