Page 1 of 1
Posted: Sat Sep 28, 2002 5:25 pm
by mikebr
Hi,
so how do I get round this register_globals problem with the following four lines that one of my example scripts contain:
echo "Local temp file called after upload: $_GET[uploadfile]\n";
echo "Original remote file called: $_GET[uploadfile_name]\n";
echo "Size of file in bytes: $_GET[uploadfile_size]\n";
echo "File type: $_GET[uploadfile_type]\n";
the first line returns the file name but the other three don't return any values?
Mike
Posted: Sat Sep 28, 2002 7:00 pm
by phice
I have no idea if this will work, but...
Code: Select all
<?php
echo "Local temp file called after upload: $_GETїuploadfile]\n";
echo "Original remote file called: $_GETїuploadname]\n";
echo "Size of file in bytes: $_GETїuploadsize]\n";
echo "File type: $_GETїuploadtype]\n";
?>
Posted: Sun Sep 29, 2002 2:26 am
by mikebr
PHP:
I have no idea if this will work, but...
<?php
echo "Local temp file called after upload: $_GET[uploadfile]\n";
echo "Original remote file called: $_GET[uploadname]\n";
echo "Size of file in bytes: $_GET[uploadsize]\n";
echo "File type: $_GET[uploadtype]\n";
?>
Doesn't seem to, still appears that they don't contain any value, I tried $_GET[upload]_name and $_GET[upload]name also but these just return the text after the "]"
Thanks anyway
Posted: Sun Sep 29, 2002 3:46 am
by twigletmac
You want to try echoing the $_FILES array when you're uploading something:
Code: Select all
echo '<pre>';
print_r($_FILES);
echo '</pre>';
that'll show you where everything about the file is being stored.
Mac
Posted: Sun Sep 29, 2002 6:02 am
by mikebr
OK, I'm new at this so I am not sure I understand exactly what you mean and so as it is clear as to what i am trying to do I have placed the php code from the file that includes the above "variable" lines. I am told in the book to use '$uploadfile_size' to get the file size of '$uploadfile', uploadfile is the variable returned from the html file that browses and selects the file to upload, I have changed the variable $uploadfile to $_GET[uploadfile] to cover the fact that register_globals=off per default but I can't find out what I need to do to '$uploadfile_size' or '$uploadfile_type' to get a value returned, I have tried '$_GET[uploadfile_size]' as is in the code below but his does not return a value, I also tried placing the line 'print_r <$_FILES> also tried 'print_r <$_uploadfile>;' after the 'echo "<pre>";' line in the code below but again there was no value returned. Unfortunatly I can't seem to find any reference in the two books I have on php I guess this is because they all refer to the code with register_globals=on.
I hope this makes sence!
Mike
<html>
<body>
<?php
# uploadit.php
echo "<pre>";
echo "Local temp file called after upload: $_GET[uploadfile]\n";
# File name
echo "Original remote file called: $_GET[uploadfile_name]\n";
# File size
echo "Size of file in bytes: $_GET[uploadfile_size]\n";
# File type
echo "File type: $_GET[uploadfile_type]\n"; # File type
echo "</pre>";
echo "<hr>";
# Is there a file?
if ($_GET[uploadfile] == "") {
echo "No file was uploaded<br>";
echo "Back to the upload <a href=\"upload.html\"> files</a>";
exit;
}
# Check the file size
# Is it less than 3KB
if ($_GET[uploadfile_size] < 3072) {
# cp file to new location
if (copy($_GET[uploadfile],"/Users/user/Sites/tmp/$_GET[uploadfile_name]"))
{
echo "File uploaded OK ";
# delete temp file
unlink($_GET[uploadfile]);
} else {
echo "File Upload Failed";
}
} else {
echo "Sorry files need to be less than 3KB for uploading<br>";
}
?>
Back to the form upload
<a href="upload.html">files</a>
</body>
</html>
Posted: Sun Sep 29, 2002 8:57 am
by twigletmac
OK, with register globals off the information about files uploaded using the POST method are stored in the
$_FILES array. You should be using the POST method and not the GET method when uploading files. The $_GET array is used for accessing variables that are in the query string (the stuff after a ? in a url) so that's why you can't access information about your uploaded file from there.
I cribbed this from the user notes on the
predefined variables page:
The $_FILES variable is not very well documented. Beware that it contains an associative array:
array(1) {
["File"]=>
array(5) {
["name"]=>
string(12) "brahhh.jpg"
["type"]=>
string(11) "image/pjpeg"
["tmp_name"]=>
string(14) "/tmp/php2Uyz0c"
["error"]=>
int(0)
["size"]=>
int(36950)
}
}
The bit of code I gave in my original post was intended to be put on the first should have given you the same information as shown above (would obviously differ on files names and whatnot) if put at the top of the page that is in the action attribute of your form tag:
Code: Select all
<form action="thispageprocessestheform.php" ... >
The echo '<pre>' stuff only helps the formatting of the output and does nothing more than that. It's the
print_r() bit that is important as it should echo out everything that is contained in that array.
In the associative array that is mentioned in the quote above the ['File'] element will be equal to whatever you have called the input box that is on the form for uploading the file so if you had:
Code: Select all
<input type="file" name="myfile" />
then you would get the temporary name of that file by doing:
Code: Select all
$temp_name = $_FILESї'myfile']ї'tmp_name'];
Mac
Posted: Sun Sep 29, 2002 12:24 pm
by mikebr
OK, I think I understand this now, thank you for your patience.
I can now get the values returned that I was looking for as can be seen below but unfortunatly this brings me to another problem in the script, the file is not getting uploaded "see Warning:error below",
Local temp file called after upload: /var/tmp/php2Ly4Wx
Original remote file called: test.rtf
Size of file in bytes: 289
File type: application/rtf
------------------------------------------------------------------------
Warning: Unable to open '' for reading: No such file or directory in /Users/user/Sites/uploadit.php on line 9
File Upload Failed
now I guess the error line 9 is:
if (copy($_POST[file_name],"/Users/user/Sites/tmp/$file_name"))
I use $_POST[file_name] here rather than $file_name because I understand that the $_POST[file_name] would be refering to the origonal file "I did try $file_name also but I get the same error" but $_POST[file_name] seems to return no value and yes I have changed the METHOD from GET to POST in the html file that uses the script, maybe it's not possible to upload on a localhost?
My revised code "as I understand $_FILES" for the php file is below.
Mike
-------------PHP FILE CODE
<html>
<body>
<?php
# uploadit.php
echo "<pre>";
# Used to check return values
# print_r($_FILES);
$file_name = $_FILES[uploadfile][name];
$file_temp_name = $_FILES[uploadfile][tmp_name];
$file_size = $_FILES[uploadfile][size];
$file_type = $_FILES[uploadfile][type];
echo "Local temp file called after upload: $file_temp_name\n";
echo "Original remote file called: $file_name\n";
echo "Size of file in bytes: $file_size\n";
echo "File type: $file_type\n";
echo "</pre>";
echo "<hr>";
# Do we have a file?
if ($file_name == "") {
echo "No file was uploaded<br>";
echo "Back to the upload <a href=\"upload.html\"> files</a>";
exit;
}
# Check the file size
# Is it less than 3KB
if ($file_size < 3072) {
# cp file to new location
if (copy($_POST[file_name],"/Users/user/Sites/tmp/$file_name"))
{
echo "File uploaded OK ";
# delete temp file
unlink($file_name);
} else {
echo "File Upload Failed";
}
} else {
echo "Sorry files need to be less than 3KB for uploading<br>";
}
?>
<br>
Back to the form upload
<a href="upload.html">files</a>
</body>
</html>
Back to the form upload files
Posted: Mon Sep 30, 2002 10:11 am
by mikebr
To prevent cross posting I am continuing this post at
viewtopic.php?t=3267 as this has taken me to a problem I have posted previously for Mac.