-1

I have a html form + php for uploading 3 pdfs similarly. while uploading i have to cut the names of the files. For instance instead of AB_2020_02_02.pdf i want to have it changed in AB.pdf - for all three files simultaneously. I tried with rename($original_filename, substr($original_filename, 2) . '.pdf'); Any other ideas? This is the entire code:

<?php
// Set Upload Path
$target_dir = '';
      $_FILES = substr($_FILES, 2, strlen($_FILES) - 12);

if( isset($_FILES['fileUpload']['name'])) {

  $total_files = count($_FILES['fileUpload']['name']);
  for($key = 0; $key < $total_files; $key++) {
    // Check if file is selected
    if(isset($_FILES['fileUpload']['name'][$key]) 
                      && $_FILES['fileUpload']['size'][$key] > 0) {

      $original_filename = $_FILES['fileUpload']['name'][$key];
      rename($original_filename, substr($original_filename, 2) . '.pdf');  
    $target = $target_dir . basename($original_filename);
      $tmp  = $_FILES['fileUpload']['tmp_name'][$key];
      move_uploaded_file($tmp, $target);
    }

  }

}
?>
  • Using `rename` makes very little sense here to begin with, and they way you tried it, even less (`$_FILES['fileUpload']['name']` is the original name the file had on the client - this has little relation with what is currently available on your server.) `move_uploaded_file` is what moves the uploaded temp file to its permanent location - so any “renaming” you want to do, should be done by specifying the desired target name here. – 04FS Feb 03 '20 at 12:18
  • Never trusts file name that client provides. So change it. And your code is not securely uploading files. On the following post, I tried to describe how to securely upload files using php. You may try https://stackoverflow.com/a/59986578/7935051 – unclexo Feb 03 '20 at 12:25

2 Answers2

1

$target_dir = '';


if( isset($_FILES['fileUpload']['name'])) {

  $total_files = count($_FILES['fileUpload']['name']);
  for($key = 0; $key < $total_files; $key++) {
    // Check if file is selected
    if(isset($_FILES['fileUpload']['name'][$key]) 
                      && $_FILES['fileUpload']['size'][$key] > 0) {

      $original_filename = $_FILES['fileUpload']['name'][$key];
      $rename = explode('_',$original_filename); 
    $target = $target_dir . $rename[0].".pdf";
      $tmp  = $_FILES['fileUpload']['tmp_name'][$key];
      move_uploaded_file($tmp, $target);
    }

  }

}
Dhaval Gol
  • 160
  • 10
0

Rename directly in move_uploaded_file(...)

<?php
// Set Upload Path
$target_dir = '';
      $_FILES = substr($_FILES, 2, strlen($_FILES) - 12);

if( isset($_FILES['fileUpload']['name'])) {

  $total_files = count($_FILES['fileUpload']['name']);
  for($key = 0; $key < $total_files; $key++) {
    // Check if file is selected
    if(isset($_FILES['fileUpload']['name'][$key]) 
                      && $_FILES['fileUpload']['size'][$key] > 0) {

      $original_filename = $_FILES['fileUpload']['name'][$key];

      $target = $target_dir . substr($original_filename, 2).'.pdf' ;
      $tmp  = $_FILES['fileUpload']['tmp_name'][$key];
      move_uploaded_file($tmp, $target);
    }

  }

}
?>
Tu4n3r
  • 441
  • 5
  • 10