By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,468 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

can you set FILES[] variables to session variables?

P: n/a
I have some code to upload files to my site. it works when the <input
type="file" is posted once even when I use session variables from the
posted variables but when I carry those session variables to a new page the
upload will not work. despite the variables being set. I have echoed then on
the second page and they display correctly but they just dont work in the
move_uploaded_file() function. I get the error message

Error uploading image - myfile.jpg0

The 'Error uploading image - myfile.jpg' is my message but where there is
the () I am expecting the php error message to be returned from
$_SESSION['uploadedfile']['error'][$x]

Is this behaviour normal ie you cant set a session variable from posted
FILES[] varaibles, or does the problem lie with my script. Ive included it
below idf it helps

if($imagechanged){
for ($x = 0; $x < count($imagechanged); $x++){
if($_SESSION['uploadedfile']["name"][$x]!==''){
$targetdir = "/questiondbase/pupiltester/";
$allowed_file_types = "(jpg|jpeg|gif|bmp|png)";
$target_path = $targetdir .
basename($_SESSION['uploadedfile']["name"][$x]);
echo $imagechanged[$x]." -
".$_SESSION['uploadedfile']["tmp_name"][$x]." - ".$target_path;
//echo $targetdir . $_SESSION['imagepath'][$x]."<br>";
if($_SESSION['uploadedfile']["size"][$x] < 35841) {
if(preg_match("/\." . $allowed_file_types . "$/i",
$_SESSION['uploadedfile']["name"][$x])) {
unlink($targetdir . $_SESSION['imagepath'][$x]);
if(file_exists($target_path)){
$msg.="<br><span class='RedWarning'>A file by the name of
".$_FILES["uploadedfile"]["name"][$x]." already exists. Please rename it and
try again.</span>";
} else {
if(move_uploaded_file($_SESSION['uploadedfile']["tmp_name"][$x],
$target_path)){
$msg.="<br><span class='greenbody'>Uploaded
".$_SESSION['uploadedfile']["name"][$x]." ("
..$_SESSION['uploadedfile']["type"][$x]. ",
".ceil($_SESSION['uploadedfile']["size"][$x] / 1024) . " Kb).</span>";
//if($uploadedfile['name'][$x]!==''){
$sql2 = "UPDATE questions SET
ImagePath='".$_SESSION['uploadedfile']["name"][$x]."' WHERE
ImagePath='".$_SESSION['imagepath'][$x]."'";
$addquest = mysql_query($sql2) or $msg.="<br><span
class='RedWarning'>Problem adding file name
".$_SESSION['uploadedfile']['name'][$x]." to database: " .
mysql_error()."</span>";
//}
}else{
$msg.="<br><span class='Red Warning'>Error uploading image -
".$_SESSION['uploadedfile']['name'][$x].$_SESSION['uploadedfile']['error'][$
x]."</span>";
}
}

}else{
$msg.="<br><span
class='RedWarning'>".$_SESSION['uploadedfile']['name'][$x].$_SESSION['upload
edfile']['error'][$x]." not uploaded. Not a valid image file.</span>";
}
}else{
$msg.="<br><span
class='RedWarning'>".$_SESSION['uploadedfile']['name'][$x]." is greater than
35Kb.</span>";
}
}
}
echo("<br><br><div align='center'>".$msg."<br><br><a
href='usersquestions.php' class='BodyLink'>Return</a></div>");
}

Oct 17 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Rik
mantrid wrote:
I have some code to upload files to my site. it works when the <input
type="file" is posted once even when I use session variables from
the posted variables but when I carry those session variables to a
new page the upload will not work. despite the variables being set. I
have echoed then on the second page and they display correctly but
they just dont work in the move_uploaded_file() function. I get the
error message
I haven't read the code, but I suspect the problem is this:
1. The user uploads the file.
2. File arrives perfectly.
3. Session variables are set. ($_SESSION['files'] = $_FILES;)
4. File is DELETED from the temp directory because the request ends.
5. Second page comes up, with perfect session variables, but the files are
no longer there.

In this case, you'd have to move it to your own custom tmp directory first,
which you will have to clean up, because the server won't do that anymore
for you.

Another option is you use $_SESSION = $_FILES. Don't. Set it in a specific
$_SESSION key.

Grtz,
--
Rik Wasmus
Oct 17 '06 #2

P: n/a
Hmm Rik <lu************@hotmail.comwrote:
Another option is you use $_SESSION = $_FILES. Don't. Set it in a
specific $_SESSION key.
what? you will lost all data in session !!!
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

2be || !2be $this =mysql_query();
Oct 17 '06 #3

P: n/a
Rik
..:[ ikciu ]:. wrote:
Hmm Rik <lu************@hotmail.comwrote:
>Another option is you use $_SESSION = $_FILES. Don't. Set it in a
specific $_SESSION key.

what? you will lost all data in session !!!
That's exactly what I say: I don't know how he copies it in the session but
he should NOT set the session to exactly the files array, but in a specific
key (i.e. $_SESSION['files'] = $_FILES;)

With option, I don't mean a solution, but an option why it doesn't work
:-).

Grtz,
--
Rik Wasmus
Oct 17 '06 #4

P: n/a
Hmm Rik <lu************@hotmail.comwrote:
With option, I don't mean a solution, but an option why it doesn't
work :-).
it works but only for values not for binary data
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

2be || !2be $this =mysql_query();
Oct 18 '06 #5

P: n/a
Rik
..:[ ikciu ]:. wrote:
Hmm Rik <lu************@hotmail.comwrote:
>With option, I don't mean a solution, but an option why it doesn't
work :-).

it works but only for values not for binary data
???
What the hell are you implying here?

I have never known the $_FILES array to hold binary data? And who made
mention of it? Furthermore, the $_SESSION array can hold binary data
easily.

$_FILES simply holds file information, not the file itself. As I said 2
posts back, if you want to propagate the $_FILES data, and not handle the
files directly, be sure to move the files themselves to another location or
they'll be deleted by the server.

Grtz,
--
Rik Wasmus
Oct 18 '06 #6

P: n/a
Hmm Rik <lu************@hotmail.comwrote:
What the hell are you implying here?
i mean you will store only informations about file(s) no data file
if you want to keep uploaded files, you have to move them to your private
temp directory
I have never known the $_FILES array to hold binary data? And who made
mention of it? Furthermore, the $_SESSION array can hold binary data
easily.
ofc

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

2be || !2be $this =mysql_query();
Oct 18 '06 #7

P: n/a
"Rik" <lu************@hotmail.comwrote in message
news:7d**************************@news1.tudelft.nl ...
mantrid wrote:
I have some code to upload files to my site. it works when the <input
type="file" is posted once even when I use session variables from
the posted variables but when I carry those session variables to a
new page the upload will not work. despite the variables being set. I
have echoed then on the second page and they display correctly but
they just dont work in the move_uploaded_file() function. I get the
error message

I haven't read the code, but I suspect the problem is this:
1. The user uploads the file.
2. File arrives perfectly.
3. Session variables are set. ($_SESSION['files'] = $_FILES;)
4. File is DELETED from the temp directory because the request ends.
5. Second page comes up, with perfect session variables, but the files are
no longer there.

In this case, you'd have to move it to your own custom tmp directory
first,
which you will have to clean up, because the server won't do that anymore
for you.

Another option is you use $_SESSION = $_FILES. Don't. Set it in a specific
$_SESSION key.

Grtz,
--
Rik Wasmus

Yes that makes sense
That is probably what is happening as the code works fine otherwise, even
using sessions. However stops working if it is propergated onwards to more
than one page. yes it looks like i will have to use an alternate directory
for the temp file.
Thanks
Oct 19 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.