Link to home
Start Free TrialLog in
Avatar of gevensen
gevensen

asked on

Best way to pass variables to a php page

what would be the best way to pass the values for strDesc and  fileUpload to a php page called grabfile.php

<input type="text" name="strDesc" size="20" maxlength="50">

<input type="file" name="fileUpload" size="20">


html>
 
<head>
 
<title> Upload a File </title>
 
</head>
 
<body bgcolor="#FFFFFF">
 
<form enctype="multipart/form-data" name="frmUploadFile" action="grabfile.php?var1='strDesc'&var2='fileUpload'" method="post">
 
<a href="http://www.devarticles.com">
 
<img border="0" src="http://www.devarticles.com/dlogo.gif">
 
</a>
 
<table border="0" cellpadding="0" cellspacing="0" bordercolor="#111111" width="100%">
 
<tr>
 
<td width="100%" bgcolor="#FF9900" height="22" colspan="2">
 
<p style="margin-left: 10"><b><font face="Verdana" size="2" color="#FFFFFF">
 
Upload a File</font></b></td>
 
</tr>
 
<tr>
 
<td width="100%" bgcolor="#FFE3BB" colspan="2">
 
<p style="margin-left: 10; margin-right: 10"><font face="Verdana" size="2">
 
<br>Please select a file from your local computer to upload to our web server
 
for saving in our database. This file can be of any type you like. Once you
 
have chosen a file, please click on the &quot;Upload this file&quot; button below.&nbsp;
 
&nbsp;<br>&nbsp;</font></td>
 
</tr>
 
<tr>
 
<td width="15%" bgcolor="#FFE3BB">
 
<p style="margin-left: 10"><font face="Verdana" size="2">
 
File Description:</font></td>
 
<td width="85%" bgcolor="#FFE3BB">
 
<input type="text" name="strDesc" size="20" maxlength="50"></td>
 
</tr>
 
<tr>
 
<td width="15%" bgcolor="#FFE3BB">
 
<p style="margin-left: 10"><font face="Verdana" size="2">File Location:</font></td>
 
<td width="85%" bgcolor="#FFE3BB">
 
<font face="Verdana" size="2">
 
<input type="file" name="fileUpload" size="20"></font></td>
 
</tr>
 
<tr>
 
<td width="33%" bgcolor="#FFE3BB">
 
<p style="margin-left: 10"><font face="Verdana" size="2">
 
<br>
 
<br>
 
&nbsp;</font></td>
 
<td width="67%" bgcolor="#FFE3BB">
 
<font face="Verdana" size="2">
 
<input type="submit" value="Upload this file" name="cmdSubmit"></font></td>
 
</tr>
 
</table>
 
</form>
 
</body>
 
</html>

Open in new window

Avatar of webwyzsystems
webwyzsystems

Just use a hidden field for strDesc, except define the value:
<input type="hidden" name="var1" size="20" maxlength="50" value="strDesc">
In your post method file, you will catch the variables with $_POST array and $_FILES should contain your file in a temp directory.
Your syntax in the <FORM> tag mixes submit methods, you call for POST as method, but you are forcing a GET method in the action portion of the FORM.

For more info on uploading files, visit:
http://www.w3schools.com/PHP/php_file_upload.asp
I would say it depends on the data and how sensitive it is and where it is coming from.  You can pass the variables in a URL string e.g. http://mysite.co.uk?strDesc=value&fileUpload=value.

But another way is do this is to include the variables in a session $_SESSION['strDesc'] = xxx;

You can also pass them using cookies or forms.  I suggest looking at the following link as it explains it better than I would:

http://www.plus2net.com/php_tutorial/variables.php
Avatar of gevensen

ASKER

im passing file desc and filename for uploading blobs to a mysql database
i passed it using session start but it doesnt pass the whole path of the image location

for example it passes the filename file.jpg but not the entire path?
ASKER CERTIFIED SOLUTION
Avatar of webwyzsystems
webwyzsystems

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i ended up putting the upload on the same php page and simply not passing the variables at all
i was able to upload the jpg and retrieve it, it must pass the temp file variables without you being able to see it if its on the same page
i posted the code if anyone has a future question and wants to blob
// mysql query for creating the table
 
create table myBlobs
 
(
 
blobId int auto_increment not null,
 
blobTitle varchar(50),
 
blobData longblob,
 
blobType varchar(50),
 
primary key(blobId),
 
unique id(blobId)
 
);
 
 
 
// php code for uploading
 
<?php
include 'include/config.php';  // your mysql data
if (isset($_POST['submit'])) 
{ // if form has been submitted 3333
$recnumb=$_POST['recnum'];
$fileUpload=$_POST['fileUpload'];
$fileUpload_size=filesize($_POST['fileUpload']);
$fileUpload_type="image/jpg";
 
if(empty($_POST['recnum']) || $_POST['fileUpload'] == "none")
 
die("You must enter both a description and file");
 
// PHP has several built-in functions that allow us to open and read files. We use the fopen and fread methods to open the uploaded file from the local directory on the web server, and then read its contents into a variable. The addslashes method escapes any apostrophises and double quotes in the file:
 
$fileHandle = fopen($fileUpload, "r");
if($fileHandle==FALSE)
			{ die ("Fopen Error Dying Now"); }
 
$fileContent = fread($fileHandle, $fileUpload_size);
if($fileContent==FALSE)
			{ die ("Fread Error Dying Now"); }
 
$fileContent = addslashes($fileContent);
 
// We connect to our database using PHP's built-in MySQL functions in combination with our database connection variables that we defined above:
 
$sConn = mysql_connect($dbServer, $dbUser, $dbPass)
 
or die("Couldn't connect to database server");
 
$dConn = mysql_select_db($dbDatabase, $sConn)
 
or die("Couldn't connect to database $dbDatabase");
 
// Once connected to the MySQL database, we run an insert query to actually add the details of our uploaded file (as a blob) to the myFiles table:
 
$dbQuery = "INSERT INTO myBlobs VALUES ";
 
$dbQuery .= "(0, '$recnumb', '$fileContent', '$fileUpload_type')";
 
mysql_query($dbQuery) or die("Couldn't add file to database");
 
// If the mysql_query function didn't succeed, then we our script calls the die function, which stops the execution of our script and outputs "Couldn't add file to database" to the clients browser. On the other hand, if the mysql_query function succeeded, then we output the details of the uploaded file to the browser:
die("Sucess!");
//header('Location: grabfile.php');
 
} // if form has been submitted 3333
else
{
?>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<center><h1>File Upload</h1></center>
<br/>
<br/>
<center>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr><td><center>Record Number:</td><td>
<input type="text" name="recnum" maxlength="60">
</td></tr>
 
<tr><td>Password:</td><td>
<input type="file" name="fileUpload" size="150">
</td></tr>
<tr><th colspan=4><input type="submit" name="submit" value="Log In"></th></tr> </table>
</form>
</center>
 
<?php
}
?> 

Open in new window

thanks for the help