Renaming File

Last post 08-26-2010, 11:06 PM by stoi2m1. 6 replies.
Sort Posts: Previous Next
  •  08-22-2010, 7:30 PM 63553

    Renaming File

    I want to rename my files as they upload to remove any spaces from the file name and replace them with underscores ( _ ).
     
    Thanks,
    Jesse
    Filed under:
  •  08-23-2010, 2:33 PM 63572 in reply to 63553

    Re: Renaming File

    Jesse,
     
    Please check the following article:
     
     

    Custom file upload handler

    If you need further control over the parsing of the file uploading request, you can write a custom file upload handler. In handler page (UploadUrl) developers can rename the uploaded files, process other logics programmatically.


    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  08-23-2010, 7:33 PM 63577 in reply to 63572

    Re: Renaming File

     Adam,
     
    I tried to use the following code from option 2.
    $filename = str_replace(' ', '_', $mvcfile->FileName);
    $mvcfile->CopyTo($uploadpath.$filename);
     
    But Im getting this error Call to a member function CopyTo() on a non-object.
     
    Jesse
  •  08-23-2010, 9:21 PM 63578 in reply to 63577

    Re: Renaming File

    Please try the following code:
    <?php require_once "phpuploader/include_phpuploader.php" ?>
    <?php session_start(); ?>   
    <html>  
    <head>
     <title>Demo 1 - use SaveDirectory property</title>
    </head>
    <body>   
            <form id="form1" method="POST">   
                <?php   
                    $uploader=new PhpUploader();   
                    $uploader->Name="myuploader";    
                    $uploader->Render();   
                ?>   
            </form>   
    <?php   
    //Gets the GUID of the file based on uploader name   
    $fileguid=@$_POST["myuploader"];   
    if($fileguid)   
    {   
        //get the uploaded file based on GUID   
        $mvcfile=$uploader->GetUploadedFile($fileguid);   
        if($mvcfile)   
        {   
            //Gets the name of the file.   
            echo($mvcfile->FileName);   
            //Gets the temp file path.   
            echo($mvcfile->FilePath);   
            //Gets the size of the file.   
            echo($mvcfile->FileSize);
            //echo "eric1".$mvcfile->FileName."eric2";
            $filename = str_replace('_', '', $mvcfile->FileName);
            //echo "eric3".$filename;       
            $targetfilepath= "savefiles/myprefix_" . $filename;   
        if( is_file ($targetfilepath) )   
            unlink($targetfilepath);   
            $mvcfile->CopyTo( $targetfilepath );      
            //Moves the uploaded file to a new location.   
            //$mvcfile->MoveTo("/uploads");   
            //Deletes this instance.   
            $mvcfile->Delete();   
        }   
    }   
    ?>  
    </body>   
    </html> 
    Regards,
    Eric
  •  08-24-2010, 6:37 PM 63600 in reply to 63578

    Re: Renaming File

    This is the current block of code I am using to store the file in the Absolute File Path of $uploadpath:
    $uploader=new PhpUploader();
    $uploader->Name="myuploader";
    $uploader->SaveDirectory = $uploadpath;
    $uploader->InsertText="Select MP3 to Upload";
    $uploader->AllowedFileExtensions="*.mp3";    
    $uploader->ManualStartUpload=true;
    $uploader->Render();
     
    I have changed it to (per the code above by Eric):
    $uploader=new PhpUploader();
    $uploader->Name="myuploader";
    $uploader->InsertText="Select MP3 to Upload";
    $uploader->AllowedFileExtensions="*.mp3";    
    $uploader->ManualStartUpload=true;
    $uploader->Render();
                                
    //Gets the GUID of the file based on uploader name   
    $fileguid=@$_POST["myuploader"];   
    if($fileguid) {   
       //get the uploaded file based on GUID   
      $mvcfile=$uploader->GetUploadedFile($fileguid);   
      if($mvcfile) {   
        //Gets the name of the file.   
        echo($mvcfile->FileName);   
        //Gets the temp file path.   
        echo($mvcfile->FilePath);   
        //Gets the size of the file.   
        echo($mvcfile->FileSize);
        //echo "eric1".$mvcfile->FileName."eric2";
        $filename = str_replace(' ', '_', $mvcfile->FileName);
        //echo "eric3".$filename;       
        $targetfilepath = $uploadpath . $filename;   
        if( is_file ($targetfilepath) )   
          unlink($targetfilepath);   
        $mvcfile->CopyTo( $targetfilepath );      
        //Moves the uploaded file to a new location.   
        //$mvcfile->MoveTo("/uploads");   
        //Deletes this instance.   
        $mvcfile->Delete();   
      }   
    }   
     
    The file is not going to the specified location of CopyTo. I dont think its leaving the temp location. And the only output Im getting now is the temp location and not the filesize, filename, and file location information I was getting before when the upload was complete.
     
    Thanks for all of the support,
    Jesse
  •  08-26-2010, 10:32 AM 63644 in reply to 63600

    Re: Renaming File

    You are using multiple files upload, please try the following code:
    <?php require_once "phpuploader/include_phpuploader.php" ?>
    <?php session_start(); ?>
    <html>
    <body>
    <form id="form1" method="POST"><?php   
    $uploader=new PhpUploader();
    $uploader->Name="myuploader";
    $uploader->MultipleFilesUpload=true;
    $uploader->Render();
    ?>
    </form>
    <?php
    $fileguidlist=@$_POST["myuploader"];
    if($fileguidlist)
    {
     $guidlist=explode("/",$fileguidlist);
     echo("<div style='font-family:Fixedsys;'>");
     echo("Uploaded ");
     echo(count($guidlist));
     echo(" files:");
     echo("</div>");
     echo("<hr/>");
     foreach($guidlist as $fileguid)
     {
      $mvcfile=$uploader->GetUploadedFile($fileguid);
      if($mvcfile)
      {
       echo("<div style='font-family:Fixedsys;border-bottom:dashed 1px gray;padding:6px;'>");
       echo("FileName: ");
       echo($mvcfile->FileName);
       echo("<br/>FileSize: ");
       echo($mvcfile->FileSize." b");
       //  echo("<br/>FilePath: ");
       //  echo($mvcfile->FilePath);
       echo("</div>");
       $filename = str_replace(' ', '_', $mvcfile->FileName); 
       echo "eric1"; 
       $uploadpath="z:/temp/";   //please replace path to your actual path
       $targetfilepath = $uploadpath . $filename;
       if( is_file ($targetfilepath) )
       unlink($targetfilepath);
       $mvcfile->CopyTo( $targetfilepath );   
       $mvcfile->Delete();
      }
     }
    }
    ?>
    </body>
    </html>
    Regards,
    Eric
  •  08-26-2010, 11:06 PM 63680 in reply to 63644

    Re: Renaming File

    I just wanted to say thanks, I did finally get this working. I really do appreciate you patients.
     
    Thanks Again,
    Jesse
View as RSS news feed in XML