Community

Forums » SEPHP Help and Tips

Need help to upload files in a custom method

    • 3 posts
    February 6, 2019 12:03 PM EST

    I have a basic method in a controller file to receive and upload an attachment when a user submits a form (post request).

    The code works perfectly when using it outside of SE. However it does not upload the file when using it within SE. Please find my method at the bottom of this post (I removed checks and initiations to keep it short)...

    Basically, the problem is the call to the PHP library 'move_uploaded_file'. The library gets ignored in my controller/model.

    move_uploaded_file($info['tmp_name'], $targetFile) )

    Just to mention, that I have tried the following already:

    1. I note that the same code is implemented and works perfectly in Core_AdminFilesController.php. 

    2. I have put the code in a Model class and a controller class. The method was successfully called in both ocassion but the line of code with 'move_uploaded_file' was ignore.

    Since the same code works perfectly outside SE, I have come to the conclusion that a setting within SE prevents calling the 'move_uploaded_file' library but I don't know what to tweak to overcome the issue, if my guess is correct.

    Kindly advise how to get around this issue, if you are in the know?

    public function uploadAction() { $path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'public/mymodule'; $info = $_FILES['myfile']; $targetFile = $path . '/' . $info['name']; // Try to move uploaded file if( !move_uploaded_file($info['tmp_name'], $targetFile) ) { $this->view->error = Zend_Registry::get('Zend_Translate')->_("Unable to move file to upload directory."); return; } }
    This post was edited by se-enthusiast at February 8, 2019 1:28 AM EST
    • 192 posts
    February 7, 2019 4:36 AM EST

    hey,

    I was actually testing the same thingy and got the same error..

     

    try this link

    https://stackoverflow.com/questions/18929178/move-uploaded-file-function-is-not-working

     

    SE  does not control that, its based on the hosting providers php.ini settings and the folder permissions.

    I tried it "outside" se folders and got the same error (we run and manage the server ourselves)

     

    The destination directory must exist; move_uploaded_file() will not automatically create it for you.

     

    more info on move_uploaded

    http://php.net/manual/en/function.move-uploaded-file.php

     

    Try using the folder /public_html/temporary instead of /tmp - its meant for that as well as has the correct permissions (777)

    so if your root is /public_html

    use /public_html/temporary

    1. Once a file has been uploaded, it is stored in a temporary directory and information on its size, type, and original and temporary names is saved to the $_FILES array. The temporary file name is then provided to the move_uploaded_file() function, which is used to copy the file from the temporary directory to a new location.

     

     

    hope it helps.

     

     

     


    This post was edited by playmusician at February 7, 2019 5:08 AM EST
    • 3 posts
    February 7, 2019 7:56 AM EST

    @playmusician, many thanks for your time/response.

    To clarify, the directory exists and has appropriate permissions (the folder has full write permission and I also check to set it automatically too). So certainly, permission is not the problem and missing folder is not the problem too.

    In my case, the same code works perfectly when used outside of SE codebase (i.e the same code uploads the posted files successfully).

    The scenario is the same both in my development machine and on the server.

    Please note too that, I am certain of the scenario. Using xdebug, I stepped over the code line by line and get to see that the line with 'move_uploaded_file' gets ignored when used within SE codebase.

    Possible solution that will help me is likely one of the following:

    1. Advise by someone who has used the 'move_uploaded_file' method successully in a standard controller.

    2. If I create an admin controller with the 'uploadAction()' in it, advise how I can instantiate the admin controller from a standard controller, and call it's method. With this option, I suspect there is a security setting within SE codebase that only allows the use of the 'move_uploaded_file' method from standard controller.

    Many thanks,

    Sola

     

     

     

     

     


    This post was edited by se-enthusiast at February 8, 2019 1:25 AM EST
    • 192 posts
    February 7, 2019 9:03 AM EST

     

    Hi sola,

    aaaah , sorry 


    If thats the case, i wouldnt be able to help cause Im not really a coding dude

    (just enough to get things tested or done)

     

    I'm guessing (wildly , that is) as you said the SE libraries cannot be called from an external instantiator

     

    Im not even sure if SE permits that. A 3DP (third party developer or expert would be the go to person, 

    hopefully someone who knows this will chime in...

     

    Sorrry, hope you get it figured out !!!

     

     

     


    This post was edited by playmusician at February 7, 2019 9:09 AM EST
    • 3 posts
    February 8, 2019 1:23 AM EST

    Thanks once more for looking in to this issue, I now found a solution.

    My findings is that Zend Frameworks has played with the file, so there is no need to call 'move_uploaded_file' anymore.

    The link below is the solution that work for me, if anyone also need the answer...

    https://stackoverflow.com/questions/10708149/move-uploaded-file-with-zend-doenst-work