How upload file in Package?



  • in my package i want upload file from html form to pfSense
    after user Submit action i looking this file

    if ($_POST['submit'] == "Upload") {
        if ($_POST['blacklist_file'] != "") {
            $conf .= 'File ' . $_POST['blacklist_file'];
            if (is_uploaded_file($_FILES['blacklist_file']['tmp_name']))
    $conf .= 'uploaded';
            else $conf .= 'not uploaded';

    in result log i view 'not uploaded'  ???
    upload test in any file (different sizes and ext's)
    why in $_POST filename present but $_FILES is empty?



  • Have a look at system_firmware.php which utilizes the HTTP POST file upload feature.

    It is in /usr/local/www/system_firmware.php or http://pfsense.com/cgi-bin/cvsweb.cgi/pfSense/usr/local/www/system_firmware.php?rev=1.92;content-type=text%2Fplain



  • OK
    I found in than problem..

    This code worked in test.php file (/usr/local/www)

    print '
    
    <form method="post" action="test.php">';
    print '';
    print '';
    print '</form>
    
    ';
    $uploaddir = '/tmp/';
    //print_info_box("<<<");
    
    if (is_uploaded_file($_FILES['upload']['tmp_name']))
    {
    	if (move_uploaded_file($_FILES['upload']['tmp_name'], $uploaddir . $_FILES['upload']['name']))
      		echo "Success";
    	else
      		echo "Error copy";
    }
    else
    	echo "Error upload";
    ?>
    

    This generated HTML. Looking field action

    
    Success
    

    In package this field set as  pkg_edit.php

    
    <form name="iform" action="pkg_edit.php" method="post">
    
    How i can send my function $_FILES values from pkg_edit.php? ($_POST sending normally)
    In XML package file? What this section?
    Now this in XML
    
     <custom_php_resync_config_command>squidguard_resync();</custom_php_resync_config_command> 
    
    
    


  • Can you share your source so far and I will take a look?



  • This test package page for upload
    Put this to /usr/local/pkg
    Test address  http://my_pf_router/pkg_edit.php?xml=upload.xml
    Log file in /var/tmp/upload.log and it's can be view in upload test page

    XML file 'upload.xml'

    
     <packagegui><include_file>upload.inc</include_file>
    	<name>UploadTest</name>
    	<title>Upload test</title>
    	 <tabs><tab><text>General settings</text>
    			<url>/pkg_edit.php?xml=upload.xml</url>
    			 <active></active></tab></tabs> 
    	 <fields><field><fieldname>upload</fieldname>
    			<fielddescr>Test</fielddescr>
    			<type>checkbox</type>
    			<description>This control unused</description></field></fields> 
    	 <custom_php_validation_command></custom_php_validation_command> 
    	 <custom_php_command_before_form>before_form(&$pkg);</custom_php_command_before_form> 
    
    	 <custom_php_after_form_command></custom_php_after_form_command> 
    	 <custom_add_php_command></custom_add_php_command> 
    
    	 <custom_php_resync_config_command>resync();</custom_php_resync_config_command></packagegui> 
    
    

    inc file upload.inc

    
    # ---------------------------------------------------------------------------------------------------------------------
    require_once('globals.inc');
    require_once('config.inc');
    require_once('util.inc');
    require_once('pfsense-utils.inc');
    require_once('pkg-utils.inc');
    require_once('filter.inc');
    require_once('service-utils.inc');
    
    # ---------------------------------------------------------------------------------------------------------------------
    define('SQUIDGUARD_CONFBASE', '/usr/local/etc/squid');
    define('SQUIDGUARD_CONFFILE', '/squidguard.conf.test');
    define('SQUIDGUARD_BINPATH',  '/usr/local/bin');
    define('SQUIDGUARD_LOGDIR',   '/var/squidGuard'); // проверить путь
    define('SQUIDGUARD_DBHOME',   '/var/db/squidGuard');
    define('TEMP_DIR',            '/var/tmp');
    
    # ---------------------------------------------------------------------------------------------------------------------
    
    define('PREFLD_UPTIME',               'uptime_');
    define('PREFLD_UPTIME_DENY',   'uptimedeny_');
    define('PREFLD_OVERTIME',      'overtime_');
    define('PREFLD_OVERTIME_DENY', 'overtimedeny_');
    
    define('MODULE_GENERAL',         'squidguardgeneral');
    define('MODULE_ACL',             'squidguardacl');
    define('MODULE_DESTINATION',     'squidguarddest');
    define('MODULE_REWRITE',         'squidguardrewrite');
    define('MODULE_SOURCE',          'squidguardsrc');
    define('MODULE_TIME',            'squidguardtime');
    
    define('BLACKLIST_DEFAULT_URL', 'http://ftp.teledanmark.no/pub/www/proxy/squidGuard/contrib/blacklists.tar.gz');
    define('BLACKLIST_TMP_FILE',    '/var/tmp/blacklists.tar.gz');
    define('BLACKLIST_BTN_STD',     'Upload Std');
    define('BLACKLIST_BTN_URL',     'Upload Url');
    define('BLACKLIST_BTN_FILE',    'Upload File');
    define('UPLOAD_LOGFILE',     'upload.log');
    
    # ---------------------------------------------------------------------------------------------------------------------
    # Upload file to /var/tmp directory
    # ---------------------------------------------------------------------------------------------------------------------
    function upload_file($file) {
            $res = '';
            $cat = '/var/tmp/';
            if (isset($_FILES[$file])) {        // if file exists
                    if (move_uploaded_file($_FILES[$file]['tmp_name'], $cat . $_FILES[$file]['name'])) {
                            $res = true;
                    }
            }
            return $res;
    }
    
    // ---------------------------------------------------------------------------------------------------------------------
    // Resync & Make
    // ---------------------------------------------------------------------------------------------------------------------
    
    function resync() {
            log_set('Begin resync. ', true);
            $s = '';
    		$s .= " POST_count_vlues=" . count($_POST) . ' ';
    		$s .= " FILES_count_values=" . count($_FILES) . ' ';
    
            $upload_file = '';
            if ($_POST['submit'] == BLACKLIST_BTN_FILE) {
                $upload_file = $_POST['upload_file'];
                $s .= 'Begin upload from file ' . $upload_file . '. ';
    
            	if (is_uploaded_file($_FILES['upload_file']['tmp_name']))
                 	 $s .= ' - Uploaded ' . $_FILES['upload_file']['tmp_name'];
            	else $s .= ' - Not uploaded '; //. $_FILES['userfile']['name'];
            }
            log_set($s);
    }
    
    // -----------------------------------------------------------------------------
    // Before form
    // -----------------------------------------------------------------------------
    
    function before_form($pkg) {
            $i=0;
            foreach($pkg['fields']['field'] as $field) {
                    if (($field['fieldname'] == 'upload')) {
                            $fld = &$pkg['fields']['field'][$i];
                            $fld['description'] .= make_grid();        // вместо description вставляем свои контролы
                    }
                    $i++;
            }
    }
    
    function make_grid() {
            global $squid_guard_info;
            $style1 = 'class=vtable'; // 'class=vncell' ;// 'style="background-color: #FFF0E2;"';
            $style2 = 'class=vtable';
            $e_size = '80';
    
            $res = '';
            $res .= "";
            $res .= "";
    
            // print log info
            $info = log_get();
            if ($info != '')
                $res .= "
                         ";
            $res .= "
    
    | Load from **File** |  |  |
    | Upload info | $info |
    
    ";
    
            // clear log info
            log_set('', true);
            return $res;
    }
    
    // -----------------------------------------------------------------------------
    // blacklist log
    // -----------------------------------------------------------------------------
    function log_set($str, $new = false)
    {
            $content = '';
            $log_file = TEMP_DIR . '/' . UPLOAD_LOGFILE;
            if (file_exists($log_file))
                $content = file_get_contents($log_file);
            if ($new)
                 $content = $str;
            else $content .= $str;
            file_put_contents($log_file, $content);
    }
    
    function log_get()
    {
            $content = '';
            $log_file = TEMP_DIR . '/' . UPLOAD_LOGFILE;
            if (file_exists($log_file))
                $content = file_get_contents($log_file);
            return $content;
    }
    
    ?>
    
    


  • @dvserg:

    In package this field set as  pkg_edit.php

    There are two issues I see here:

    a) our RELENG_1 pkg_edit.php generates this form tag:

    The form tag you did paste above isn't valid XHTML.

    b) I think the super global $_FILES isn't initialized, because the pkg_edit.php script generates a standard form tag. Tho, to upload files you'll need a form tag including the attribute:

    
    enctype="multipart/form-data"
    
    

    This is important!

    Regards
    Daniel S. Haischt



  • OK In near time i testing it



  • File uploadconfig.php
    How this worked without ' enctype="multipart/form-data '?

    –>header("Content-Type: text/plain");

    require("guiconfig.inc");
    
    header("Content-Type: text/plain");
    
    /* get config.xml in POST variable "config" */
    if ($_POST['config']) {
    	$fd = @fopen("{$g['tmp_path']}/config.xml", "w");
    	if (!$fd) {
    		echo "ERR Could not save configuration.\n";
    		exit(0);
    	}
    	fwrite($fd, $_POST['config']);
    	fclose($fd);
    	if (config_install("{$g['tmp_path']}/config.xml") == 0) {
    		echo "OK\n";
    		system_reboot();
    	} else {
    		echo "ERR Could not install configuration.\n";
    	}
    } else {
    	echo "ERR Invalid configuration received.\n";
    }
    
    exit(0);
    
    


  • @dvserg:

    OK In near time i testing it

    –----------------------------------------------------
    This HTML generated pfSense, enctype="multipart/form-data" not present

    I am pretty sure that this isn't source code generated by pfSense. Just have a look at the above



  • @dvserg:

    File uploadconfig.php
    How this worked without ' enctype="multipart/form-data '?

    Please stop posting file snippets and please stop to argue against the usage of multipart/form-data encoded form to be used to upload files. Have a look at diag_backup.php, it is used to restore config.xml and as you may figure, it is using a multipart/form-data encoded form as well.

    Regards
    Daniel S. Haischt



  • @DanielSHaischt:

    I am pretty sure that this isn't source code generated by pfSense. Just have a look at the above



  • html is older then xhtml
    using html and xhtml on 1 page can comfuse a browser



  • @dvserg:

    It's real html code. I store this from IE File->SaveAs and change only IP address my pfRouter to NICKname.
    I can give you any HTML code by any page from pfsense with UPcased tags along 1 minute time  ;).

    There you go - You should not save the page to a file to get the HTML source. Almost any browser modifies the original HTML code so the page still can be viewed locally. That means the HTML code that you did save to a file isn't equal to the HTML code displayed within your browser.

    To get the unmodified HTML source you should always use the view source option from the context menu of your browser. This context menu can be unveiled if right-clicking onto a HTML page that is currently beeing displayed in your browser window.

    Regards
    Daniel S. Haischt


Log in to reply