Page 1 of 1
Uploading Image Problem ?
Posted: Sat Feb 11, 2006 3:12 pm
by No0b
I'm trying to upload an image then display it. This is what I try doing.
Code: Select all
<?
if ($_FILES[icon][name] != "") {
@copy($_FILES[icon][tmp_name],"/home/content/nottelling/html/icons/".$_FILES[icon][name]) or $icon_result = "Didn't Change Eyecon";
$sql_for_icon = "UPDATE $table_name SET picture = '$image_name' WHERE username = '$_SESSION[username]'";
$result_for_icon = @mysql_query($sql_for_icon,$connection) or die(mysql_error());
$icon_result = "icon Changed.<img src=\"http://www.mywebsite.com/icons/$_SESSION[username]\">";
} else if ($_FILES[icon][name] == "") {
$icon_result = "Didn't Change icon";
}
echo "$icon_result";
?>
It all works when there's no <img> tag in the code. What happends is without the img tag it uploads the image to the sever but with it it doesn't for some reason it just stays the same image everytime I try and upload it.
Thanks for reading and all help it appreciated.
Posted: Sat Feb 11, 2006 3:19 pm
by feyd
- start quoting your named indices:
Code: Select all
$_FILES[icon][name]
// should be
$_FILES['icon']['name']
- use move_uploaded_file() instead of copy()
- unless you have a rewrite script in the icons directory, it will never match the session username to the icon.
- some browsers send the full path of the file in the name field of the files array, use basename() on it to ensure you get just the filename if you insist on using user submitted data.
Posted: Sat Feb 11, 2006 3:46 pm
by No0b
Thanks for the reply fayd. But can you please explain the problem of my problem? What is it the problem with my script and what's the reason to change my copy to move_uploaded_file()??? Expain why?

Posted: Sat Feb 11, 2006 3:56 pm
by feyd
I already explained what the problem is:
unless you have a rewrite script in the icons directory, it will never match the session username to the icon.
But I'll explain further. Your copy() call places the file into the icons directory using the original filename. You are trying to access it in your image tag using the username stored in sessions, which is not likely to match.
copy() is not a smart function. move_uploaded_file() checks to ensure the file you are trying to move/copy is an actual uploaded file.
Posted: Sat Feb 11, 2006 4:03 pm
by No0b
So all I have to do is change the
Code: Select all
@copy($_FILES[icon][tmp_name],"/home/content/nottelling/html/icons/".$_FILES[icon][name]) or $icon_result = "Didn't Change Eyecon";
to
Code: Select all
@move_uploaded_file($_FILES[icon][tmp_name],"/home/content/nottelling/html/icons/".$_FILES[icon][name]) or $icon_result = "Didn't Change Eyecon";
???
Posted: Sat Feb 11, 2006 4:29 pm
by John Cartwright
Please re-read feyd's first post..
Posted: Sat Feb 11, 2006 5:10 pm
by No0b
Ok it works... But how do I make a limit to the size, width and height of the file and also make sure there uploading a image??? Please awnser...

Posted: Sat Feb 11, 2006 5:13 pm
by josh
No0b wrote:Please awnser...

Asking someone to answer your question is redundant and annoying, it decreases the chances someone is going to want to answer your question...
Anyways
filesize() will give you the site in bytes of the filename that you pass to it,
getimagesize() is good for getting the dimensions of the image in pixels. You can also set a max upload size in php.ini and in your form, even with the form and php.ini limitations in place its always good to verify that the is actually the right size using filesize.
Posted: Sat Feb 11, 2006 5:55 pm
by No0b
How does that work???
Posted: Sat Feb 11, 2006 8:16 pm
by josh
You're going to need to elaborate a bit if you want help.. how does what work? Did you actually read the documentation? Is there a specific problem you're having with it?
Posted: Sun Feb 12, 2006 12:55 am
by No0b
Yes i'm sorry for being so stupid. I'm a noob...

I used the getimagesize() function in my script. The problem I'm have is this:
Code: Select all
list($width, $height, $type, $attr) = getimagesize($_FILES['icon']['tmp_name']);
if (($_FILES[icon][name] != "") && (substr('$type',0,6) == "image/")) {
@move_uploaded_file($_FILES[icon][tmp_name],"/home/content/html/icons/".$_FILES[icon][name]) or $icon_result = "Didn't Change icon";
$sql_for_icon = "UPDATE $table_name SET picture = '$image_name' WHERE username = '$_SESSION[username]'";
$result_for_icon = @mysql_query($sql_for_eyecon,$connection) or die(mysql_error());
$eyecon_result = "icon Changed.<br><img src=\"http://www.website.com/icons/$_SESSION[username]\">";
} else if ($_FILES[icon][name] == "") {
$icon_result = "Didn't Change Eyecon";
} else if (substr('$type',0,6) != "image/") {
$icon_result = "Unknown File Type";
}
then i echo the $icon_result but it doesn't seem to echo, it's just blank. What is it that i'm doing wrong?
Posted: Sun Feb 12, 2006 1:13 am
by No0b
No wait I was uploading the wrong php script to my sever. But still it displays the result saying Unknown File Type.
Posted: Sun Feb 12, 2006 1:30 am
by No0b
Ok so I changed my script to:
Code: Select all
list($width, $height, $type, $attr) = getimagesize($_FILES['icon']['tmp_name']);
if (($_FILES['icon'][name] != "") && ($type != "")) {
@move_uploaded_file($_FILES['icon'][tmp_name],"/home/content/html/icons/".$_FILES['icon'][name]) or $icon_result = "Didn't Change icon";
$sql_for_icon = "UPDATE $table_name SET picture = '$image_name' WHERE username = '$_SESSION[username]'";
$result_for_icon = @mysql_query($sql_for_icon,$connection) or die(mysql_error());
$icon_result = "icon Changed.<br><img src=\"http://www.site.com/icons/$_SESSION[username]\">";
} else if ($_FILES['icon'][name] == "") {
$icon_result = "Didn't Change icon";
} else if (substr('$type', 0, 6) != "image/") {
$icon_result = "Unknown File Type $type";
}
Now What it's doing is the same as before. The image doesn't change, if you can find anything in my script that may help it's vary appreciated.