Welcome to the nuBuilder Forums!
Join our community by registering and logging in.
As a member, you'll get access to exclusive forums, resources, and content available only to registered users.
Join our community by registering and logging in.
As a member, you'll get access to exclusive forums, resources, and content available only to registered users.
upload file to server
-
- nuBuilder Team
- Posts: 4562
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 76 times
- Been thanked: 528 times
- Contact:
Re: upload file to server
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?
-
- nuBuilder Team
- Posts: 4562
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 76 times
- Been thanked: 528 times
- Contact:
Re: upload file to server
You can add this code in PHP AS (After Save) event:johan wrote: Just when I delete a subrow containing a file.
Than the row is deleted in de sql table.
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];
Code: Select all
nuDebug(nuSubformObject('sample_files'));
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
)
Re: upload file to server
Kev1n
NuDebug returns
$
I've added nudebug(($uploaddir . $fileid. "_". $filename));
This returns
NuDebug returns
$
So I've adjusted[0] => -1
[1] => Werken_aan_een_betere_aanpak_van_schulden_SAMvzw_15_april_2019_1_0.pdf
[2] => 1574364113_5dd6e3d1b3004_5da701edb0fb627
[3] => 0
But when I save my form, the file is not deleted.$fileid = $o->rows[$i][2];
$filename = $o->rows[$i][1];
$delete = $o->rows[$i][3];
I've added nudebug(($uploaddir . $fileid. "_". $filename));
This returns
Johan[0] : /var/www/html/documents/_[0] : /var/www/htmllibs/upload/documents/_
-
- nuBuilder Team
- Posts: 4562
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 76 times
- Been thanked: 528 times
- Contact:
Re: upload file to server
Could you show the full output of nudebug? And also the PHP code if you have changed anything.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
Re: upload file to server
Kev1n
Here's the full debug
Here's the full debug
and the php[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
)
)
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);
}
}
-
- nuBuilder Team
- Posts: 4562
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 76 times
- Been thanked: 528 times
- Contact:
Re: upload file to server
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);
}
}
Re: upload file to server
Kev1n
This is the output of debug
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/
-
- nuBuilder Team
- Posts: 4562
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 76 times
- Been thanked: 528 times
- Contact:
Re: upload file to server
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?
If yes, can you show me the output?
Re: upload file to server
Kev1n
Johan[0] : File does not exist /var/www/html/libs/upload/documents/1574424664_5dd7d05856d12_5da701edb0fb627_tekst wonen beleidsplan.pdf
-
- nuBuilder Team
- Posts: 4562
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 76 times
- Been thanked: 528 times
- Contact:
Re: upload file to server
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?
Or this
https://stackoverflow.com/questions/921 ... n-my-linux
Or google for "/var/www/html/" file_exists PHP to find more results.
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?
http://php.net/manual/en/function.file- ... turnvaluesWarning: 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.
Or this
https://stackoverflow.com/questions/921 ... n-my-linux
Or google for "/var/www/html/" file_exists PHP to find more results.