Question - Checking for duplicate file before uploading to server?

Started by Ryan Timothy B, Thu 06/05/2010 20:36:38

Previous topic - Next topic

Ryan Timothy B

I'm quite new to PHP and I don't know much about Java.  I'm asking here because you guys are all pretty talented in different areas.  I'm getting frustrated from searching Google and cannot seem to find what I'm looking for.

Does anyone know how to check for a duplicate file name before uploading it to the server with an html upload form?  At the moment I have the original file name stored in a MySQL database and I want to check against that  before it has to be uploaded.  My guess would be to use Java.

It's for my buddies site that I made for him recently: http://www.colourfulphotos.com
I had to completely learn PHP and MySQL just to make this for him.

Sometimes he uploads about 20-30 photos at a time and once in a while he accidentally uploads a file twice and doesn't know it.  Since it's a photography website, duplicate files should never exist on it.  He wants me to add a warning that tells him before he actually has to wait 30 seconds or so per file to upload.  Is this possible?

Thanks!

Edit: I forgot to mention that the images are being uploaded by an html upload form.
Can Java even check against a MySQL table?

bicilotti

php should do, like foreach.... (check and wait here).

I'll check for connections stuff when I get to my office. I did something similar once, but it was written in asp.


Wyz

Quote from: Ryan Timothy on Thu 06/05/2010 20:36:38
Edit: I forgot to mention that the images are being uploaded by an html upload form.
Can Java even check against a MySQL table?

Not in any direct way unfortunately. When sending files php handles the transfer completely, and there is not much you can do while the upload is in progress. So your best shot is to check it before the upload even starts. You can use javascript with a http request backed by a php script that checks it against the database for that. Well it is as complicated as that sentence sounds, but can be done :)

If you don't need to check for duplicates you can just rename the file to something that does not exist. Add a number to it or something. That would certainly be a lot easier. :P
Life is like an adventure without the pixel hunts.

Ryan Timothy B

Thanks for the responses guys.

I already have the images renamed to colourfulphotosX.jpg, with X being the auto incremented number of the file.

Yes, I could easily do it without any issues by checking against the MySQL table to see if that file name has already been uploaded after he uploads them, then delete it from the temp location if it's a duplicate.  Obviously only after I upload it because PHP works server side.  But he doesn't want that.  He uploads LARGE images (eg: 4,288px × 2,848px) that are about 2MB's a piece and with his slow cellphone internet it can take up to 30 seconds or more per photo.  And he'll be uploading about 50-100 photos per night.

So yes, I'm definitely looking for a user side to server side Java script like Wyz mentioned, before the file is uploaded.  That way it can warn my friend that he's already uploaded that file.  So if he accidentally clicked on the same file or decided later that he actually wanted to crop the photo, so when he uploads the new photo with the same old name, it will ask him if he wants to replace, create a new entry, or cancel that photo upload.

If someone knows of a script or something that does what I'm asking, that they've used or know of on the internet, feel free to send it my way.  It wouldn't take me too long to understand how it works and how to tweak it.

Thanks.


Unrelated side note: The images are uploaded at the full resolution that he takes the photo at and then when it's viewed on certain areas of the website, the server resizes the image to the specified size and saves it in a Cache folder - if it hasn't already done it yet.  That way the server only has to resize them once, and I can easily change the website layout and sizes of images without any hassle at all.

Wyz

I've put some code together quickly. It checks the content of a php script, plain text.

Assume you have this in some form:
Code: ags

<input id="selector" type="file" name="file" onchange="CheckFilename()" />


This piece of code will acces a php backend:
Code: ags

		// Basic Ajax stuff
		
		<script type="text/javascript">
			function newRequest()
			{
				try { return new XMLHttpRequest(); }
					catch(e) {}
			  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
			    catch(e) {}
			  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
			    catch(e) {}
			  try { return new ActiveXObject("Msxml2.XMLHTTP"); }
			    catch(e) {}
			  try { return new ActiveXObject("Microsoft.XMLHTTP"); }
			    catch(e) {}
			  throw new Error("This browser does not support XMLHttpRequest.");
			};
			
			function RequestText(url, callback)
			{
				var xhr = newRequest();
				
				xhr.onreadystatechange = function()
				{
					var data;
					
					if (xhr.readyState == 4)
						if ((xhr.status == 200) || (!xhr.status))
							data = xhr.responseText;
					
					if (data)
						callback(data);
				}
				xhr.open('GET', url, true);
				xhr.send(null);
			}
			
			//--------------------------------------------------------------------------------------
			// The function that does the magic
			
			function CheckFilename()
			{
				filename = document.getElementById('selector').value;
				
				RequestText('check.php?file='+escape(filename),
					 function(text)
					{
						if (text == 'something')
						alert('something!');
					}
				);
			}
		</script>


Have fun ;D
Life is like an adventure without the pixel hunts.

Ryan Timothy B

Oh wow, looks like I have some playing around to do tonight once I'm done work.

QuoteIt checks the content of a php script, plain text.
Curious in case I'm understanding that wrong, the   if (text == 'something')   is that the plain text that has been echoed from the check.php file?

That's some pretty cool code, I was actually expecting to see Java do the MySQL check (i don't even know if it's possible) instead of referring to a php file that does it, in the background.  But either way is cool for me.

Also, if his browser doesn't support XMLHttpRequest, does that mean he'll be given an error each time after he clicks on the file input box?

Thanks, Wyz!

Wyz

Quote
Curious in case I'm understanding that wrong, the   if (text == 'something')   is that the plain text that has been echoed from the check.php file?

yups, that's right.

Quote
Also, if his browser doesn't support XMLHttpRequest, does that mean he'll be given an error each time after he clicks on the file input box?

That depends on the browser. This will work in for all major browsers, the rest of them I don't really know. Best guesses they give some sort of error in the status bar.

Quote
Thanks, Wyz!

You're welcome :D
Life is like an adventure without the pixel hunts.

Ryan Timothy B

Freakin' Eh!  You're a bloody genius, it works perfectly!!
Once again... THANKS!!

Edit #312 (lol):
I ran into some issues with internet explorer adding the full path C:\fakepath\image.jpg, but I managed to find a forum that another person was asking for a solution to the same issue.  She's all fixed.  Yay!

Wyz

Life is like an adventure without the pixel hunts.

SMF spam blocked by CleanTalk