0

I have an upload form on a site that uploads multiple files to a server, and also sends me an email. It is written in php, with the main file part being the following:

if (isset($_FILES) && (bool) $_FILES) {
    $files = array();
    $ext_error = "";
    // Define allowed extensions

    // blahblahblah checking

    // Store attached files in uploads folder
    $server_file = dirname(__FILE__) . "/uploads/" . $path_part['basename'];
    move_uploaded_file($temp_name, $server_file);
    array_push($files, $server_file);

Nowadays, people upload pictures from a cell phone and often they are all named the same file name: image.jpg (or something similar) - so they get overwritten.

I would like to append a counter on to each multiple file (like 1,2,3...) name so they are uploaded and sent with unique names, even though the client sends them as same name.

Something like:

if (isset($_FILES) && (bool) $_FILES) {
    $files = array();
    $ext_error = "";
    // Define allowed extensions

// counter= counter++;
// newFilename=oldFileName+String(counter);

// doRestStuffNewFileName();

// blahblahblah checking

// Store attached files in uploads folder
$server_file = dirname(__FILE__) . "/uploads/" . $path_part['basename'];`
`move_uploaded_file($temp_name, $server_file);`
array_push($files, $server_file);`

How can I modify it as such in php?

ok new comments:

I would like:

for int i=0;i<files[attached];i++;
fileName=files[i]
newFileName=filename+String(Integer(i));
uploadWithNewFileName();
writeToServerWithNewFileName();

This is php current code:

if (isset($_FILES) && (bool) $_FILES) {
$files = array();
$ext_error = "";
foreach ($_FILES as $name => $file) {
    if (!$file['name'] == "") {
        $file_name = $file['name'];
        $size += $file['size'];
        $temp_name = $file['tmp_name'];
        $path_part = pathinfo($file_name);
        $ext = $path_part['extension'];

        // Store attached files in uploads folder
        $server_file = dirname(__FILE__) . "/uploads/" . $path_part['basename'];
        move_uploaded_file($temp_name, $server_file);
        array_push($files, $server_file);
    }
}

Why doesnt this work:

if (isset($_FILES) && (bool) $_FILES) {
$files = array();
$ext_error = "";
counter =0; //My code

foreach ($_FILES as $name => $file) {
   counter++; //My code
    if (!$file['name'] == "") {
        $file_name = $file['name'];
        $size += $file['size'];
        $temp_name = $file['tmp_name'];
        $path_part = pathinfo($file_name) + counter; //My code
        $ext = $path_part['extension'];

        // Store attached files in uploads folder
        $server_file = dirname(__FILE__) . "/uploads/" . $path_part['basename'];
        move_uploaded_file($temp_name, $server_file);
        array_push($files, $server_file);
    }
}
  • 3
    Maybe duplicate https://stackoverflow.com/questions/460164/unique-and-temporary-file-names-in-php – Muhammad Usman Jun 05 '17 at 23:31
  • Seems like a lot of your code is missing.. where is `$path_part`, `$temp_name`? The loop that goes through the files? – Daniel Jun 05 '17 at 23:34
  • if (isset($_FILES) && (bool) $_FILES) { $files = array(); foreach ($_FILES as $name => $file) { if (!$file['name'] == "") { $file_name = $file['name']; $size += $file['size']; $temp_name = $file['tmp_name']; $path_part = pathinfo($file_name); $ext = $path_part['extension']; $server_file = dirname(__FILE__) . "/uploads/" . $path_part['basename']; move_uploaded_file($temp_name, $server_file); array_push($files, $server_file); } } – HowdyDoody Jun 06 '17 at 00:22
  • @HowdyDoody I have just fixed your formatting. Please add your last comment to your question and try to mirror the code tabbing that I have used. Always post new question information directly into your question, so that future readers don't have to read the comments to get the full story. ...then delete your comment. Also, if the duplicate link resolves your question, you may delete your question as you won't need a posted answer. – mickmackusa Jun 06 '17 at 00:25
  • Add date including HH MM SS? image-2017-06-05-172315.jpg also helps a user tell them apart (2017 vs. 2015). – Dave S Jun 06 '17 at 00:33
  • just a number- please keep it simple- just add on 1,2,3 etc... – HowdyDoody Jun 06 '17 at 00:42
  • I added in the comments as an edit- – HowdyDoody Jun 06 '17 at 03:13
  • I just need a simple solution to a simple issue- just add on a counter to each file name. that is all I need it to do. Can anyone help? – HowdyDoody Jun 06 '17 at 03:13
  • You're missing a `$` on your variable `counter`. – Bytewave Jun 06 '17 at 03:41
  • Also, concatenation is `.`, not `+`. – Bytewave Jun 06 '17 at 03:41

0 Answers0