Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

randon number

Status
Not open for further replies.

zishan876

Programmer
Mar 19, 2007
61
US
Hi all:
how do i associate a picture with a user when they upload? the pic uploads fine but do not know how it goes back to the user? please help
Code:
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("sphotos/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "sphotos/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "sphotos/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
thanks
 
that depends on how you are storing the information. Perhaps you can rename the uploaded file so the name contains the user, such as username_file.jpg or something.

If you have a database you can have a table that has user's and their associated images.

But of course this is all just guessing, perhaps if you gave us a bit more information, like what you are doing with the images and how you track your users we can be of more help.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for replying back... well this is my first upload page and yes I am using a database to store user info...
I would like to if I can when the user saves his/her info to store the picture as well into the table...
But do not know how to do that with pics... with data info yes thats easy...
basically it is a list of schools that users go in and place there info and the school logo... So when they search a school the logo comes up with the school..

Hope this helps out...
Thanks
 
You can use a Blob field to store the image into the database, however I recommend you just use a normal varchar field to store the path to the image. Its easier to maintain images that way, and requires less code to show them back than if you store them in the DB.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
hi do you have an example of this...
so instead of storing the image in my sphotos folder i would store it in the database under a blob with an attribute of varchar...is that right?
please provide an example thanks
 
No, I said you can store it in the database in a Blob type field. But that I don't recommend it.

Its better if you just use a varchar field to store the path to file, in your case /sphotos/image.jpg.

Store the path in your users table, or in another table where you have the user Id and the image path.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
ohhhh ok... cool but how does one contenate in php.. Cause two users can have logo.gif but I like for it to be stored as schoolname_logo.gif or randomnumber_logo.gif....
Like
sphotos/".$school"_" . $_FILES["file"]["name"]); <== this does not seem to work??
 
$file="sphotos/" . $school . "_" . $_FILES["file"]["name"];

You need periods to concatenate each section.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
in fact with your setup there is no need for you to store anything in the database at all. just name the logo with the primarykey from the relevant school entry in the table.
 
hi, you have some security flaws here.

1) do a check if(is_uploaded_file())
> if it is
>> check width or height of $im @imagecreate()
>>> if it has no width, this is not an image
>>> if it has a width
>>>> move to the proper folder and chmod() it 0755 or what you prefer.
> if it isnt, move it elsewhere and chmod() it to 0600

Remember to pad the imagecreate with @ in front, so your user wont see the error.

Also, if its not a valid image, just do a header with location and 301, also connectio:close.

Code:
header("HTTP/1.1 301 Moved Permanently"); 
header("location: [URL unfurl="true"]http://www.domain.tld/");[/URL] 
header("Connection: close");

Side note: I made this script yesterday, its an imagehostingsite and it uses what I just said.

Also it stores the filename in the db.
btw. to generate a unique filename, I made this genious simple code:

Code:
while(is_file("path/to/{$newfilename}")) {
$newfilename = genRand() . "_" . $newfilename;
}

You dont have the genRand() function here, but you can always just KISS it and do a md5(microtime()) for a simple and effective sollultion.

Oh, also.. if you are NEAR the db or just thinking about the DB, read up on mysql injection, its under the mysql_real_escape_string. I would use the "best practice".

Ps. you have to reference the db in there, or the sql query will fail, also you might want to set the db_query to a $result var and loop that, for output.

Good luck!

Some references for you:

Olav Alexander Mjelde
Admin & Webmaster
 
forgot one thing:
I had one strange bug in my script, with the exception of unsupported filetypes and the header:location inside the loop.

I figured out it was due to the fact that the header didnt "react" quick enough, so it still processed the script.

So I put a die(); beneath the connection:close too, then its worked super since.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top