473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

can you set FILES[] variables to session variables?

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
7 2015
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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Josef Blösl | last post by:
hi, how must i declare a variable in php to get access from another php file who is later called?
4
by: PJ | last post by:
A particular page seems to be having issues with correctly setting Session variables. I am setting a couple of session variables on the Page_Unload event. While stepping through code, the...
31
by: Harry Simpson | last post by:
I've come from the old ASP camp where session variables were not used. When i started using ASP.NET in 2001, I started using them again because it was ok from what I'd read. I've been merrily...
7
by: jsale | last post by:
I have made an ASP.NET web application that connects to SQL Server, reading and writing data using classes. I was recommended to use session objects to store the data per user, because each user...
3
by: Phillip N Rounds | last post by:
I'm writing a user control which has two states: Active & InActive. I additionally am required that there to be only one active control per page, and all logic has to be contained within the...
18
by: BillE | last post by:
When a user opens a new IE browser window using File-New-Window the integrity of an application which relies on session state is COMPLETELY undermined. Anyone who overlooks the fact that...
26
by: BillE | last post by:
Some ASP.NET applications use Session Variables extensively to maintain state. These should be re-written to use viewstate, hidden fields, querystring, etc. instead. This is because if a user...
1
by: fatjoez | last post by:
Hey there. I've been trying to modify my file upload script so that it handles 10 files instead of one. i was thinking the most straightforward way would be to add a FOR LOOP? placed...
1
by: Kesavan | last post by:
I install apache2 in /usr/local/apache2 and install php5 by ./configure --with-apxs2=/usr/local/apache2/bin/ apxs PHP is successfully installed in my system. But now my .php files inside...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.