Jump to content

Image Upload


coolphpdude

Recommended Posts

Hi people,

 

Im new to PHP and new on here so hope you's can bare with me. Im doing a uni project where i have users register an account. Upon registration they can upload pictures and assignments (word files). The way i was thinking about doing it was uploading the pic and storing the URL within a relevant field in a database that corresponds to that user thus creating a link to the picture but meaning i can display the picture online from that URL. The problem i have is if 2 different users upload 2 different pictures but both pictures have the same filename. How do I make it so that there is no clash or that 1 pic doesnt get overwritten???

 

Very stuck and confused so any help will be appreciated.

 

Cheers

Link to comment
Share on other sites

Yeah i considered that. Well not so much the function (i know very little about functions within PHP to be honest), i considered writing an IF statement to do it for me but thats not what i need.

 

Because i cud have 1000 users all trying to upload 1 picture (just for example everybody's pic was titled pic1.jpg) i need to make it as automated as possible for the user. i dont want the user to have to rename his/her file (simply for the fact that a user might sit there for 10hours trying different names for their file before they find 1 that isnt already in use... if that makes sense).

Link to comment
Share on other sites

for that you can probably try looping the file_exists, so that when you finally get a unique random filename, you exit the loop and save the file

 

something like

 

while (file_exists($filename)) {
  //$filename = generate random file name....
}
//save the file now, since there is not duplicate filename anymore

Link to comment
Share on other sites

table : data

 

Id            TEXT          IMAGE

1            t1              img_1.jpg

2            t2              img_2.gif 

3            t3              img_3.jpg

4            t4              img_4.bmp

 

For record 1 you may keep image name like this image_1.jpg

where 1 is id for table which is unique for each records so no need to check file_exist.

 

In this case if two diff users have uploaded same files then nothing worried abt because they will save as diff names in source and DB.

Link to comment
Share on other sites

The majority rules on that then!! so uploading to a DB is a bad idea due to space. is there anyway that once a user registers with my site then a folder can be automatically created on my web space (so if joe blogs registered a folder would be created called /pictures/joeblogs/ and when they upload a pic it goes straight into their folder. That way if their pic is uploaded to their dedicated folder it should be the only pic in there with that name? just an idea...

 

The while loop with the random rename is looking the best option.

Link to comment
Share on other sites

you can use mkdir()

 

try something like

if the users registration is sucessful,

make a directory called 'username'

unpload the picture to that directory

 

but it would be much easier to make it have a ranodom name

then your DB would have something along the lines of:

file_id|file_title|file_name    |file_user

1      |title    | 25478h.doc  | username

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.