Upload file with ajax memory overload
Results 1 to 2 of 2

Thread: Upload file with ajax memory overload

  1. #1
    Junior Member
    Join Date
    Feb 2017
    Posts
    1

    Upload file with ajax memory overload

    sorry for english , in my project the user can upload from 1 to 14 file (file from 2mb to 50mb)with ajax ,this is the simple php code :

    Code:
    //percorso della cartella dove mettere i file caricati dagli utenti
    $uploaddir = $_SERVER['DOCUMENT_ROOT'].'/uploaded/';
    
    
    
    ############################################
    #              FILE 1                      #
    ############################################
    
    //solo per il file 1
    if(!isset( $_FILES['stl1']['tmp_name']) OR $_FILES['stl1']   ['tmp_name']==''){
       $user->set_errors('File 1 non selezionato!!!');
    
    }
    
    //se presente il file 1
    if(isset( $_FILES['stl1']['tmp_name']) AND $_FILES['stl1']['tmp_name']!=''){
       //se non c' l'id del materiale
       if(!isset($_POST['materiale1'])){
          $user->set_errors('Manca il materiale!!!');
    
       }
    
       else{
    
          //Recupero il percorso temporaneo del file
          $userfile_tmp = $_FILES['stl1']['tmp_name'];
    
          //recupero il nome originale del file caricato
          $userfile_name = $_FILES['stl1']['name'];
    
    
    
      //copio il file dalla sua posizione temporanea alla mia cartella upload
      if (move_uploaded_file($userfile_tmp, $uploaddir . $nome_dropbox)) {
        //Se l'operazione  andata a buon fine...
           //inserisco nel db il file 
                        if($user->insert_lavorazioni($_POST['materiale1'],
                                     $_POST['numero_scheda'],
                                     $_POST['colore'],
                                     $userfile_name)){
    
                                     $user->debug[]='<br>Lavorazione inserita nel db';
    
    
    
                        }else{
                              $user->set_errors('Problemi nell\'inserimento lavorazione nel db');
    
                        }
    
    
      }else{
        //Se l'operazione  fallta...
        $user->set_errors('Upload NON valido!'); 
      }
    
    
    
    
    
    
    
       }
    
    }
    For every file i called another script equal to this .When more user upload file simultaneously the server memory go to overload and other ajax request for example populate dinamically select input dont work .

    How can i reduce memory usage ?

    this is my server features:
    Cores: 2 x 2.1 ghz - Physical Memory: 1 Gbyte - Virtual Memory: 8 Gbyte - HD: SSD.12 Core Server Minimum - 64 GB RAM Server Minimum

    memory_limit is set at 768Mb

  2. #2
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    8,414
    Limit the number of simultaneous requests. For example, you might have a "counter" system in the DB:

    Code:
    mysql> select * from upload;
    +------------+
    | user_count |
    +------------+
    |          0 |
    +------------+
    1 row in set (0.00 sec)
    Then your AJAX server does something like this:

    PHP Code:
    $max_uploads 4//arbitrary number

    $s "select user_count from upload;";
    $q $db->query($s);
    $r $q->fetch_assoc();
    $c $r['user_count'];

    if (
    $c $max_uploads) {
       
    //return error to AJAX and quit
       
    die("Too many users, please try again in a moment.");
    }

    $s "update $table_name set user_count = user_count +1;";
    $q $db->query($s);

    //do the upload here

    $s "update $table_name set user_count = user_count -1;";
    $q $db->query($s);

    //respond back to the AJAX, telling whether the upload was successful or not 
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •