1

I would like to use a form to send a few pictures via email. Unfortunately, this does not work as reliably as I had hoped. If I want to send a few small pictures it works but as soon as the pictures get a little bit bigger they are not noticed at all.

Here is my code

HTML:

<div class="form-check">
                            <div class="input-group">
                                <!--<input type="file" class="form-control" id="files" name="uploaded_file[]" accept="image/jpeg,image/gif,image/png,application/pdf" multiple="multiple"/>-->
                                <button type="button" class="btn btn-outline-send" data-bs-toggle="modal" data-bs-target="#exampleModal">
                                <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="upload" class="svg-inline--fa fa-upload " role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
                                    <path fill="currentColor" d="M296 384h-80c-13.3 0-24-10.7-24-24V192h-87.7c-17.8 0-26.7-21.5-14.1-34.1L242.3 5.7c7.5-7.5 19.8-7.5 27.3 0l152.2 152.2c12.6 12.6 3.7 34.1-14.1 34.1H320v168c0 13.3-10.7 24-24 24zm216-8v112c0 13.3-10.7 24-24 24H24c-13.3 0-24-10.7-24-24V376c0-13.3 10.7-24 24-24h136v8c0 30.9 25.1 56 56 56h80c30.9 0 56-25.1 56-56v-8h136c13.3 0 24 10.7 24 24zm-124 88c0-11-9-20-20-20s-20 9-20 20 9 20 20 20 20-9 20-20zm64 0c0-11-9-20-20-20s-20 9-20 20 9 20 20 20 20-9 20-20z"></path>
                                </svg>
                                Dokumente hochladen
                                </button>
                                <!-- Modal -->
                                <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">     
                                <div class="modal-dialog modal-dialog-centered">
                                    <div class="modal-content">
                                      <div class="modal-header">
                                        <h5 class="modal-title" id="exampleModalLabel">Bitte wählen Sie aus, welche Dokumente Sie hochladen möchten.</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                      </div>
                                      <div class="modal-body">
                                      <input type="file" class="form-control" id="files" name="uploaded_file[]" accept="image/jpeg, image/jpg, image/gif, image/png, application/pdf" multiple="multiple" value=""/>
                                      <output id="list"></output>
                                      <div class="progress" id="progress_bar" style="display:none; ">
                                        <div class="progress-bar" id="progress_bar_process" role="progressbar" style="width:0%; background-color: #a1131c;">0%</div>
                                      </div>
                                      <div id="uploaded_image" class="row mt-2">
                                      </div>
                                        <small>
                                          <strong>
                                              <br>
                                              Hinweis: Es können <u>maximal 15</u> Dateien aufeinmal versendet werden! <br>Unterstützte Dateiformate: PDF, JPG und PNG
                                            </strong>
                                        </small>
                                      </div>
                                      <div class="modal-footer">
                                          <button type="button" class="btn btn-outline-send" id="clearFiles">Inhalte löschen</button>
                                          <button type="button" class="btn btn-outline-send" data-bs-dismiss="modal">Weiter</button>
                                        <!--<button type="button" class="btn btn-primary">Save changes</button>-->
                                      </div>
                                    </div>
                                  </div>
                                </div>
                            </div>
                        </div>

PHP:

$totalFiles = count($_FILES['uploaded_file']['tmp_name']);
                    for ($i = 0; $i < $totalFiles; $i++) {
                        //if (($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
                            $fileMimeType = mime_content_type($_FILES['uploaded_file']['tmp_name'][$i]);
                            $path = $_FILES['uploaded_file']['tmp_name'][$i];
                            $name = $_FILES['uploaded_file']['name'][$i];
                            if(in_array($fileMimeType, $imageMimeTypes)) {
                                $mail->addattachment($path,$name);
                                echo $name . ' successfull <br>';
                            } 
                    }

JavaScript:

    function handleFileSelect(evt) {
  var files = evt.target.files; // FileList object

  // Loop through the FileList and render image files as thumbnails.
  for (var i = 0, f; f = files[i]; i++) {

    // Only process image files.
    if (!f.type.match('image.*') && !f.type.match('pdf')) {
    console.log(f.type)
      continue;
    }
    
    // A flag to know if the file is an image or a PDF
    let source_is_image = true;
    if(!f.type.match('image.*')){
      source_is_image = false;
    }

    var reader = new FileReader();

    // Closure to capture the file information.
    reader.onload = (function(theFile) {
      return function(e) {
        // Render thumbnail.
        var span = document.createElement('span');
        
        // Based on the flag, decide to use the image or the PDF logo
        var file = source_is_image ? e.target.result : "images/PDFlogo.png";//"https://upload.wikimedia.org/wikipedia/commons/8/87/PDF_file_icon.svg";
        
        span.innerHTML = ['<img class="uploadFile" src="', file,
          '" title="', escape(theFile.name), '" style=" height: 100px; max-width: 150px; border: 1px solid #000; margin: 10px 5px 0 0;"/>'
        ].join('');
        document.getElementById('list').append(span);
      };
    })(f);

    // Read in the image file as a data URL.
    reader.readAsDataURL(f);
  }
}

document.getElementById('files').addEventListener('change', handleFileSelect, false);

// To clear the images
document.querySelector("#clearFiles").addEventListener("click", function(){
  document.querySelector("#list").innerHTML = "";
  document.querySelector("#progress_bar").innerHTML = "";
  document.querySelector("#uploaded_image").innerHTML = "";
});

I can not say exactly what it is and hope that I can be helped here

allstar_141
  • 43
  • 1
  • 1
  • 8
  • First thing, have you checked your php.ini file to see if there is still a limit on file uploading? – thenullptr Jan 23 '22 at 17:14
  • yes, it says a maximum of 20 images but that also happens regardless of how many images I want to send. Let's take 5 images of which for example 1 image is larger than 1 mb this image will be ignored. – allstar_141 Jan 23 '22 at 17:19
  • @thenullptr i think it's because upload_max_filesize is set to 2M, right? How can I configure that? – allstar_141 Jan 23 '22 at 17:27
  • Check the documentation [here](https://www.php.net/manual/en/ini.core.php) - search for `upload_max_filesize` and `post_max_size` – Professor Abronsius Jan 23 '22 at 17:42
  • @allstar_141 yes I believe this is the issue, you need to open the php.ini file and increase the upload_max_filesize and post_max_size variables as Professor Abronsius said. This may help: https://www.tecmint.com/increase-file-upload-size-in-php/ – thenullptr Jan 23 '22 at 18:03
  • @ProfessorAbronsius have now changed this but would like to know if I can also specify this in the code. – allstar_141 Jan 23 '22 at 18:37
  • so at the end where the website is hosted I have no access to this file for this reason I am looking for another way – allstar_141 Jan 23 '22 at 18:57
  • you should be able to use `ini_set` - [more](https://www.php.net/manual/en/function.ini-set.php) and also check out [this answer](https://stackoverflow.com/questions/6135427/increasing-the-maximum-post-size) perhaps – Professor Abronsius Jan 23 '22 at 21:29
  • @ProfessorAbronsius Thanks for the help I will try this out later – allstar_141 Jan 25 '22 at 09:09

0 Answers0