Database: upload file function

This commit is contained in:
sparkyx 2025-10-01 11:25:13 +02:00
parent 4c5fa7ebea
commit d2a199ffce
3 changed files with 53 additions and 9 deletions

View file

@ -82,7 +82,7 @@ class Database extends DatabaseCore
. "]"; . "]";
} }
/*** /***
* \brief Save a "piece justificative" , the name must be pj * \brief Save a "piece justificative" , the name must be a receipt
* *
* \param $seq jr_grpt_id * \param $seq jr_grpt_id
* \return $oid of the lob file if success * \return $oid of the lob file if success
@ -91,12 +91,16 @@ class Database extends DatabaseCore
*/ */
function save_receipt($seq) function save_receipt($seq)
{ {
/**
* pj is the $_FILES key
*/
$oid = $this->upload('pj'); $oid = $this->upload('pj');
if ($oid == false) { if ($oid == false) {
return false; return false;
} }
// Remove old document // Remove old document
$ret = $this->exec_sql("select jr_pj from jrn where jr_grpt_id=$seq"); $ret = $this->exec_sql("select jr_pj from jrn where jr_grpt_id=$1"
,[$seq]);
if (pg_num_rows($ret) != 0) { if (pg_num_rows($ret) != 0) {
$r = pg_fetch_array($ret, 0); $r = pg_fetch_array($ret, 0);
$old_oid = $r['jr_pj']; $old_oid = $r['jr_pj'];
@ -107,6 +111,12 @@ class Database extends DatabaseCore
$this->exec_sql("update jrn set jr_pj=$1 , jr_pj_name=$2, $this->exec_sql("update jrn set jr_pj=$1 , jr_pj_name=$2,
jr_pj_type=$3 where jr_grpt_id=$4", jr_pj_type=$3 where jr_grpt_id=$4",
array($oid, $_FILES['pj']['name'], $_FILES['pj']['type'], $seq)); array($oid, $_FILES['pj']['name'], $_FILES['pj']['type'], $seq));
if ( $_FILES['pj']['type'] == 'text/xml'
|| $_FILES['pj']['type'] == 'application/xml'
)
{
}
return $oid; return $oid;
} }

View file

@ -718,7 +718,7 @@ class DatabaseCore
} }
/*** /***
* \brief Save a document into the database , it just puts the file in the database * \brief Save one or several documents into the database , it just puts the file in the database
* and returns the corresponding OID , the mimetype , size ... of the document * and returns the corresponding OID , the mimetype , size ... of the document
* must be set in the calling function. * must be set in the calling function.
* *
@ -730,15 +730,25 @@ class DatabaseCore
function upload($p_name) function upload($p_name)
{ {
//var $a : 0 we're in a transaction, 1 we are not in a transaction
$a=0;
if ( $this->status() !== PGSQL_TRANSACTION_INTRANS ) {
$a=1;
$this->start();
}
/* there is no file to upload */ /* there is no file to upload */
if ($_FILES[$p_name]["error"] == UPLOAD_ERR_NO_FILE) { if ($_FILES[$p_name]["error"] == UPLOAD_ERR_NO_FILE) {
\record_log("DC759: error upload file".var_export($_FILES, true));
if ( $a==1) { $this->rollback(); }
return false; return false;
} }
$new_name = tempnam($_ENV['TMP'], $p_name); $new_name = tempnam($_ENV['TMP'], $p_name);
if ($_FILES[$p_name]["error"] > 0) { if ($_FILES[$p_name]["error"] > 0) {
print_r($_FILES); \record_log("DC740: error upload file".var_export($_FILES, true));
echo_error(__FILE__ . ":" . __LINE__ . "Error: " . $_FILES[$p_name]["error"]); if ( $a==1) { $this->rollback(); }
return false; return false;
} }
if (strlen($_FILES[$p_name]['tmp_name']) != 0) { if (strlen($_FILES[$p_name]['tmp_name']) != 0) {
@ -746,17 +756,20 @@ class DatabaseCore
// echo "Image saved"; // echo "Image saved";
$oid = pg_lo_import($this->db, $new_name); $oid = pg_lo_import($this->db, $new_name);
if ($oid == false) { if ($oid == false) {
echo_error(__FILE__, __LINE__, "cannot upload document"); \record_log("DC747: error upload file".var_export($_FILES, true). "SQL MESSAGE". pg_last_error($this->db));
$this->rollback(); $this->rollback();
return false; return false;
} }
return $oid; return $oid;
} else { } else {
echo "<H1>Error</H1>"; \record_log("DC754: move_uploaded fails".var_export($_FILES, true));
$this->rollback(); $this->rollback();
return false; return false;
} }
} }
\record_log("DC576: Files error names empty".var_export($_FILES, true));
if ( $a==1) { $this->commit(); }
return false; return false;
} }
/** /**

View file

@ -34,13 +34,34 @@ $cn=Dossier::connect();
echo h2(" Status IDLE"); echo h2(" Status IDLE");
echo $cn->status(); echo $cn->status();
echo h2(" Status in begin "); echo h2(" Status start "); //echo PGSQL_TRANSACTION_INTRANS;
echo $cn->start(); echo $cn->start();
echo $cn->status (); echo $cn->status ();
echo "<br>"; echo "<br>";
$cn->get_value("select count(*) from jrnx"); $cn->get_value("select count(*) from jrnx");
echo "<br>";
echo $cn->status (); echo $cn->status ();
echo PGSQL_TRANSACTION_INTRANS;
echo "<br>";
echo h2(" Status after rollback "); echo h2(" Status after rollback ");
echo $cn->commit(); echo $cn->commit();
echo "<br>";
echo $cn->status (); echo $cn->status ();
echo h1("Upload");
echo h2("One file");
if ( isset($_POST['submit_one'])) {
var_dump($_FILES);
// $cn->start();
if ( $cn->upload("validate_one") != false){
echo "success : loaded ".var_export($_FILES['validate_one'],true);
}
// $cn->commit();
}else{
echo <<<FORM
<FORM METHOD="POST" enctype="multipart/form-data">
<input type="FILE" name="validate_one">
<input type="SUBMIT" name="submit_one">
</FORM>
FORM;
}