SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    upload file, notify user the same filename is exist in the directory before upload

    hi, may i know how can i make my system to check the file name in the directory whether it has the name same as the new file that going to be uploaded?

    any reference link/tutorial?

    thank!
    newbie...really need your guidance......

  2. #2
    SitePoint Enthusiast mrsmiley's Avatar
    Join Date
    Jul 2004
    Location
    Melbourne
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Short of using an AJAX request prior to form submit, I'm not sure you can. You cant do it server side at least anyway because you have to upload the file before you can get the file name and check it.

    You could always just rename the file after it comes through anyway. When you use move_uploaded_file, the second parameter lets you name it as something else.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think without posting your file to the server (i mean without using server side script) you cannot do that. But if you want to check this when the form is submitted, then it simple. There is function named file_exists().
    PHP Code:
    $uploadingfile_name basename($_FILES['file']['name']);
    $target "./uploads";
    if(!
    file_exists($target "/" $uploadingfile_name){
      
    //do further
      //file does not exists
    }
    else{
     echo 
    'File already exists with the name $uploadingfile_name';

    Does that mean and help you out?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    I think without posting your file to the server (i mean without using server side script) you cannot do that. But if you want to check this when the form is submitted, then it simple. There is function named file_exists().
    PHP Code:
    $uploadingfile_name basename($_FILES['file']['name']);
    $target "./uploads";
    if(!
    file_exists($target "/" $uploadingfile_name){
      
    //do further
      //file does not exists
    }
    else{
     echo 
    'File already exists with the name $uploadingfile_name';

    Does that mean and help you out?
    thanks mrsmiley & rajug...

    rajug, i've tried on your suggestion...

    at the else part, i trying to check whether the user wanna replace the file by javascript below:

    HTML Code:
    <script type="text/javascript">
    		var x=window.confirm("File already exists with the name <? echo $uploadingfile_name ?>. Do you want to overwrite the file? The process can not be reversed!")
    		if (x==true)
    		{
    		<?
    		copy ($_FILES['imagefile']['tmp_name'], "images/".$_FILES['imagefile']['name']) 
    	    or die ("Wrong filetype! Only .gif, .jpg, .jpeg, .png and .bmp is allowed."); 
    		?>
    
    		alert("File <? echo $uploadingfile_name ?> is replaced.");
    
    		}
    		else
    		{
    		alert("File is not uploaded.");
    		}
    
    		</script>
    however, i found that the file is being replace no matter the user select yes or no to replace the file...

    how can i make it works correctly?

    thank you!
    newbie...really need your guidance......

  5. #5
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is it really necessary to use javascript? if you use it, then you have to consider users who do not have javascript enabled. Why not just upload the file as normal after submitting the form, check if file_exists, and show the result?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  6. #6
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh yeah, by the way the reason the file is being replaced in any case, is because the php is executed BEFORE the page is displayed, you cannot write php into javascript the way you have done it.
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You cannot use the JavaScript in that way what you have done. Do always client side script inside server side script not server side script inside client side. If you want to inform the user in javascript alert, you can use javascript inside php by echoing the script.

    And replacing the file?? you dont have to worry about it if you just want to replace the file without alerting/informing the user. Just upload the file as normal way by copying or moving the uploaded file.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  8. #8
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    If you want to inform the user in javascript alert, you can use javascript inside php by echoing the script.
    Yes, you can, but its not going to stop the rest of the php executing
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  9. #9
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks wheeler & rajug !

    well, actually i wish to check if the file already contained in the directory, it will prompt confirmation from user whether to replace the file...

    from the discussion above, i know where's the problem already...

    now looking for way to make it works like what i decided...

    if any one has experienced, please share...

    i've stuck here for some times...

    thanks!
    newbie...really need your guidance......


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •