?
Solved

Best way to pass variables to a php page

Posted on 2009-04-09
7
Medium Priority
?
437 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:gevensen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 10

Expert Comment

by:webwyzsystems
ID: 24108650
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
0
 
LVL 5

Expert Comment

by:john_hollings
ID: 24108726
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
0
 

Author Comment

by:gevensen
ID: 24108821
im passing file desc and filename for uploading blobs to a mysql database
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gevensen
ID: 24109000
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?
0
 
LVL 10

Accepted Solution

by:
webwyzsystems earned 2000 total points
ID: 24109145
It cannot pass the entire path because the file is stored in a temporary directory that is destroyed at the end of the request. You have to expressly move the uploaded file out of the temp directory and into it's permanent resting place - or a place where you can work with it further.
Also - you won't be able to pass any decent amount of data in the URL string because it's size is limited to 2048 chars in IE. I believe that DOM states max is supposed to be 8096 or something like that....but IE truncates at 2048.
IF the user is coming in via proxy - then the URL string is limited to 255 chars, which is REALLY tiny for transferring data.
0
 

Author Comment

by:gevensen
ID: 24110556
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

0
 

Author Closing Comment

by:gevensen
ID: 31568548
thanks for the help
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question