A
A
Andrey2018-02-06 17:05:24
PHP
Andrey, 2018-02-06 17:05:24

How to copy a file using php?

How to make the php script copy the file file.txt to the ../folder directory and name it with a random name and a random extension from the list (png, jpg, exe)
For example, to get copies of the file.txt file: h8y9z59h.exe, y3i2jyd3.png , 28pelsb4.jpg, 3vs3dwo3.png....

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stimulate, 2018-02-06
@tellst1

function getRNDFiles($file, $number) {
  $symbols = array_merge(range('a', 'z'), range(0, 9));
  $exts = array('exe', 'png', 'jpg');
  
  for ($it = 1; $it <= $number; $it++) {
    shuffle($symbols);
    
    $number_symbols = mt_rand(8, 10);
    $new_filename = '';
    
    for ($it_symbol = 1; $it_symbol <= $number_symbols; $it_symbol++) {
      $new_filename .= $symbols[array_rand($symbols)];
    }
    
    $new_filename .= '.'.$exts[array_rand($exts)];
    
    copy($file, './folder/'.$new_filename);
  }
  
  return true;
}

getRNDFiles('./file.txt', 10);

D
Dmitry, 2018-02-06
@slo_nik

Good afternoon.
To implement this script, you will need:
1) copy() or move_uploaded_file()
2) pathinfo
3) A function that will generate a random file name. You can use time() for this task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question