Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

upload file to server

Questions related to customising nuBuilder Forte with JavaScript or PHP.
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: upload file to server

Unread post by kev1n »

Ok - and what should happen if you upload a new file in a row for which you have already uploaded one? Should the previously uploaded file be deleted?
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: upload file to server

Unread post by kev1n »

johan wrote: Just when I delete a subrow containing a file.
Than the row is deleted in de sql table.
You can add this code in PHP AS (After Save) event:

Code: Select all

$uploaddir = $_SERVER['DOCUMENT_ROOT']."/libs/upload/documents/";

deleteFiles('sample_files',$uploaddir);  // Replace sample_files with your subform id

function deleteFiles($form, $uploaddir) {
  $o = nuSubformObject($form);
  
  for($i = 0 ; $i < count($o->rows) ; $i++){
      
		$fileid = $o->rows[$i][1];
		$filename = $o->rows[$i][2];
		$delete = $o->rows[$i][3];
		if ($delete == "1") {
			deleteFile($uploaddir . $fileid. "_". $filename);
		}
   }

}


function deleteFile($file)
{
    if (file_exists($file))
    {
        if (! unlink($file)) 
		{
			nuDebug("Cannot delete the file ".$file);
		}
    } else
	{
		nuDebug("File does not exist ".$file);
	}
}

Modify this part (numbers 1 ... 3). These are the column numbers

Code: Select all

	$fileid = $o->rows[$i][1];
		$filename = $o->rows[$i][2];
		$delete = $o->rows[$i][3];
You can also find them out if you run this PHP code in the AS first. (Replace sample_files with your subform id)

Code: Select all

nuDebug(nuSubformObject('sample_files'));
The you'll find in the debug a log entry like this:

Code: Select all

[0] : stdClass Object
(
    [id] => sample_files
    [foreign_key] => files_main_id
    [primary_key] => files_id
    [object_id] => 5dd0435d0785413
    [table] => files
    [action] => save
    [rows] => Array
        (
            [0] => Array
                (
                    [0] => -1  // this is the first column with the primary key
                    [1] => 1574345421_5dd69acd4e526_5dd69ab2b4db682
                    [2] => Test.pdf
                    [3] => 0 // last column: if delete then 1 else 0
                )
johan
Posts: 422
Joined: Sun Feb 27, 2011 11:16 am
Location: Belgium
Been thanked: 3 times

Re: upload file to server

Unread post by johan »

Kev1n

NuDebug returns
$
[0] => -1
[1] => Werken_aan_een_betere_aanpak_van_schulden_SAMvzw_15_april_2019_1_0.pdf
[2] => 1574364113_5dd6e3d1b3004_5da701edb0fb627
[3] => 0
So I've adjusted
$fileid = $o->rows[$i][2];
$filename = $o->rows[$i][1];
$delete = $o->rows[$i][3];
But when I save my form, the file is not deleted.

I've added nudebug(($uploaddir . $fileid. "_". $filename));
This returns
[0] : /var/www/html/documents/_[0] : /var/www/htmllibs/upload/documents/_
Johan
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: upload file to server

Unread post by kev1n »

johan wrote: NuDebug returns
$
[0] => -1
[1] => Werken_aan_een_betere_aanpak_van_schulden_SAMvzw_15_april_2019_1_0.pdf
[2] => 1574364113_5dd6e3d1b3004_5da701edb0fb627
[3] => 0
Could you show the full output of nudebug? And also the PHP code if you have changed anything.
johan
Posts: 422
Joined: Sun Feb 27, 2011 11:16 am
Location: Belgium
Been thanked: 3 times

Re: upload file to server

Unread post by johan »

Kev1n

Here's the full debug
[0] : stdClass Object
(
[id] => blagen
[foreign_key] => bl_reg_id
[primary_key] => bl_id
[object_id] => 5dd16c9f90f9981
[table] => zzzsys_bijlage
[action] => save
[rows] => Array
(
[0] => Array
(
[0] => 5dd789fd50cf86d
[1] => Werken_aan_een_betere_aanpak_van_schulden_SAMvzw_15_april_2019_1_0.pdf
[2] => 1574406649_5dd789f949bf1_5da701edb0fb627
[3] => 1
)

[1] => Array
(
[0] => -1
[1] =>
[2] =>
[3] => 1
)

)

[columns] =>
[chartData] =>
[chartDataPivot] =>
[edited] => Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)

[1] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)

)

[deleted] => Array
(
[0] => 1
[1] => 1
)

[fields] => Array
(
[0] => ID
[1] => bl_file_name
[2] => bl_file_id
[3] => nuDelete
)

)
and the php

Code: Select all

nuDebug(nuSubformObject('blagen'));

    $uploaddir = $_SERVER['DOCUMENT_ROOT']."libs/upload/documents/";

    deleteFiles('blagen',$uploaddir);  // Replace sample_files with your subform id

    function deleteFiles($form, $uploaddir) {
      $o = nuSubformObject($form);
     
      for($i = 0 ; $i < count($o->rows) ; $i++){
         
          $fileid = $o->rows[$i][2];
          $filename = $o->rows[$i][1];
          $delete = $o->rows[$i][3];
          if ($delete == "1") {
            
              nudebug(($uploaddir . $fileid. "_". $filename));
             deleteFile($uploaddir . $fileid. "_". $filename);
          }
       }

    }


    function deleteFile($file)
    {
        if (file_exists($file))
        {
            if (! unlink($file))
          {
             nuDebug("Cannot delete the file ".$file);
          }
        } else
       {
          nuDebug("File does not exist ".$file);
       }
    }
    
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: upload file to server

Unread post by kev1n »

This code will output each variable to the debug lug. Could you use it and then post the debug output here again?

Code: Select all

// nuDebug(nuSubformObject('sample_files'));

$uploaddir = $_SERVER['DOCUMENT_ROOT']."/libs/upload/documents/";

deleteFiles('sample_files',$uploaddir);

function deleteFiles($form, $uploaddir) {
  $o = nuSubformObject($form);
 
  for($i = 0 ; $i < count($o->rows) ; $i++){
	 
	  $fileid = $o->rows[$i][2];
	  $filename = $o->rows[$i][1];
	  $delete = $o->rows[$i][3];
	  
	  if ($delete == "1" && $fileid != '') {
	  
	  $debug  = "
		 rows id: $id
		 
		 fileid: $fileid
		 
		 filename: $filename
		 
		 delete: $delete
		 
		 uploaddir: $uploaddir
		 
		 ";
		 nuDebug($debug); 
		  
		 deleteFile($uploaddir . $fileid. "_". $filename);
	  }
   }
}

function deleteFile($file)
{
  
    if (file_exists($file))
    {
        if (! unlink($file)) 
		{
			nuDebug("Cannot delete the file ".$file);
		}
    } else
	{
		nuDebug("File does not exist ".$file);
	}
}
johan
Posts: 422
Joined: Sun Feb 27, 2011 11:16 am
Location: Belgium
Been thanked: 3 times

Re: upload file to server

Unread post by johan »

Kev1n
This is the output of debug
[0] :
rows id:

fileid: 1574424664_5dd7d05856d12_5da701edb0fb627

filename: tekst wonen beleidsplan.pdf

delete: 1

uploaddir: /var/www/html/libs/upload/documents/
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: upload file to server

Unread post by kev1n »

It's a little weird, everything looks fine to me. Does the deleteFile() function also output an entry "Cannot delete the file" or "File does not exist" to the debug ?
If yes, can you show me the output?
johan
Posts: 422
Joined: Sun Feb 27, 2011 11:16 am
Location: Belgium
Been thanked: 3 times

Re: upload file to server

Unread post by johan »

Kev1n
[0] : File does not exist /var/www/html/libs/upload/documents/1574424664_5dd7d05856d12_5da701edb0fb627_tekst wonen beleidsplan.pdf
Johan
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: upload file to server

Unread post by kev1n »

Can I presume that the file 1574424664_5dd7d05856d12_5da701edb0fb627_tekst wonen beleidsplan.pdf actually exists in that documents folder ?

If it exists, you need to figure out why file_exists() returns false.

Maybe this is helpful (from stackoverflow.com)

Why file_exists cannot find file?
Warning: This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir.
http://php.net/manual/en/function.file- ... turnvalues

Or this
https://stackoverflow.com/questions/921 ... n-my-linux

Or google for "/var/www/html/" file_exists PHP to find more results.
Post Reply