Upload mod ; check if file exists

Snitz™ Forums 2000
https://forum.snitz.com/forumTopic/Posts/61689?pagenum=1
05 November 2025, 02:10

Topic


MarcelG
Upload mod ; check if file exists
23 April 2006, 09:58


I've got a problem that I cannot seem to solve by myself....I need the help of someone who's got the correct skills.
Here's the scenario :
Simple file upload mechanism. A single file is selected via a form, to be uploaded. Upon submit, the file is uploaded to the site, and stored in the folder site/uploaded/MemberID/filename
Now I want to enable some sort of intelligent feedback when someone tries to upload a file that already exists. The feedback would then enable a warning message plus a (previously hidden) inputfield, which defines whether or not the file should be overwritten or renamed. Meanwhile the submit button would be disabled/hidden, until the choice whether to overwrite or rename was made.
Can someone make that for me ? I can spend up to US$ 75 on it (via PayPal). The base uploading script is already available, the only thing that needs to be modified is the piece described above.
Please contact me at marcel@oxle.com for more information.<

 

Replies ...


Zuel
23 April 2006, 12:30


Avatar Mod has a class with that information. Has a checkbox for overwrite and informs user if file already exists. You probably save some cash and see what he did.<
MarcelG
23 April 2006, 13:11


Zuel, thanks for the tip, but which avatar mod would that be ? I've got an old version (by Huw if I'm not mistaking) running on oxle.com<
ruirib
23 April 2006, 14:19


I'd guess Zuel's own avatar mod smile. Check the mod W/Code forum, it's sitting at the top...<
Zuel
23 April 2006, 14:36


No no no,

RichardKinser's Mod. Sorry for not being specific. I didn't write the Upload Class, he did. You can find it on SnitzBitz, Hamlin converted it for 3.4. The base code on there would do you some good.<
MarcelG
24 April 2006, 08:13


Oh, that one. Thanks! I'll investigate tonight ; saves me 75 bucks.<
HuwR
24 April 2006, 17:41


Did you find the info you needed MarcelG ?<
MarcelG
25 April 2006, 01:23


Not yet...haven't found the time to do that yet. Will do so tonight.<
MarcelG
02 May 2006, 16:51


Guys...I'm lost....okay, not actually lost as in not knowing where I am or unable to find my way back, but I'm lost as in I cannot figure out how to get this working. The referal to the avatar upload mod brought me some other insights, but not the one I was looking for. I've described it in more detail here: http://oxle.com/topic.asp?tid=3826 and I've decided to put an incentive with it for the person who can help me out bigsmile
The theory is this:
If you upload a file to the site, you get a form with a browse button and a submit button. If you - for instance - select the file C:\MY DOCUMENTS\TESTFILE.TXT, this and you press the submit button, the file gets uploaded to http://oxle.com/uploaded/<member id>/testfile.txt. The <member id> is unique for every member.
So, in my case, uploading the file mypic.jpg to oxle would result in the following file: http://oxle.com/uploaded/18/mypic.jpg

Now, I want to improve this feature with the following extra feature. If you want to upload mypic.jpg, and that file already exists, I'd like to see a warning, ánd I want to have the option to overwrite the file (default), or rename the file (non default option).
I want that warning ánd that choice before I hit the submit button, so without a page refresh. If you think you have the solution for this, please read on.<
HuwR
02 May 2006, 17:03


it is not possible in the way that you want it to be for one simple reason, it is not possible to get the filename of the uploaded file until the form is posted, and for obvious security reasons this stuff should be done serverside.<
Zuel
02 May 2006, 17:04


Code:

<script language="JavaScript"><!-
function CheckForImage(URL) {
var ThisImage = new Image();
ThisImage .onError = alert('The following does not exist. Please correct to continue.<br><br>" + URL');
ThisImage .src = URL;
}
//--></script>

<form name="upload">
<input type="file" onChange="CheckForImage(document.upload.file.value);" name="file" />
<input type="submit" value="submit"
</form>

Spaghetti code but it should do what you want. My javascript has gotten horrible within the past few months. I believe you got the uploading piece already.
You should be able to use javascript to grab just the file name then tag on your forumurl to the beginning. You should be able to create this javascript function dynamically to get all that, even the member id to concatinate the forum url + member id folder + image name.<
HuwR
02 May 2006, 17:12


that code won't work, document.upload.file.value is a path local to the clients machine not the server so you are checking the existance of a local file not a server file, javascript like that runs on the client.<
MarcelG
02 May 2006, 17:14


Originally posted by HuwR
it is not possible in the way that you want it to be for one simple reason, it is not possible to get the filename of the uploaded file until the form is posted, and for obvious security reasons this stuff should be done serverside.
Huw, I understand what you're saying, but can you then explain me why it is working at storage.oxle.com ? (use demouser/demouser to test it)
I think it's working because the browse button is not a 'real' browse button, but I'm not sure. My understanding of javascript is lacking here....
Zuel, your function would be applicable if it concerned only images. I want it to work for both images as well as other documents.<
HuwR
02 May 2006, 17:57


the code on storage.oxle.com is php not asp<
AnonJr
02 May 2006, 18:11


What's available in PHP that makes the difference?<
HuwR
02 May 2006, 18:40


Originally posted by AnonJr
What's available in PHP that makes the difference?
the ability to check if a file exists on the server smile<
AnonJr
02 May 2006, 21:30


How is that different from using the FileSystem object? Don't you still have to do the whole round-trip with either scripting language? Or is the lack of sleep getting to me?<
MarcelG
03 May 2006, 03:02


Originally posted by AnonJr
What's available in PHP that makes the difference?
the ability to check if a file exists on the server smile question Sorry HuwR, but the page my browser is using is HTML, and all the browser does is execute some javascript. The CheckFileExist javascript function I posted at oxle is completely clientside....or not?
Code:
// Check if a file exists and update the status
function checkFileExists ( sFile, sFolder )
{
var xmlhttp = getXMLHttpObject ( );
if ( xmlhttp && fileExistStat && sFile != '' )
{
xmlhttp.onreadystatechange = function ( )
{
if ( xmlhttp.readyState == 4 )
{
if ( xmlhttp.status == 200 )
{
fileExistStat.innerHTML += xmlhttp.responseText;
fileExistStat.style.display = fileExistStat.innerHTML == '' ? 'none' : 'block';
}
}
}
xmlhttp.open ( 'GET', 'upload.php?action=checkfile&folder=' + sFolder + '&file=' + escape ( sFile ), true );
xmlhttp.send ( null );
}
}
Or is that red line responsible for checking if the file exists? <
HuwR
03 May 2006, 03:28


so, if you already have the answer, why are you asking the question ?<
HuwR
03 May 2006, 03:33


Originally posted by AnonJr
How is that different from using the FileSystem object? Don't you still have to do the whole round-trip with either scripting language? Or is the lack of sleep getting to me?
Ok, it is very simple to check if a file exists on the server using the fso, it is not possible however to check if the file you are about to upload exists because you need to post the form first before you can get at the filename, you can get at the filename using clientside javascript but you can't check if it exists on the server using clientside javascript, PHP does not have this security restriction<
MarcelG
03 May 2006, 03:42


Originally posted by HuwR
so, if you already have the answer, why are you asking the question ?
Well...sorry for trying to understand this....dead
I've been looking into upload.php, to see what it does when it's requested as mentioned in the red part. it then does this:
Code:
{
$files = gpc ( 'files', 'P', array() );
$folder = path_decode ( gpc ( 'folder', 'P', '' ) );
$path = $user_root . $folder . '/';

for ( $i = 0; $i < count ( $files ); ++$i )
{
if ( is_file ( $path . $files[$i] ) )
{
print parse ( $lang_upload['upl_file_exists_warn'], array ( '{file}' => htmlentities ( $files[$i] ), '{folder}' => ( $folder == '' ? $lang_misc['main_folder'] : basename ( $folder ) ) ) );
}
}
}
<
HuwR
03 May 2006, 03:49


MarcelG, as I said, it is the PHP doing the checking
<
MarcelG
03 May 2006, 04:06


Okay...I'll just give up then. Thanks for the feedback.<
HuwR
03 May 2006, 04:15


why can't you just use the upload code you have at storage.oxle.com ? <
MarcelG
03 May 2006, 05:08


Well, the code I'm looking for should 'plug in' to the Snitz file attachment mod. So, ASP Upload and ASP.<
HuwR
03 May 2006, 05:40


some of the asp upload components may have the ability to check, although they may still do the upload first, but do it to memory rather than to disk, you could still try to integrate the php upload form in to Snitz, alternatively you could write a .net upload component which did it all for you and integrate that in to Snitz<
AnonJr
03 May 2006, 08:33


Originally posted by HuwR
Ok, it is very simple to check if a file exists on the server using the fso, it is not possible however to check if the file you are about to upload exists because you need to post the form first before you can get at the filename, you can get at the filename using clientside javascript but you can't check if it exists on the server using clientside javascript, PHP does not have this security restriction
Way past time for me to start learning some PHP... thanks. I'm still not sure I understand the mechanics of how PHP gets the file name before you upload the file, but I'm willing to let it be ... until I get some basic PHP skills down. tongue<
Todd
03 May 2010, 20:24


Hi,

I'm wondering if this has ever been resolved. I'm using proeders file attachment mod to upload photos. More and more I'm having users upload photos with filenames they have already used. It's careless on their part but I would like a way to stop them from doing this.
HuwR
04 May 2010, 02:35


As per my previous post it is not possible to check prior to uploading the file, so the easisest thing to do would be to append the datetime stamp on the end of any filename saved, then you can't overwrite anything.
Classicmotorcycling
04 May 2010, 05:08


According to the outputFile.asp (line 79) the file is saved with the current Year, Month, Day, Hour, Minute and Second at the front of the filename that is uploaded, which in my books would give it a different name each time a file is uploaded.
Code:

  filename = Year(Now) & Month(Now) & Day(Now) &  Hour(Now) & Minute(Now) & Second(Now) & "_" & filename

Maybe change the line to read:
Code:

  filename = DateToStr(DateAdd("h", dtDateTime, strForumTimeAdjust)) & "_" & filename

Let me know how you go.
HuwR
04 May 2010, 11:14


Originally posted by Classicmotorcycling
According to the outputFile.asp (line 79) the file is saved with the current Year, Month, Day, Hour, Minute and Second at the front of the filename that is uploaded, which in my books would give it a different name each time a file is uploaded.
yep, should do, so maybe it was resolved smile
Todd
05 May 2010, 21:49


Thanks, I'll use the timestamp method. I didn't see that before it was posted today.
Todd
06 May 2010, 23:29


I used the timestamp alternative by editing the clsupload.inc file. I wasn't looking to mess with the avatar uploads, only the images that get uploaded and placed in the posts.
In the following section of that file;

'--- Get the file name ---
lngPosFilename= InstrB(lngPosBoundary, strBinRequest, _
UnicodeToBinary("filename="))
if (lngPosFileName <> 0) And _
(lngPosFileName < InstrB(lngPosEnd, strBinRequest, strBinBoundary)) Then
lngPosBeg = lngPosFileName + 10
lngPosEnd = InstrB(lngPosBeg, strBinRequest, UnicodeToBinary(Chr(34)))
strFileName = BinaryToUnicode(MidB(strBinRequest, lngPosBeg,
_ lngPosEnd - lngPosBeg))


I added the following line directly after to adjust the filename;

strFileName = DateToStr(DateAdd("h", dtDateTime, strForumTimeAdjust)) & "_" & strFileName

Worked fine for me. Didn't affect the avatar upload. I'll see how things go, works great so far.
Classicmotorcycling
07 May 2010, 06:24


Glad it is working for you Todd.
Todd
07 May 2010, 21:56


Originally posted by Classicmotorcycling
Glad it is working for you Todd.

Thanks, me too. Makes my life easier.
© 2000-2021 Snitz™ Communications