0

I am uploading files via php. The amount of files can be uploaded is determined by a value which is stored in database. So let's say, this value is 3, than my code show three html file input. This is working. I could also rename one file too. working extension filter, and size restrictions.. But when I tried to do this with more than one, I stacked... The new name should be the 'number of list element'-'base filename'. Example:

1-thisisanimage.jpg
2-anotherimage.jpg
3-andthisalso.jpg

I tried with foreach but I think I over-complicated. I also tried different scripts from the internet but none of them really what I just cannot do this. Anybody? :)

EDIT: added the part of my code

if (!file_exists("uploads/".$date."-".$email."-vid-".$videoID)) {
    mkdir("uploads/".$date."-".$email."-vid-".$videoID."/", 0777, true);
    }

    $imagecounter = 0;
        $_FILES["file"] = array();
    foreach ($_FILES["file"] as $file) {
        $imagecounter++;
        
            
        $filename = $_FILES["file"]["name"];
        $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
        $file_ext = substr($filename, strripos($filename, '.')); // get file name
        $filesize = $_FILES["file"]["size"];
        $allowed_file_types = array('.jpg','.jpeg','.png','.gif');  
     
        if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000))
        {   
            // Rename file
            $newfilename = "$imagecounter-".$file_basename . $file_ext;
            if (file_exists("uploads/".$date."-".$email."-vid-".$videoID."/" . $newfilename))
            {
                // file already exists error
                echo "You have already uploaded this file.";
            }
            else
            {       
                move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/".$date."-".$email."-vid-".$videoID."/" . $newfilename);
                echo "File uploaded successfully.";     
            }
        }
Community
  • 1
  • 1
Ignity
  • 25
  • 7

2 Answers2

0

When you have files in array:

$i = 1;
foreach($_FILES['file'] as $file){
    move_uploaded_file($file['tmp_name'], $i.'-'.$file['name']);
    ++$i;
}
TiMESPLiNTER
  • 5,741
  • 2
  • 28
  • 64
turson
  • 421
  • 2
  • 9
0

your problem is that the $imagecounter is allways 0, so when you upload a new file it gets the value 1 (one file in this request) instant of the number of files that are allready uploaded.

so count the files that are allready uploaded in your destination folder and increase this value +1 to get your list number work.

$uploadDir = "uploads/".$date."-".$email."-vid-".$videoID."/";
$fileInterator = new FilesystemIterator($uploadDir, FilesystemIterator::SKIP_DOTS);
$imagecounter = iterator_count($fileInterator);

but attention when delete files - eg. you have these files in the folder:

  • 1-test.png
  • 2-hello.png
  • 3-wow.png

and delete 2-hello.png when only count the number of files (wich is now 2) then the next uploaded file get the name 3-new.png unnecessary this number is allready taken by 3-wow.png

ins0
  • 3,918
  • 1
  • 20
  • 28
  • Hello! image counter is only 0 before the for each. The program increase the image counter at each file :) should. – Ignity Jul 20 '14 at 23:56
  • it increase the number only per file per current upload, not for allready uploaded files! – ins0 Jul 21 '14 at 00:55