How to rename multiple files and displayed it vertically (v2022)

Tips submitted by PHPMaker users
Post Reply
mobhar
User
Posts: 11841

How to rename multiple files and displayed it vertically (v2022)

Post by mobhar »

This following tip will show you how to upload multiple files, rename the files while uploading based on the auto-increment ID field, and then display the files vertically (multi-line) instead of horizontally (one-line) in Delete, List, and View pages.

**1. Create a MySQL table named upload_files using the following SQL script:

CREATE TABLE `upload_file`  (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `File_Name` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `Date_Uploaded` datetime NOT NULL,
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

**2. Create a new fresh project and connect to that database and table above.

**3. Go to Fields setup, select File_Name field, choose File, and then enable Multiple from Edit Tag pane -> File Upload.

**4. Insert the following code into Row_Inserting server event, so the complete code as follows:

// Row Inserting event
function Row_Inserting($rsold, &$rsnew)
{
    // Enter your code here
    // To cancel, set return value to false
    $NewFiles = explode(Config("MULTIPLE_UPLOAD_SEPARATOR"), $rsnew["File_Name"]);
    $NextID = ExecuteScalar("SELECT COALESCE(MAX(ID),0) +1 CNT FROM upload_file") ;

    $FileCount = count($NewFiles);

    if (trim($NewFiles[0]) == '') return true;  // skip if no file uploaded
  
    for ($i = 0; $i < $FileCount; $i++) {
        if (trim($NewFiles[$i]) != '') {
            $files = $NewFiles[$i];
            $file_extension = substr(strtolower(strrchr($files, ".")), 1);
            $files = $NextID. "-" .date("YmdHis", strtotime($rsnew['Date_Uploaded'])).  "-" . ($i + 1) . "." . $file_extension;
            $NewFiles[$i] = $files;
        }
    }

    $sFileName = implode(Config("MULTIPLE_UPLOAD_SEPARATOR"), $NewFiles);
    $rsnew['File_Name'] = $sFileName;
    
    return true;
}

**5. Put the following code into Startup Script section of **Delete Page, List Page, and also View Page:

$('div.d-flex.flex-row.ew-images').removeClass('flex-row').addClass('flex-column');

If you want to reverse the order of File_Name, then simply change flex-column to flex-column-reverse.

**6. Generate ALL the script files, and enjoy the final result from the generated web application via your favorite browser.


titemi21
User
Posts: 1

Post by titemi21 »

Thanks for spending so much time to educate some of us who are learning how to improve on the use of the PHPMaker application. Your write ups are very helpful and in deed I am most grateful for your posts. i have tried to check out this code but found out that it works only wen you are inserting the data but wen you want to update or edit the information it changes the name back to the original file name.

In the process where you have more than one column to upload for example:

CREATE TABLE `uploadtest` (
  `userid` int(11) NOT NULL AUTO_INCREMENT COMMENT 'User ID',
  `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'User Name',
  `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Password',
  `picture` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Picture',
  `picture2` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `doc` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `date_uploaded` datetime DEFAULT NULL,
  PRIMARY KEY (`userid`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Where you need to upload into the Picture, Picture2 and doc column how do u go about it to reflect the new names at same time.

Many Thanks


mobhar
User
Posts: 11841

Post by mobhar »

If you want to implement it also for Edit page, then you may use Row_Updating server event. You may post your code for more discussion.


neo808080
User
Posts: 10

Post by neo808080 »

I am stuck in the code by wanting to rename the files of all the fields.
Although it uploads the images only in the first 6 fields it replaces the name and the rest it no longer does, leaving the name that comes by default from the file
I really can't find the problem
Can somebody help me ?

// Row Inserting event
function Row_Inserting($rsold, &$rsnew)
{
$codigo_propiedad=date("YmdHis");
$rsnew['propiedad_activa'] = $codigo_propiedad;

$campos = array("p_hab_1", "p_hab_2", "p_hab_3", "p_hab_4", "p_hab_5", "p_hab_6", "p_liv_1", "p_liv_2", "p_c_1", "p_te_1", "p_te_2", "p_par_1", "p_par_2", "p_tra_1", "p_tra_2", "p_b_1", "p_b_2", "p_b_3", "p_b_4");

$longitud = count($campos);
for($c = 0; $c < $longitud; $c++)
      {
      $NewFiles = explode(Config("MULTIPLE_UPLOAD_SEPARATOR"), $rsnew[$campos[$c]]);
      $FileCount = count($NewFiles);
      if (trim($NewFiles[0]) == '') return true;  // skip if no file uploaded (omitir si no se cargó ningún archivo)
  
        for ($i = 0; $i < $FileCount; $i++) {
              if (trim($NewFiles[$i]) != '') {
                  $files = $NewFiles[$i];
                  $file_extension = substr(strtolower(strrchr($files, ".")), 1);
                  $reducir = str_replace("propiedad_", "", $campos[$c]);
                  
                  $files = date("YmdHis")."-".$reducir."-".($i + 1) .".". $file_extension;
                  $NewFiles[$i] = $files;
                  }
          }
          $sFileName = implode(Config("MULTIPLE_UPLOAD_SEPARATOR"), $NewFiles);
          $rsnew[$campos[$c]] = $sFileName;

       }
   
    return true;
}

From what I see, the problem is generated when one of the fields is empty, so no file was loaded in that input.

It is sure that the problem is in this line

if (trim($NewFiles[0]) == '') return true;

I don't know how to fix it. I have modified it to this:

if (trim($NewFiles[0]) != '')
        { .... }

But I got an error: " Notice: Undefined index: .... "


arbei
User
Posts: 9603

Post by arbei »

Since it is in a for loop, you should use continue, not return true. Read continue.


neo808080
User
Posts: 10

Post by neo808080 »

Thanks


danilo.macri
User
Posts: 109

Post by danilo.macri »

the code works great. I noticed that after using the sparsce code the "," as a separator for multiple files. in card view but if I insert more files, only the first file is displayed. is there a way to view the files vertically also the card view? thank you


Post Reply