Forum Moderators: coopster

Message Too Old, No Replies

upload images with php

upload images with php, what's wrong with this code?

         

ikbenhet1

12:45 pm on Mar 1, 2003 (gmt 0)

10+ Year Member



Hello, i've been searching for a while now for the php code to upload a single image file through php to the server.

I found lotta stuff, but they were for only gif or only jpeg upload, i would like to be able to upload at least both types.

I found this code below somewhere on www, but i can't get it to work.
I 777'd the upl directory. I select a file, click upload, but i always get the error message.

What is wrong? (or maybe there is a specific thread i should read,please point me to it. thanks.)

-----

uploadform.php:
<form enctype="multipart/form-data" action="upload.php" method="post" name="upload_file">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000">
<input name="userfile" type="file">
<input type="submit" value="upload" name="file_uploaded">
</form>

-----

upload.php:
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
$file_realname = $_FILES['userfile']['name'];
copy($_FILES['userfile']['tmp_name'], "http://www.the-server.nl/upl/".trim($file_realname));
}
else
{
echo "<b><font color=red>No file uploaded.</font></b><BR>No file available or file too big to upload.";
}
echo $file_realname;
?>

Birdman

12:56 pm on Mar 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this out, ikbenhet1.

$file_realname = trim($_FILES['userfile']['name']);
$uploaddir = your/dir/;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $file_realname)) {
} else {
print "<strong>$file_realname</strong> did not upload!";
}

added semicolon

[edited by: Birdman at 1:24 pm (utc) on Mar. 1, 2003]

ikbenhet1

1:11 pm on Mar 1, 2003 (gmt 0)

10+ Year Member




Birdman,
i get this message:

did not upload!

it seems that $file_realname is empty.

The server i'm using:
Apache/1.3.14 (Unix) PHP/3.0.18 PHP/4.0.3pl1

Is my php version too old?

Birdman

1:21 pm on Mar 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe try setting the variable yourself, as a test. Then see if it uploads.

$file_realname = test.jpg;

Or echo the variable:

$file_realname = trim($_FILES['userfile']['name']);
echo $file_realname;

Not sure if you have already tried this.

<sorry>After re-reading your post, I see that the var was not set. Maybe try removing trim().

Birdman

1:31 pm on Mar 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe I found the prob. Since you are on an old version of php, you need to call the variables with $HTTP_POST_FILES instead of _FILES

Found it here [php.net].

$file_realname = trim($HTTP_POST_FILES['userfile']['name']);

edit reason-typo-just woke up ;)

ikbenhet1

1:41 pm on Mar 1, 2003 (gmt 0)

10+ Year Member



Thanks Birdman,

The filename now shows up, but no upload.

The code is now:

$file_realname = trim($HTTP_POST_FILES['userfile']['name']);
$uploaddir = "upl/";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $file_realname)) {
} else {
print "<strong>$file_realname</strong> did not upload!";
}
echo $file_realname;

I will study the link. And start from scratch.

Too bad, that i can't even get the a ready-made php example to work.

Birdman

1:46 pm on Mar 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On my site I define my directory like this:

$uploaddir = "/home/username/public_html/images/";

Rather than:

$uploaddir = "/images/";

Hope that does it for you. Your close!

andreasfriedrich

1:48 pm on Mar 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>if (move_uploaded_file($_FILES['userfile']['tmp_name'],

Of course you will need to replace $_FILES with $HTTP_POST_FILES everywhere in your code. You missed it in the piece I quoted. ;)

Andreas

ikbenhet1

4:24 pm on Mar 1, 2003 (gmt 0)

10+ Year Member




Yes, it works now. Thanks birdman and Andreas, you guys are the best.