|
|
|
|
Thread title: image uploading, need help |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
11-05-2011, 10:26 PM
|
#1
|
Status: Paladin
Join date: Jul 2006
Location: Sheffield, UK
Expertise: design, front-end markup
Software: Photoshop
Posts: 2,353
|
image uploading, need help
Hey guys,
I wrote this pretty quickly; http://screensnapr.com/v/Q2SqQJ.png
If the file doesnt exist, then it uploads and saves the image fine. But, if the file does already exist, then i add an 8 digit number to the beginning of the file. It gives the url fine, but the problem is, it doesnt save the image.
Any ideas guys?
|
|
11-06-2011, 12:44 PM
|
#2
|
Status: Ruby on Rails Developer
Join date: Oct 2004
Location: England, UK
Expertise: Ruby, Rails, jQuery
Software: Chocolat, Sublime Text 3
Posts: 2,343
|
Firstly, I'd create random generated names for the images so you don't need to do a check if it exists, work on it time so the number can only go forward, saves the if/else malarky.
Post removed, updating code. Brb.
|
|
11-06-2011, 03:58 PM
|
#3
|
Status: Paladin
Join date: Jul 2006
Location: Sheffield, UK
Expertise: design, front-end markup
Software: Photoshop
Posts: 2,353
|
I come up with a solution, just dont even check if it exists...
PHP Code:
$random_digit=rand(00000000,99999999);
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $random_digit . $_FILES["file"]["name"]);
$newthumb = "images/" . $random_digit . $_FILES["file"]["name"];
|
|
11-07-2011, 02:09 PM
|
#4
|
Status: I'm new around here
Join date: Nov 2011
Location: Denmark
Expertise: Web Back End / Front End
Software: Photoshop, Netbeans, xampp
Posts: 1
|
|
|
11-07-2011, 06:01 PM
|
#5
|
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
Posts: 6,894
|
While the chances of a duplicate are slim the only completely sure way to do it is to use a do...while loop and rename it every time an existing one is made, to give a quickly typed pseudocode example
PHP Code:
$uploaded=false; do{ $randNum=rand(0,99999); if file+$randnum does not exist upload file $uploaded=true; end if } while($uploaded==false);
|
|
11-10-2011, 02:35 AM
|
#6
|
Status: Member
Join date: Jun 2008
Location: Buenos Aires, Argentina
Expertise: programming, wizardy
Software:
Posts: 165
|
That random digit think should do the work, but as they said, I'd try to slim down the chances by adding at least one or two characters at the end or beggining of the name, it's better to avoid than to regret.
Cheers!
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|