Forum Moderators: coopster

Message Too Old, No Replies

What am i doing wrong?

Trying to make a simple form where people can upload small files...

         

casey133

3:17 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Hi guys, i want to create a simple form where people can "browse" for a file to upload, once submitted it will upload to the folder where the page with the form is on, so it would be in effect the root folder, altho not the folder where my index.html page is in. here is the html code i am using:

<form action="test.php" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="submit" name="Submit" value="Submit">
</form>

is this right? If so, can someone post the php code needed to make this work, or know where i can download one as i am nowhere even good at PHP... if it matters, I am using dreamweaver MX 2004

Thanks

ironik

3:32 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Put very simply, the code to upload a file would be like this:

<?php
if (is_uploaded_file($_FILES['file]['tmp_name'])
{
$destination_dir = 'some/dir/'
if (move_uploaded_file($_FILES['file']['tmp_name'], $destination_dir.basename($_FILES['file']['name'])))
{
echo 'successfully uploaded!';
} else {
echo 'file was not uploaded';
}
}
?>

This is very simple, so there's no validation in there. If the public has access to this form, then conceivably anything could be uploaded. You can read more about uploading files at:

[php.net...]

casey133

3:40 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Thanks for the fast reply... so in my case this line:

$destination_dir = 'some/dir/'

would look like this:

$destination_dir = 'public_html/folder/'

?

casey133

3:42 am on Mar 1, 2005 (gmt 0)

10+ Year Member



hum, or would it just look like this since i want to have this form in the same fole where the file will be uploaded:

$destination_dir = '/'

casey133

3:49 am on Mar 1, 2005 (gmt 0)

10+ Year Member



No go :(

i keep getting this error:

Parse error: parse error, unexpected T_STRING, expecting ']' in /home/kydesi73/public_html/upload.php on line 2

here is what i have tried to change the upload dir to

$destination_dir = 'public_html/'
$destination_dir = '/public_html/'
$destination_dir = 'public_html'
$destination_dir = '/'

should the php file have a CHMOD of 777 (all access) or something?

ironik

3:59 am on Mar 1, 2005 (gmt 0)

10+ Year Member



My mistake... put a terminating semi-colon at the end

$destination_dir = '/';

casey133

4:01 am on Mar 1, 2005 (gmt 0)

10+ Year Member



will try, one sec ;)

casey133

4:05 am on Mar 1, 2005 (gmt 0)

10+ Year Member



exact same error :(

ironik

4:24 am on Mar 1, 2005 (gmt 0)

10+ Year Member



Having a bad day.. I didn't check my code thoroughly

this line:
if (is_uploaded_file($_FILES['file]['tmp_name'])

should be:
if (is_uploaded_file($_FILES['file']['tmp_name'])

casey133

12:10 pm on Mar 1, 2005 (gmt 0)

10+ Year Member



Parse error: parse error, unexpected '{' in /home/kydesi73/public_html/upload.php on line 3

maybe its not meant for me to be able to upload anything ... i changed the CHMOD to 777 on the php file and made the changes to line 2, not its giving an error for line 3.

anything else you can think of? Thanks for all the help so far tho!

gettopreacherman

3:41 pm on Mar 1, 2005 (gmt 0)

10+ Year Member



You're missing a semicolon if you copied the code:

<?php
if (is_uploaded_file($_FILES['file]['tmp_name'])
{
$destination_dir = 'some/dir/'

At the end of the line for $destination_dir =

Should have a semicolon:

$destination_dir = 'some/dir/';

usually those errors are parse related...

casey133

7:00 pm on Mar 1, 2005 (gmt 0)

10+ Year Member



Parse error: parse error, unexpected T_VARIABLE in /home/kydesi73/public_html/upload.php on line 4

jatar_k

7:01 pm on Mar 1, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



and whats on line 4 and the couple lines before?

gettopreacherman

7:23 pm on Mar 1, 2005 (gmt 0)

10+ Year Member



Remember that if you are using a variable for the "some/directory" it will not have quotes...

casey133

7:44 pm on Mar 1, 2005 (gmt 0)

10+ Year Member



i will try that as well, sorry for all these errors guys but i am very new in PHP and am still learning as i go along, thanks for putting up with me so far.

ironik

9:09 pm on Mar 1, 2005 (gmt 0)

10+ Year Member



That's no worries. Sorry about posting broken code. Maybe you could post your entire page code here and someone can check the syntax for you.

casey133

11:21 pm on Mar 1, 2005 (gmt 0)

10+ Year Member



no problem about the code being a little off, more than i have done :) ... the page is very simple as far as the login form goes, no graphics or anything, here is the entire HTML code for the login form i will use:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Upload Reports</title>
</head>

<body>
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>

here is the current PHP code that will go along with this form, unchanged as i have it now:

<?php
{
(is_uploaded_file($_FILES['file']['tmp_name'])
$destination_dir = '/';
if (move_uploaded_file($_FILES['file']['tmp_name'], $destination_dir.basename($_FILES['file']['name'])))
{
echo 'successfully uploaded!';
} else {
echo 'file was not uploaded';
}
}
?>

casey133

12:49 am on Mar 2, 2005 (gmt 0)

10+ Year Member



hey guys i have great news but one more questiona nd i think i will be done, i found a free php script online that does what i need and more... the only thing is; if you were to upload the same file (same name, etc...) i will auto rename the file, i need it to replace the existing file with the one being uploaded. If someone is kind enough and knows enough about php to change this small part of the code, can you please send me a private message on here?

casey133

3:50 am on Mar 2, 2005 (gmt 0)

10+ Year Member



Disregard my post with the PHP code, it was to long to post all of it; if someone can private message me willing to help me out i will be very thankful...