coolphpdude Posted July 24, 2007 Share Posted July 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
obay Posted July 24, 2007 Share Posted July 24, 2007 try using the file_exists() function. if the file exists, inform your user that the filename has to be changed because someone has already uploaded a picture of the same filename. Quote Link to comment Share on other sites More sharing options...
coolphpdude Posted July 24, 2007 Author Share Posted July 24, 2007 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). Quote Link to comment Share on other sites More sharing options...
obay Posted July 24, 2007 Share Posted July 24, 2007 assign random filenames for all/duplicate uploads. try googling how to make random filenames if (file_exists($filename)) { //generate random file name and save the file } Quote Link to comment Share on other sites More sharing options...
coolphpdude Posted July 24, 2007 Author Share Posted July 24, 2007 Quick responce! Much appreciated. Have you used this before? What happens if the random filename already exists... would it then just overwrite the existing picture? My head is battered! Quote Link to comment Share on other sites More sharing options...
obay Posted July 24, 2007 Share Posted July 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
coolphpdude Posted July 24, 2007 Author Share Posted July 24, 2007 That sounds good, i'll give it a try. Thanks alot for you help mate! Quote Link to comment Share on other sites More sharing options...
jitesh Posted July 24, 2007 Share Posted July 24, 2007 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. Quote Link to comment Share on other sites More sharing options...
coolphpdude Posted July 24, 2007 Author Share Posted July 24, 2007 Would that not just make it unique within the database? If all the pics are uploaded to /pictures/ folder within my web host then the filename would not be unique without it being renamed. Quote Link to comment Share on other sites More sharing options...
wrathican Posted July 24, 2007 Share Posted July 24, 2007 can i refer you to this tutorial? http://www.php-mysql-tutorial.com/upload-to-file-server.php this shows how to create a randowm filename for the file and also insert that necessary data into mysql Quote Link to comment Share on other sites More sharing options...
jitesh Posted July 24, 2007 Share Posted July 24, 2007 You can ask to user to rename the file before uploading if any image with same file name is already exist. This is not a good idea. Quote Link to comment Share on other sites More sharing options...
coolphpdude Posted July 24, 2007 Author Share Posted July 24, 2007 Is there away to upload the image to a field within a database, that way it would be unique to a row in a database? So it actually goes into the field rather than the URL linking to the pic on the file server. Quote Link to comment Share on other sites More sharing options...
jitesh Posted July 24, 2007 Share Posted July 24, 2007 Yes it can be. but also not a good idea. because it needs lots of space in database. Quote Link to comment Share on other sites More sharing options...
wrathican Posted July 24, 2007 Share Posted July 24, 2007 if you are really sure you want to do it then use the link i posted above and go to the previous page of it... it tells you how to do it there but it really is not a good idea... Quote Link to comment Share on other sites More sharing options...
coolphpdude Posted July 24, 2007 Author Share Posted July 24, 2007 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. Quote Link to comment Share on other sites More sharing options...
wrathican Posted July 24, 2007 Share Posted July 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
coolphpdude Posted July 24, 2007 Author Share Posted July 24, 2007 Not a bad idea. Maybe i could do both. So... registration successful create folder rename filename to random name upload pic and record to database. Am i understanding this right? Quote Link to comment Share on other sites More sharing options...
wrathican Posted July 24, 2007 Share Posted July 24, 2007 yes that would be right. but if your going to make a random filename then you do not need to do that Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.