Uploading Image Problem ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Uploading Image Problem ?

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post 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? 8O
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post 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";
???
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Please re-read feyd's first post..
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post 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... :(
Last edited by No0b on Sat Feb 11, 2006 5:45 pm, edited 1 time in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post by No0b »

How does that work???
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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?
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post 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?
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post by No0b »

No wait I was uploading the wrong php script to my sever. But still it displays the result saying Unknown File Type.
User avatar
No0b
Forum Commoner
Posts: 37
Joined: Tue Feb 07, 2006 6:17 pm

Post 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.
Post Reply