By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,816 Members | 2,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,816 IT Pros & Developers. It's quick & easy.

registered user upload image & store file path to MySQL

P: 1
Hi

I'm a beginner with PHP and MySQL and would like to add a function to a website that lets registered users upload an avatar picture of themselves and store the path in MySQL.

Then have a page which displays back the correct image for whoever is logged in.

I created 2 mySQL tables, users & images

Users table fields - username (is PK), name, password
images table fields - imageID (is PK), username (is FK) and imagePath.

I run through online tutorials and have got an image upload working but i have problems

1. if I upload 2 images with the same name i get an error so i think i need to rename each image to a random filename.

2. displaying conect from the database for member currently logged in, both text and image. the online tutorial i echo'd the image but just got the path.

If someone could explain how to write this code for this purpose, i use notepad++ i would very much like to learn php

thanks you
Mar 25 '12 #1
Share this Question
Share on Google+
2 Replies


100+
P: 1,059
for problem one:
user name is unique i guess, if not then first suggestion make username unique. you can do a silly trick. Just save the image file same as user name. such as user johny, if user johny upload a file then rename the image file as johny.jpeg

for problem 2:
if you echo image path, it will display the path. for the browser the path is a simple text. To display the image you must have to follow the standard html rules related with img:
a small example:
Expand|Select|Wrap|Line Numbers
  1. <img src='your_image_path_including_file_name_' alt='file missing'>
  2.  
Mar 26 '12 #2

PsychoCoder
Expert Mod 100+
P: 465
Take a look at this, renaming your file should help stop duplicate file names.

Upload and rename file

If you didnt want to use their way of generating a random number then you can replace that with a GUID, which can look like so:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function create_guid(){
  3.     if (function_exists('com_create_guid')){
  4.         return com_create_guid();
  5.     }else{
  6.         mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
  7.         $id = strtoupper(md5(uniqid(rand(), true)));
  8.         $hyphen = chr(45);// "-"
  9.         $uuid = chr(123)// "{"
  10.                 .substr($id, 0, 8).$hyphen
  11.                 .substr($id, 8, 4).$hyphen
  12.                 .substr($id,12, 4).$hyphen
  13.                 .substr($id,16, 4).$hyphen
  14.                 .substr($id,20,12)
  15.                 .chr(125);// "}"
  16.         return $uuid;
  17.     }
  18. }
  19. echo guid();
  20. ?>
Hope that helps :)
Mar 26 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.