0

There is a photo upload code. It loads the client's photo into the database. But the problem is that it can only upload one photo. If you upload a second photo, it simply replaces it by removing the old photo.

Photo upload form display code

<div class="col-md-2" style="width: 223px; float: right; margin-top: -210px;">
    <div class="x_panel">
        <span class="input-text" style="margin-right: 5px;">Include Photo (+)</span>
        <input contenteditable="true" onchange="saveUpdate('medsoft','Inst_Int','photos','<?=$row['Int_Int'];?>',this.value);" style="margin-left: 5px;width: 30px" type="1checkbox" class="1js-switch" name="photo" value="<?= $row['photos'];?>"><? //= ($row['photos']=='+' ? 'checked' : null);?>
        <div class="x_contenti">
            <br><br>
            <span class="input-text " >Photo</span>
            <!-- <a style="height: 26px; margin-left: 10px;" href="###/kont.php?action=new_kod&rowkod=<?//=$row['Sch_Row']?>" class="jsdelete btn btn-warning btn-sm" data-alert="Add photo ? ">Add</a> -->
            <br><br>
                                  
            <a style="cursor:pointer;border:1px solid;padding:3px;background: #eee; float: right; margin-top: -35px;" data-ch="<?=$row['Count_Row']?>" data-toggle="modal" data-target="#myModal">
                Upload a photo
            </a>
<!--
<a style="cursor:pointer;border:1px solid;padding:2px;background: red;color:#fff;" href="/medsoft.php?action=del_foto&rowfoto=<?//=$row['Sn_Row']?>" class="jsdelete btn btn-warning btn-sm" data-alert="Delete photo ?">Delete photo</a>
-->
<?php 
    if ($row['Photo_dev']=='+') { 
?>
        <div style="border: 0px solid; ">
                                     
            <img id="ch-pers-img<?=$row['Ch_Rs']?>" src="/images/<?=$row['Chs_Rs']?>.jpg?t=<?=time ()?>" width="189">

        </div>
<?php
    } 
?>
        </div>
    </div>
</div>

Photo Upload Processing Code

        <div class="modal fade " id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog modal-sm" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">Photo Upload</h4>
                    </div>
                    <div class="modal body">
                    <div>
                    <form action="upplo.php" method="post" name="uplod" id="uplod" enctype="multipart/form-data">
                        <input type="file" name="filename"><br>
                        <br>
                        <input id="submit-card" type="button" form="uplod" value="Upload">
                    </form>
                </div>
    
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

Photo upload code

<?php
mysql_set_charset('utf8',$db);

if($_FILES["filename"]["size"] > 2024*3*2024) {
    echo ("File size exceeds three megabytes");
    exit;
}
$success=[];
$errors=[];
foreach( $_FILES as $file ){
    // Check if the file is loaded
    if(is_uploaded_file($file["tmp_name"])) {
        // If the file is uploaded successfully, move it
        // from temporary directory to destination
        if (move_uploaded_file($file["tmp_name"], "images/".intval($_GET['ch']).".jpg"))
            $success[]=intval($_GET['ch']).".jpg"."?t=".time();
            // $db = mysql_connect("localhost","armit_mit","oZ5hI7jID");
            // mysql_select_db("armit_mit",$db);
            $result_foto = mysql_query ("UPDATE `Persons` SET `Photos` = '+' where `Ch-Persons` = ".intval($_GET['ch'])."", $db);

        } else {
            $errors[] = "Error loading file ".intval($_GET['ch']).".jpg";
        }
    }
    echo json_encode(['success'=>$success,'errors'=>$errors]);
?>

What needs to be done so that you can upload several photos at once.

The code also contains a code for displaying a photo, which is displayed when the + sign is specified in the database in the "fhotos" line.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • What have you tried to resolve the problem? Could it help to not store all files under the same name? – Nico Haase Jan 21 '22 at 12:52
  • @Nico Haase I am new to php. While I'm still learning. I took this code as a test to learn from it. How can I make the name changeable then? Where do you need to change or add and what exactly? – Александр Курылев Jan 21 '22 at 13:00
  • Q: How can I make the name changeable? A: 1) Take the name PHP gives you (either $file["name"] or $file["tmp_name"]), 2) Assign that value to a new variable (e.g. $new_name) and 3) change the name (e.g. `$new_name = $new_name . '-' . $counter++;` read this for more details: https://www.php.net/manual/en/reserved.variables.files.php – paulsm4 Jun 09 '23 at 16:39

2 Answers2

0

You store all uploaded files under the same name:

if (move_uploaded_file($file["tmp_name"], "images/".intval($_GET['ch']).".jpg"))

If you want to use distinct names, you need to add something to the file name, like a unique ID or a counter, or any part from the original file name.

Nico Haase
  • 11,420
  • 35
  • 43
  • 69
0

If you send multiple every name getting own counters.

$tmpFilePath = $_FILES['upload']['tmp_name'][$i];

follow ready answer -> Multiple file upload in php

Nico Haase
  • 11,420
  • 35
  • 43
  • 69
Kamil Dąbrowski
  • 984
  • 11
  • 17