Unable to get file name of uploaded file

Last post 02-12-2012, 4:34 AM by manpakhong. 11 replies.
Sort Posts: Previous Next
  •  06-22-2011, 5:07 PM 68093

    Unable to get file name of uploaded file

    I am trying to rename a file after it has been uploaded but I am unable to get the file name.  I can get the GUID for the file but not the file name.  Any ideas why $uploader is not returning a phpuploadfile object?
     
    Here is how I instantiate the uploader:
                      <form id="form1" method="POST">
                            <?php
                               $uploader=new PhpUploader();
                               $uploader->Name="current_uploader";
                               $uploader->MultipleFilesUpload=true;
                               $uploader->InsertText="Select file(s)";
                               //$uploader->MaxSizeKB=1024000;
                               $uploader->AllowedFileExtensions="*.jpg,*.png,*.gif,*.bmp,*.txt,*.zip";
                               $uploader->SaveDirectory="files/" . $_SESSION['orgid'];
                               $uploader->FlashUploadMode="Partial";
                               $uploader->Render();
                            ?>
                            </form>
    Here is where I try to rename the file:
       $fileguid=$_POST["current_uploader"];
       if($fileguid)
       {
          echo $fileguid;
          $uploadedfile=$uploader->GetUploadedFile($fileguid);
          echo "myfile=" . $uploadedfile->FileName;
          if($uploadedfile)
          {
             //Rename the uploaded file
             echo "Renaming file";
             $uploadedfile->MoveTo("./".$_SESSION['curTkt']."-".$uploadedfile->FileName);
          }
       }
    The echoes are for debugging.  It echoes the fileguid but then $uploadedfile->FileName is empty.  It never gets to the part that echoes "Renaming file."
     
  •  06-22-2011, 10:09 PM 68098 in reply to 68093

    Re: Unable to get file name of uploaded file

    Hi DBarsri,
     
    Please check the code of example "form-keepingstate.php", it shows you how to get the file name by property "FileName".
     
    Online demo http://phpfileuploader.com/demo/form-keepingstate.php
     
    Regards,
     
    Ken
     
  •  06-23-2011, 9:55 AM 68120 in reply to 68098

    Re: Unable to get file name of uploaded file

    My problem isn't so much getting a filename, it is getting the object that will give me the filename.  I changed my code to match the example but am still getting the same result.  Here is the new code:
     
       $fileguidlist=@$_POST["current_uploader"];
       if($fileguidlist)
       {
          echo $fileguidlist;
          $guidlist=split("/",$fileguidlist);
          foreach($guidlist as $fileguid)
        {
         $mvcfile=$uploader->GetUploadedFile($fileguid);
         if($mvcfile)
         {
    // Never gets here
          }
       }
    }
     
    I'm wondering if $uploader is losing the uploaded files after the post although earlier I tried making it a session variable and that didn't work either.
  •  06-23-2011, 12:22 PM 68122 in reply to 68120

    Re: Unable to get file name of uploaded file

    Hi
     
    Did you check whether $fileguid and $_POST["current_uploader"] have correct value?
     
    You can post your full code here, we will run your code and check it for you.
     
    Thanks for asking
  •  06-23-2011, 1:07 PM 68123 in reply to 68122

    Re: Unable to get file name of uploaded file

    How would I check if the guid is correct?
     
    Thanks
     
    EDIT - I checked the posted guid with the js task guid and they match.  I cannot seem to find the difference between my code and the form-keepingstate.php example.  My source code has a lot of dependencies and it is part of a larger web application with many pages and built-in security so posting the full code is probably not a good idea.  Here is what it looks like right now:
     
       $fileguidlist=@$_POST["current_uploader"];
       if($fileguidlist)
       {
          //echo $fileguidlist;
          $guidlist=split("/",$fileguidlist);
          foreach($guidlist as $fileguid)
        {
           if($uploader)
              echo $fileguid;
         $mvcfile=$uploader->GetUploadedFile($fileguid);
         if($mvcfile)
         {     
                echo "myfile=" . $mvcfile->FileName;
                //Rename the uploaded file
                rename("files/".$_SESSION['orgid']."/".$mvcfile->FileName,$_SESSION['curTkt']."~".$mvcfile->FileName);
             }
          }
       }
    It is still not getting into the if($mvcfile) block
     
    Thanks
  •  06-23-2011, 3:56 PM 68125 in reply to 68123

    Re: Unable to get file name of uploaded file

    Hi DBarsri,
     
    Can you post the creation code of php uploader here?
    It should look like the following:
     
    <?php
        $uploader=new PhpUploader();
        $uploader->MaxSizeKB=10240;
        $uploader->MultipleFilesUpload=true;
        $uploader->Name="myuploader";
        $uploader->InsertText="Select multiple files (Max 10M)";
        $uploader->AllowedFileExtensions="*.jpg,*.png,*.gif,*.bmp,*.txt,*.zip,*.rar";
        $uploader->Render();
     ?>
    Thanks for asking
  •  06-24-2011, 10:51 AM 68139 in reply to 68125

    Re: Unable to get file name of uploaded file

    Here is the creation code:
     
                            <?php
                               $uploader=new PhpUploader();
                               $uploader->Name="current_uploader";
                               $uploader->MultipleFilesUpload=true;
                               $uploader->InsertText="Select file(s)";
                               //$uploader->MaxSizeKB=1024000;
                               $uploader->AllowedFileExtensions="*.jpg,*.png,*.gif,*.bmp,*.txt,*.zip";
                               $uploader->SaveDirectory="files/" . $_SESSION['orgid'];
                               $uploader->FlashUploadMode="Partial";
                               $uploader->Render();
                            ?>
     
    Thanks
  •  06-25-2011, 8:04 AM 68143 in reply to 68139

    Re: Unable to get file name of uploaded file

    Hi DBarsri,
     
    I tried the following code, those two highlighted code are commented, this example works fine on my end, the mvcfle return correct value, please check whether those highlighted code leads to your issue:
     
    <?php require_once "phpuploader/include_phpuploader.php" ?>
    <?php session_start(); ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head> 
    </head>
    <body>
     <div class="demo">
       <form id="form1" method="POST">
        <?php
                               $uploader=new PhpUploader();
                               $uploader->Name="current_uploader";
                               $uploader->MultipleFilesUpload=true;
                               $uploader->InsertText="Select file(s)";                         
                               $uploader->AllowedFileExtensions="*.jpg,*.png,*.gif,*.bmp,*.txt,*.zip";
                               //$uploader->SaveDirectory="files/" . $_SESSION['orgid'];
                               $uploader->FlashUploadMode="Partial";
                               $uploader->Render();
        ?>
    <?php
    $fileguidlist=@$_POST["current_uploader"];
       if($fileguidlist)
       {
          //echo $fileguidlist;
          $guidlist=split("/",$fileguidlist);
          foreach($guidlist as $fileguid)
        {
           if($uploader)
              echo $fileguid;
         $mvcfile=$uploader->GetUploadedFile($fileguid);
         if($mvcfile)
         {     
                echo "myfile=" . $mvcfile->FileName;
                //Rename the uploaded file
                //rename("files/".$_SESSION['orgid']."/".$mvcfile->FileName,$_SESSION['curTkt']."~".$mvcfile->FileName);
             }
          }
       }
    ?> 
        <br /><br />
        <input type='submit' value="Submit Form" />    
       </form>
     </div>
    </body>
    </html>
     
    Thanks for asking
  •  07-25-2011, 4:10 AM 68834 in reply to 68143

    Re: Unable to get file name of uploaded file

    I was looking for this. Thanks for sharing.

    kitchen remodeling south florida |kitchen renovation south florida
  •  09-24-2011, 7:08 AM 70186 in reply to 68834

    Re: Unable to get file name of uploaded file

    Hi
     
    I used your file :
    ---------
    <script type='text/javascript'>
    function CuteWebUI_AjaxUploader_OnTaskComplete(task)
    {
    var div=document.createElement("DIV");
    var link=document.createElement("A");
    link.setAttribute("href","admin/testUploader/myprefix_"+task.FileName);
    link.innerHTML="You have uploaded file : savefiles/myprefix_"+task.FileName;
    link.target="_blank";
    div.appendChild(link);
    document.body.appendChild(div);
    }
    </script>
    ------------
    Could you tell me how can I get FileName and put it in : <?php $fileUploaded = 
    task.FileName;  ?>
    many thanks in advance
    regards
    Attourath 
     
     
     
     
     
  •  09-26-2011, 8:43 AM 70203 in reply to 70186

    Re: Unable to get file name of uploaded file

    Hi attourath,
     
    The line below shows you how to get the upload file name in my example
     
    echo "myfile=" . $mvcfile->FileName; 
     
    Regards,
     
    Ken
  •  02-12-2012, 4:34 AM 72990 in reply to 70186

    Re: Unable to get file name of uploaded file

    simply to put the code in the same page:
     
     
        <?php
            $uploader=new PhpUploader();
            
            $uploader->MultipleFilesUpload=true;
            $uploader->InsertText="Select multiple files (Max 1000M)";
            
            $uploader->MaxSizeKB=1024000;
            $uploader->AllowedFileExtensions="*.jpg,*.png,*.gif,*.bmp,*.txt,*.zip,*.rar";
            
            $uploader->SaveDirectory="../pics/";
            
            $uploader->FlashUploadMode="Partial";
            
            $uploader->Render();
            
        ?>

    <script type='text/javascript'>
        function CuteWebUI_AjaxUploader_OnTaskComplete(task)
        {
            var div=document.createElement("div");
            var table=document.createElement("table");


            var tableRow=document.createElement("tr");
            var tableData=document.createElement("td");
            var tableData1=document.createElement("td");

            var input=document.createElement("input");
            input.setAttribute("type","text");
            input.setAttribute("name", "albumPicComment");

            var link=document.createElement("a");
            link.setAttribute("href","../pics/"+task.FileName);
            link.innerHTML="You have uploaded file : ../pics/"+task.FileName;
            link.target="_blank";
            
            var img=document.createElement("img");
            img.setAttribute("src", "../pics/" + task.FileName);
            img.setAttribute("class", "postBackPics");

            div.appendChild(table);
            table.appendChild(tableRow);
            tableRow.appendChild(tableData);
            tableRow.appendChild(tableData1);
            tableData.appendChild(img);
            tableData1.appendChild(input);
            img.appendChild(link);
            document.body.appendChild(div);
        }
        </script> 
     
     
    Man Pak Hong, Dave

    Man Pak Hong, Dave
View as RSS news feed in XML