在PHP中将文本附加到文件名? [关闭]

Hello: I am relatively new to PHP. I have a directory with 16 .jpg files in it. (eg: /images/boxer1.jpg, boxer2.jpg etc...)

As part of a larger PHP file copy and upload procedure, I want to create a second version of these files by appending a _h to the file (eg: boxer1_h.jpg, boxer2_h.jpg, etc..); leaving the new files in the same directory with the original files for further manipulation.

What would be the easiest way to accomplish this using PHP programming?

Mureinik's answer is a good solution, it copies the uploaded file and changes it's name. Here is a good way to do it during the upload.

You used

move_uploaded_file ($uploaded, 'path/to/uploads/'.$filename);

Use it again:

move_uploaded_file ($uploaded, 'path/to/uploads/'.$str_replace('.', '_h.', $filename));

AND THAT, assuming you give a proper filename to the uploaded files without any other DOTS besides the one to separate the extension from the name.

You can get a list of files in a directory using scandir. Then, just replace the . with _h. and you're set to go:

$origFiles = scandir('/path/to/directory');
foreach ($origFiles as $origFile) {
    $destFile = $str_replace('.', '_h.', $origFile);
    copy($origFile, $destFile);
}