File name append when already present

Last post 11-30-2012, 6:32 AM by Kenneth. 8 replies.
Sort Posts: Previous Next
  •  11-18-2012, 12:19 PM 75281

    File name append when already present

    I was doing some mobile testing this weekend .. uploading images form my iOS device. Greatness. But, when a file already exists with the same name, the newly uploaded file never appears.

    If a file is uploaded with the same name as one present, is / could there be a way to just append "_1 / _2" the file name? This would be nice!

     

     

  •  11-19-2012, 6:03 AM 75283 in reply to 75281

    Re: File name append when already present

    Hi mtimmons,

     

    I suggest you use method "CopyTo" to handle the upload file store location and the file name, then before you save the upload file, you can check the file name first and save it as a new name if exists.

     

    Below is the example which shows you how to use method "CopyTo" to handle the upload file.

     

    1. <?php require_once "phpuploader/include_phpuploader.php" ?> 
    2. <?php session_start(); ?> 
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    4. <html xmlns="http://www.w3.org/1999/xhtml"
    5.     <head> 
    6.         <title> 
    7.             example 
    8.         </title> 
    9.     </head> 
    10.     <body> 
    11.  
    12.         <form id="form1" method="POST"
    13.             <?php                 
    14.                     $uploader=new PhpUploader(); 
    15.                     $uploader->MaxSizeKB=10240; 
    16.                     $uploader->Name="myuploader"
    17.                     $uploader->InsertText="Select multiple files (Max 10M)"
    18.                     $uploader->AllowedFileExtensions="*.jpg,*.png,*.gif,*.txt,*.zip,*.rar";   
    19.                     $uploader->MultipleFilesUpload=true; 
    20.                     $uploader->Render(); 
    21.  
    22.             ?> 
    23.  
    24.         </form> 
    25.  
    26.  
    27.         <?php 
    28. $fileguidlist=@$_POST["myuploader"]; 
    29. if($fileguidlist
    30. $guidlist=explode("/",$fileguidlist);    
    31.     foreach($guidlist as $fileguid)     
    32.     {     
    33.          
    34.         //get the uploaded file based on GUID        
    35.         $mvcfile=$uploader->GetUploadedFile($fileguid);        
    36.         if($mvcfile)        
    37.         {        
    38.         $mvcfile->CopyTo("D:/sites/phpuploader/test/".$mvcfile->FileName);  
    39.         }     
    40.     }     
    41.  
    42. ?> 
    43.  
    44.         </div> 
    45.     </body> 
    46. </html> 
     

    Regards,

     

    Ken

  •  11-27-2012, 4:50 PM 75358 in reply to 75283

    Re: File name append when already present

    I have not had much luck implementing this..

    any more direction?

    my code fromt the page where my uploader is..

    <div class="demo" id="uploaderDiv" style="display:none;">                        
           <h1>Selecting multiple files for upload</h1>
    <h2><p>You Can select multiple files and upload multiple files at once.</p>
    <form id="form1" method="POST">
    <?php    

    $uploader=new PhpUploader();
    $uploader->MaxSizeKB=2150400;
    $uploader->Name="myuploader";
    $uploader->MultipleFilesUpload=true;


    if ($dir=="../../")
    {
    $uploader->SaveDirectory="/FTP2/WebDisk/Uploaded_Files/";
    $uploader->InsertText="<br><br>CLICK TO UPLOAD FILE(S)<br>* Max 2 GB *<br><br>Goes to the 'Uploaded Files' Folder.<br><br>";

    }
    else
    {
    $uploader->SaveDirectory=$dir;
    $uploader->InsertText="<br><br>CLICK TO UPLOAD FILE(S)<br>* Max 2 GB *<br><br>Goes to the currently open Folder.<br><br>";

    }
    $uploader->AllowedFileExtensions="*.jpg,*.png,*.gif,*.bmp,*psd,*.tiff,*.tga,*.ind,*.ils,*.eps,*.txt,*.ptf,*.zip,*.rar,*.mp3,*.wav,*.bwf,*.aif,*.m4a,*.omf,*.mov,*.m4v,*.mp4,*m4r,*,sd2,*.caf,*.alac,*.pdf";
    $uploader->Render();
    ?>
    </form>
    <?php

    $fileguidlist=@$_POST["myuploader"];
    if($fileguidlist)
    {
    $guidlist=explode("/",$fileguidlist);
    foreach($guidlist as $fileguid)
    {

    //get the uploaded file based on GUID
    $mvcfile=$uploader->GetUploadedFile($fileguid);
    if($mvcfile)
    {
    $mvcfile->CopyTo("/FTP2/WebDisk/Uploaded_Files/test/".$mvcfile->FileName);
    }
    }

    }


    ?> 

  •  11-28-2012, 6:41 AM 75366 in reply to 75358

    Re: File name append when already present

    Hi mtimmons,

     

    1. Do not use "SaveDirectory" to store the upload file, I provided the new method "CopyTo" to handle the upload file store location.

     

    2.  $mvcfile->FileName is the original file name. In your requirement, you need to change it if has the same name file, so please wire your own code to check if the file exists, then save as a new name.

     

    if(file exists)

    {

    $mvcfile->CopyTo("/FTP2/WebDisk/Uploaded_Files/test/".NewFileName); 

    }

    else

    {

     $mvcfile->CopyTo("/FTP2/WebDisk/Uploaded_Files/test/".$mvcfile->FileName);

    }

     

    Regards,

     

    ken

     

  •  11-28-2012, 7:35 PM 75368 in reply to 75366

    Re: File name append when already present

    Ok, I put in sode code with a little time stamp to do the job.
    Question however,  Is there a way to NOT use get?
     
    Right now I use this code on a Div that I unhide on a page to upload:
    1. <div class="demo" id="uploaderDiv" style="display:none;">                         
    2.         <h1>Selecting multiple files for upload</h1> 
    3.         <h2><p>You Can select multiple files and upload multiple files at once.</p> 
    4.           <?php     
    5.  
    6.                 $uploader=new PhpUploader(); 
    7.                 $uploader->MaxSizeKB=2150400; 
    8.                 $uploader->Name="myuploader"
    9.                 $uploader->MultipleFilesUpload=true; 
    10.  
    11.                 $uploader->SaveDirectory="/FTP2/WebDisk/Uploaded_Files/"
    12.                 $uploader->InsertText="<br><br>CLICK TO UPLOAD FILE(S)<br>* Max 2 GB *<br><br>Goes to the 'Uploaded Files' Folder.<br><br>"
    13.  
    14.                 $uploader->AllowedFileExtensions="*.jpg,*.png,*.gif";     
    15.                 $uploader->Render(); 
    16.  
    17.             ?> 
    18.                                      
    19.             <ol id="filelist" align="left"
    20.             </ol> 
    21.             <input type="button" name="answer" value="Close" onclick="fadeTrans('uploaderDiv')" style="width:100;" />      
    22. </div> 
    However, with code provided, you were using a form to PUSH to info. Is it possible to do it without a form like I am above:
     
    New code:
     
    1. <div class="demo" id="uploaderDiv" style="display:none;">                         
    2.         <h1>Selecting multiple files for upload</h1> 
    3.         <h2><p>You Can select multiple files and upload multiple files at once.</p> 
    4.      
    5.           <?php     
    6.  
    7.                 $uploader=new PhpUploader(); 
    8.                 $uploader->MaxSizeKB=2150400; 
    9.                 $uploader->Name="myuploader"
    10.                 $uploader->MultipleFilesUpload=true; 
    11.  
    12.                 { 
    13.                 $uploader->InsertText="<br><br>CLICK TO UPLOAD FILE(S)<br>* Max 2 GB *<br><br>Goes to the currently open Folder.<br><br>"
    14.  
    15.                 } 
    16.                 $uploader->AllowedFileExtensions="*.jpg,*.png,*.pdf";     
    17.                 $uploader->Render(); 
    18.          
    19.                 $fileguidlist=@$_POST["myuploader"];  
    20.                     if($fileguidlist)  
    21.                     {  
    22.                     $guidlist=explode("/",$fileguidlist);     
    23.                         foreach($guidlist as $fileguid)      
    24.                         {      
    25.                               
    26.                             //get the uploaded file based on GUID         
    27.                             $mvcfile=$uploader->GetUploadedFile($fileguid);         
    28.                             if($mvcfile)         
    29.                             {         
    30.                                 $path="/FTP2/WebDisk/Uploaded_Files/test/"
    31.                                 $currentfilename=$mvcfile->FileName; 
    32.                                 $time=date('H:i:s').'_'
    33.                                 if(file_exists($path.$currentfilename)) 
    34.                                     { 
    35.                                         $mvcfile->CopyTo($path.$time.$currentfilename);  
    36.                                     } 
    37.                                     else 
    38.                                     { 
    39.                                      $mvcfile->CopyTo($path.$currentfilename); 
    40.                                     } 
    41.                                                              
    42.                                              
    43.                                                
    44.                             }      
    45.                         }      
    46.                       
    47.                     }  
    48.                     ?>  
    49.             <ol id="filelist" align="left"
    50.             </ol> 
    51.             <input type="button" name="answer" value="Close" onclick="fadeTrans('uploaderDiv')" style="width:100;" /> 
    52.          
    53.     </div> 
  •  11-29-2012, 6:07 AM 75369 in reply to 75368

    Re: File name append when already present

    Hi mtimmons,

     

    For now can not achieve it without the form.

     

    Regards,

     

    Ken

  •  11-29-2012, 6:32 PM 75371 in reply to 75369

    Re: File name append when already present

    Thanks. I reworked my implmentation to allow the form to work. The uploader is now on its own page....

    Anyway, Now I have lost the print out for showing the upload status and completion via the ajax handler.

     

    Any suggestions? Here is the code now (I am not showing the ajaz-mutiple file function because is conflicst)

     

     

    1. <html> 
    2. <head> 
    3.  
    4. <link href="demo.css" rel="stylesheet" type="text/css" /> 
    5. </head> 
    6.  
    7. <body> 
    8.                        
    9.         <?php require_once "phpuploader/include_phpuploader.php" ?>  
    10.         <?php session_start(); ?>  
    11.  
    12.         <h1>Selecting multiple files for upload</h1> 
    13.         <h2><p>You Can select multiple files and upload multiple files at once.</p> 
    14.          <h2><p align =center"><a href="mailto:post.admin@fellowshipchurch.com?Subject=Upload%20Support" style='color:white; size=15' col ><br>Email support.</a></p> 
    15.                         <form id="form1" method="POST">   
    16.                                                  
    17.                                     <?php 
    18.                                         ob_start(); 
    19.                                         include 'index.php'
    20.                                         ob_end_clean();  
    21.                                         $dir=$_SESSION['phpuploader_dir']; 
    22.                                  
    23.                                         $uploader=new PhpUploader(); 
    24.                                         $uploader->MaxSizeKB=2150400; 
    25.                                         $uploader->Name="myuploader"
    26.                                         $uploader->MultipleFilesUpload=true; 
    27.                              
    28.                                         if ($dir=="../../"
    29.                                             { 
    30.                                             $uploader->InsertText="<br><br>CLICK TO UPLOAD FILE(S)<br>* Max 2 GB *<br><br>Goes to the 'Uploaded Files' Folder.<br><br>"
    31.                                             $path="/FTP2/WebDisk/Uploaded_Files/test/"
    32.                                             } 
    33.                                         else 
    34.                                             { 
    35.                                             $uploader->InsertText="<br><br>CLICK TO UPLOAD FILE(S)<br>* Max 2 GB *<br><br>Goes to the currently open Folder.<br><br>"
    36.                                             $path=$dir
    37.                                             } 
    38.                                         $uploader->AllowedFileExtensions="*.jpg,*.png,*.gif,*.bmp,*psd,*.tiff,*.tga,*.ind,*.ils,*.eps,*.txt,*.ptf,*.zip,*.rar,*.mp3,*.wav,*.bwf,*.aif,*.m4a,*.omf,*.mov,*.m4v,*.mp4,*m4r,*,sd2,*.caf,*.alac,*.pdf";   
    39.                                         $uploader->Render(); 
    40.                            
    41.                                     ?>    
    42.                                     </form> 
    43.                                    <?php  
    44.                         $fileguidlist=@$_POST["myuploader"];  
    45.                         if($fileguidlist)  
    46.                         {  
    47.                         $guidlist=explode("/",$fileguidlist);     
    48.                             foreach($guidlist as $fileguid)      
    49.                             {      
    50.                                   
    51.                                 //get the uploaded file based on GUID         
    52.                                 $mvcfile=$uploader->GetUploadedFile($fileguid);         
    53.                                 if($mvcfile)         
    54.                                 {         
    55.                                                  
    56.                                                 $currentfilename=$mvcfile->FileName; 
    57.                                                 $time=date('H:i:s').'_'
    58.                                                 if(file_exists($path.$currentfilename)) 
    59.                                                     { 
    60.                          
    61.                                                             $mvcfile->CopyTo($path.$time.$currentfilename);  
    62.                                                     } 
    63.                                                     else 
    64.                                                     { 
    65.                                                      $mvcfile->CopyTo($path.$currentfilename); 
    66.                                                     } 
    67.                                                
    68.                                 }      
    69.                             }      
    70.                           
    71.                         }  
    72.                         ?>  
    73.                 <ol id="filelist" align="left"
    74.                 </ol> 
    75.  
    76.  
    77.  
    78.  
    79.     </div> 
  •  11-29-2012, 7:04 PM 75372 in reply to 75371

    Re: File name append when already present

    I tried to add:

     

    1. <script type='text/javascript'
    2.     function CuteWebUI_AjaxUploader_OnTaskComplete(task) 
    3.     { 
    4.         var div=document.createElement("DIV"); 
    5.         div.innerHTML=task.FileName + " is uploaded!"
    6.         document.body.appendChild(div); 
    7.     } 
    8.     </script>  

    But, the problem is that page refreshes itself after the upload.

     

  •  11-30-2012, 6:32 AM 75376 in reply to 75372

    Re: File name append when already present

    Hi mtimmons,

     

    The post back is necessary because the control needs to send the upload data to the server.

     

    You can handle the upload start manually, like example http://phpfileuploader.com/demo/form-manualstart.php

     

    Regards,

     

    Ken

View as RSS news feed in XML