-2

Okay so, the below php upload script already WORK, the part didn't work is only of renaming file if exist.

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){

foreach ($_FILES['files']['name'] as $f => $img_name) {     
    if ($_FILES['files']['error'][$f] == 4) {
        continue;
    }
    if ($_FILES['files']['error'][$f] == 0) {              
        if ($_FILES['files']['size'][$f] > $max_file_size) {
            $message[] = "$img_name est trop lourde !";
            continue;
        }
        elseif( ! in_array(pathinfo($img_name, PATHINFO_EXTENSION), $valid_formats) ){
            $message[] = "$img_name est pas valide !";
            continue;
        }
        else{
            if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$img_name)) {
                while(file_exists($path . $img_name)){
                    $increment++;
                    $img_name = $name.$increment.'.'.$extension;    
                    $count++;
                }
            }
        }
    }
  }
}

I have search a lot on php doc, try fews fews way to go but .. when i'm trying a file with a name already uploaded before, it's not changing the actual upload file.

bansi
  • 55,591
  • 6
  • 41
  • 52
Cory
  • 35
  • 5
  • possible duplicate of [Multiple File Upload PHP](http://stackoverflow.com/questions/14975340/multiple-file-upload-php) – e4c5 Sep 25 '15 at 03:14
  • It would be better if you determine the `$img_name` before `move_uploaded_file`. BTW, shouting is not an answer to anything :) – bansi Sep 25 '15 at 03:22
  • don't worry questions won't get marked duplicate that easily. It need to be verified by much senior moderators before it is marked as duplicate. the possible duplicate just helps the moderators by telling them where to look for. – bansi Sep 25 '15 at 03:32
  • possible duplicate of [Multiple file upload in php](http://stackoverflow.com/questions/2704314/multiple-file-upload-in-php) – Elias Nicolas Sep 25 '15 at 03:36
  • @e4c5 and Elisa this is not the duplicate of those. OP is having problem renaming the file if it exists not uploading, as per question – bansi Sep 25 '15 at 03:39
  • @bansi file upload with PHP remains the most frequently asked question in Stackoverflow. A little research often leads the answer. Even if the specific question I linked to isn't the exact duplicate, there will be 10 others. – e4c5 Sep 25 '15 at 03:47
  • @e4c5 The link you have given is completly different of my question, seriously if you are just here to give wrong link .. don't help me please. And by the way, Who you are for juge I don't I search ? Be sure, I have search, A LOT. – Cory Sep 25 '15 at 03:52
  • @e4c5 a little reading the question also helps. OP specifically noted, that the problem is not with uploading, but renaming if file already exists, and if you look at the code the renaming part is a misplaced loop. – bansi Sep 25 '15 at 04:00
  • @bansi that still makes it off topic since simple typographic errors are off topic for stack overflow. – e4c5 Sep 25 '15 at 04:08

1 Answers1

0

You are determining the filename after you upload the file. determine it before. Change like:

        while(file_exists($path . $img_name)){
            $increment++;
            $img_name = $name.$increment.'.'.$extension;    
            $count++;
        }
        if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$img_name)) {
            // File Uploaded !!!
        }

Edit:
I have changed your code. Comments in code.

$valid_formats = array("jpg", "JPG", "png", "PNG" , "bmp", "BMP");
$max_file_size = 1024*6000; //60 000 kb - 6 mb
$path = "../../../img/final/img_recipes/"; //directory
$count = 0;
$uploaded_image_names = array(); //create a new array

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    foreach ($_FILES['files']['name'] as $f => $img_name) {     
        if ($_FILES['files']['error'][$f] == 4) {
            continue;
        }
        if ($_FILES['files']['error'][$f] == 0) {

            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$img_name est trop lourde !";
                continue;
            }
            elseif( ! in_array(pathinfo($img_name, PATHINFO_EXTENSION),     $valid_formats) ){
                $message[] = "$img_name est pas valide !";
                continue;
            }
            else{
                // Moved name and extension initialization to here.
                // Here is where you want to determine the actual filename
                $name = pathinfo($img_name, PATHINFO_FILENAME);
                $extension = pathinfo($img_name, PATHINFO_EXTENSION);
                $increment = 0;
                while(file_exists($path . $img_name)){
                    $img_name = $name.$increment.'.'.$extension;
                    $increment++;
                }
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$img_name)) { 
                        $count++;
                        //Store the uploaded filenames to array here
                        $uploaded_image_names[] = $path.$img_name;
                }
            }
        }
    }
}
foreach ($uploaded_image_names as $uploaded_image_name){
    //store the $uploaded_image_name to db
}

Note: I have not tested this, as I don't have PHP available with me now.

bansi
  • 55,591
  • 6
  • 41
  • 52
  • So I have update my code, now the php rename my file but not correctly, it's not taken the actual name and skip the extension lol .. I will put below my code, I have quickly try some tricks but I didn't catch the wrong things here .. – Cory Sep 25 '15 at 03:39
  • I have test over my host, work like a charm. God thank you bansi, really thank's a lot ! I can sleep in peace and my smile come back :) Next time I will not forget to place variable at the correct place and the while too ; ) – Cory Sep 25 '15 at 04:02
  • nice to hear it helped you. Next time don't get angry when something don't work. We all are here to help. – bansi Sep 25 '15 at 04:03
  • I've got a related problems now, I don't know if I can post it here too, if not, just don't answer or tell me to open another thread instead. – Cory Sep 25 '15 at 04:23
  • After file is upload, I use the code sound like; $img1 = "http://url.com/img_recipes/".$_FILES["files"]['name'][0]; $img2 = "http://url.com/img_recipes/".$_FILES["files"]['name'][1]; and so, I have change this for; $img1 = "http://url.com/img_recipes/".$img_name[0]; $img2 = "http://url.com/img_recipes/".$img_name[1]; To be insert in my DB at same time of upload, when I check in the DB row, I only see the first letter of file name, there a way to have the complete name for db to, instead for "$_FILES["files"]['name'][0]" ? * I use the [0] because I have a multiple upload at same time. – Cory Sep 25 '15 at 04:28
  • Just let me know if you can handle me or not, sorry for double post, I have skip the character limit ^^ – Cory Sep 25 '15 at 04:28
  • updated my answer. please note just typed in too hard to verify even syntax from mobile :) – bansi Sep 25 '15 at 05:13
  • Thank's ! Can I use the variable like this after; $uploaded_image_name[0]; $uploaded_image_name[1]; ?? I mean, to be store in DB. – Cory Sep 25 '15 at 05:26
  • yes you can. `$uploaded_image_name` is an array and you get total items from `$count` – bansi Sep 25 '15 at 05:30
  • EDIT- Okay, so, this is exactly what I have do; put in my query likes this, for every ten images (and yes I have up by one the number each line ^^) but in DB is only stored dot "." and slash "/", do you want a screenshot to a better view of the situation ?. GetSQLValueString($uploaded_image_name[0], "text"), – Cory Sep 25 '15 at 05:34
  • Okay, instead of '$uploaded_image_name[0]' I use '$uploaded_image_names[0]' and now work, it's correct ? – Cory Sep 25 '15 at 05:43
  • I have tested on every-side, big big thank's .. seriously it's really appreciated bansi, thank's for the time you have allocated for me and to be very helpful. Many thank's ! Now I go sleep for real :) – Cory Sep 25 '15 at 05:50