Small PHP uploading script with an error that I can't figure

Hey! It aint that hard!
Post Reply
Gerry
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 5727
Joined: 12/04/01, 12:00 am
Location: Perth, Western Australia
Contact:

Post by Gerry » 11/26/02, 5:11 pm

OK so I'm trying to get this script to upload to a 777 permission folder on a test Apache server that I have installed on Win98. Everything works in the script except for the actual uploading and it displays "Your File was uploaded successfully" but when I check for the file it's not there. Can anybody see what I'm doing wrong?

I coded this myself so the problem is most likely in the code.

<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class=postbody>Code:</font><HR width=100% color=#333333 SIZE=1></TD></TR><TR><TD><FONT class=code><PRE><?php

if($_FILES['userfile']['tmp_name'])
{
echo 'File name: '.$_FILES['userfile']['name'];
echo '<br />Temp name: '.$_FILES['userfile']['tmp_name'];
echo '<br />File size: '.$_FILES['userfile']['size'];

if(copy($_FILES['userfile']['tmp_name'], '/uploaded/'.$_FILES['userfile']['name']))
{
echo '<br />Your File was uploaded successfully';
}
}

?></PRE></FONT></TD></TR><TR><TD><HR width=100% color=#333333 SIZE=1></TD></TR></TABLE><!-- BBCode End -->

ps. $HTTP_POST_FILES and $userfile_name etc. are not options, I'm trying to make a script with the new PHP 4.02+ more secure variables (ie. $_FILES[])
pps. I have tried move_uploaded_file() instead of copy() and it didn't work either, but I knew it wouldn't as PHP prints out an error message if that's the problem (which it didn't).

_________________

<font class=editedby>[ This Message was edited by: Gerry on 2002-11-27 01:15 ]</font>

Gerry
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 5727
Joined: 12/04/01, 12:00 am
Location: Perth, Western Australia
Contact:

Post by Gerry » 11/26/02, 5:51 pm

*Gerry bitch slaps himself*

copy($_FILES['userfile']['tmp_name'], '<font color="#ff0000">/</font>uploaded/'.$_FILES['userfile']['name'])

a single foward slash, a single dam slash caused me two days of torment!!! AGGGGGGGHHHH!!!!!
I answer rhetorical questions for my own enjoyment.

bob
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 7565
Joined: 12/03/01, 12:00 am
Location: St. Louis
Contact:

Post by bob » 11/27/02, 4:41 am

Heh heh.... happens to me all the time, Gerry. Isn't there some editor that'll point up these errors?
WYSIWTF

SOD
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 5284
Joined: 12/06/01, 12:00 am
Location: here and there

Post by SOD » 11/27/02, 8:27 am

he he those are always fun....I also like the ones that totally throw debuggers into fits debugger says one error in reality it is something different.

Gerry
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 5727
Joined: 12/04/01, 12:00 am
Location: Perth, Western Australia
Contact:

Post by Gerry » 11/27/02, 7:34 pm

<!-- BBCode Quote Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class=postbody>Quote:</font><HR width=100% color=#333333 SIZE=1></TD></TR><TR><TD><FONT class=quote><BLOCKQUOTE>
On 2002-11-27 12:41, bob wrote:
Isn't there some editor that'll point up these errors?</BLOCKQUOTE></FONT></TD></TR><TR><TD><HR width=100% color=#333333 SIZE=1></TD></TR></TABLE><!-- BBCode Quote End --> Well it's a logic error and not a syntax error (ie. the code still runs fine, it just doesn't do what you intended it to do), sometimes your editor may display a warning if it looks like you have made a common logic error but most editors don't as it would then output errors on perfectly working code which would piss some programmers off.
I answer rhetorical questions for my own enjoyment.

Jester
Member
Posts: 3
Joined: 12/24/02, 12:00 am
Location: England
Contact:

Post by Jester » 12/24/02, 12:57 am

Hey Gerry,

Remember, in PHP when using functions like copy() (and it also applies for include() and require()) the path to the file involved should be from the SYSTEM's point of view, not the web directory.

You can normally just get away with:

next-level-dir/script.php (or whatever)

But if you're wanting to use this file from scripts in multiple directories, say a file that'll be included into various documents whose PHP_SELF value will span many dirs, it's best to use the full system path.

/home/you/www

c:program filesapache.... etc

You'll get used to it hehe

Gerry
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 5727
Joined: 12/04/01, 12:00 am
Location: Perth, Western Australia
Contact:

Post by Gerry » 12/24/02, 5:22 am

Heh, good to see ya over here! It was certainly a shock to see your name next to the most recent post.

OK well I've heard you say that before, but I'm not sure I totally agree. I prefer to use relative directories, that way you can move things around or put the script on a computer that doesn't have a c:program filesapache... directory and have the script still work. Maybe I'm missing what your saying... *stops short* OK after reading your post for like the fourth time I finally caught on, OK I get you, your right, but in this case I'm not looking to re-use the code in more than one place but I do want it to work in a relative way as stated above. Please let me know if I'm still not catching on to what your saying.

<!-- BBCode Start --><TABLE bgColor=000000 cellPadding=5 width=400 border=1><TR><TD><FONT class="ot">OFF TOPIC: Didn't know you were living in England. I should be heading back over there in a couple of years if things keep falling into place. The Twycross, Warks. area to be more precise.</font></TD></TR></TABLE><!-- BBCode End -->
I answer rhetorical questions for my own enjoyment.

Jester
Member
Posts: 3
Joined: 12/24/02, 12:00 am
Location: England
Contact:

Post by Jester » 12/29/02, 4:29 am

Well I can give you an example.

I'm writing new forums at the moment and I have a common header file that includes a language file, I include this header file at the beginning of every script, the header file is in

/forums/themes/header.inc.php

Now when I use it from /index.php and the header file includes lang/en.php, that's fine, but what when i include the header file into /admin/index.php? lang/en.php looks in /forums/admin/lang/en.php which is not there.

I can nullify this in many ways, I could do something like:

if (stristr($_SERVER['PHP_SELF'], '/admin')) {
require '../lang/en.php';
} else {
require 'lang/en.php';
}

Or I could just use the full system path.

So what I'm saying is, to avoid file path issues altogether, use the FULL path to it from the system's point of view.

/home/gerry/www/script.php

Now you don't have to do that everytime, you could put this in your config file

$fullpath = '/home/gerry/www';

Then just do:

$fullpath.'/script.php';

If you ever move the files to another server or anything, just change that full path var.

Relative paths are the best way, but sometimes it's just not possible.

Another good thing to play around with is using .htaccess (or ini_set()) to set the include_path dynamically and be rid the file path issues with include(), require() and such altogether.

Post Reply