Page 1 of 1

Passing variables from PHP to CGI

Posted: Wed Jun 21, 2006 9:40 pm
by tristanlee85
Maybe it's far-fetch, but I'll try it anyway. I know how to do it with all PHP, but no idea how to with PHP and CGI.

Basically, I have this PHP upload script I created. The user logs in and username/password is verified from a phpBB database. Once the user logs in, there's an IF statement seeing if a directory of the same name as the username exists. If it doesn't exist, it creates the directory. In other words, if it's the user's first time logging in, the following directory will be created:

http://mydomain/upload/$username with $username obviously being the real username. Well, now that I'm using CGI, the upload directory is already pre-set in the CGI script in my CGI-BIN. Here is the partial script:

Code: Select all

package XUploadConfig;

BEGIN
{
  use Exporter;
  @XUploadConfig::ISA = qw( Exporter );
  @XUploadConfig::EXPORT = qw( $c );
}

our $c=
{
 # Directory for temporary using files
 temp_dir        => '/upload/temp',

 # Directory for uploaded files
 target_dir      => '/upload/uploads',
As you can see, the upload directory is at the very bottom. I would like to do something as such:

package XUploadConfig;

Code: Select all

BEGIN
{
  use Exporter;
  @XUploadConfig::ISA = qw( Exporter );
  @XUploadConfig::EXPORT = qw( $c );
}

our $c=
{
 # Directory for temporary using files
 temp_dir        => '/upload/temp',

 # Directory for uploaded files
 target_dir      => '/upload/$username',
Is this possible?

Posted: Fri Jun 23, 2006 9:11 am
by bmcewan
What is the final result that you want to acheive?

What does your CGI script do, maybe the same thing can be scheived in PHP?

Posted: Tue Jun 27, 2006 3:07 am
by tristanlee85
It's an upload script. Basically, the upload directory is set by default within the CGI file. Here is the partial code:

Code: Select all

# PREFs Section 02: Paths and Directories.
############################################################################
# This is where the uploaded files will go.  It must be world-readable
# and world-writable, aka "chmod a+rwx" or "chmod 0777".
#
# On most servers this will be in the DOCROOT, in which case we'll prepend
# your DOCROOT to whatever you set uploaded_files_dir to.  If for some
# reason you want to specify it with an absolute path, or a path that's
# relative to the current directory instead of the DOCROOT, then set the 
# _is_in_docroot PREF to no.
#
$PREF{uploaded_files_dir}				= '/upload/files';
$PREF{uploaded_files_dir_is_in_docroot}			= 'yes';
All uploads go directly into /upload/files/. Since I have a login system, I would like "files" to be replaced with the username thats stored in a session. When I wrote the upload script in PHP, it was as simple as:

Code: Select all

<?php
include('cookie.php');
$upload_dir = "/upload/$username/";
?>
bit since I'm using CGI, is it possible to have something like this;

Code: Select all

$PREF{uploaded_files_dir}				= '/upload/$username';
$PREF{uploaded_files_dir_is_in_docroot}			= 'yes';
If so, how do I pass that $username variable through the CGI script?