0

I am trying to upload files using php to directory that is not in the same directory that the upload script is, here is the structure of dir's:

var>www>html>
            foo>index.php //this ones where upload is done
            bar>          //this is where I want to upload it

the index.php file's form element:

<form enctype="multipart/form-data" method="POST" action="index.php">
            <fieldset style='width:33%;'>
                <legend>UPLOAD PROJECT FILES</legend>
                <select name="dirList" id="dirList" onChange="getSubDir(this.value)" required>
                    <option value="">SELECT PROJECT</option>
                    <?php
                        $path = "../";
                        $scan = scandir($path);
                        foreach($scan as $result){
                            if($result === '.' or $result === '..') continue;
                            if(is_dir($path . '/' . $result)){
                                echo "<option value='../$result'>$result</option>";
                            }
                        }
                    ?>
                </select>
                <select name="subDirList" id="subDirList">
                    <option value="">SELECT PATH</option>
                </select>
                <input type="file" name="filePath"/>
                <input type="submit" name = "upload" value="UPLOAD"/>
            </fieldset>
        </form>

so the idea here is to get the list of folders from first combobox which will get me the list of subdirectories in the second combobox.

and then the php function at the head of index.php for uploading the file:

if(isset($_POST['upload'])){
            $upload_path = $_POST['subDirList'];
            if($upload_path == ''){
                $upload_path = $_POST['dirList'];
            }
            $upload_path = getcwd().'/'.$upload_path;
            $upload_path = $upload_path.'/'.basename($_FILES['filePath']['name']);              
            if(move_uploaded_file($_FILES["filePath"]["tmp_name"], $upload_path)){
                echo "success";
            } else {
                echo print_r($_FILES);
                echo 'fail';
            }

        }

the error log:

[Wed Sep 23 14:10:46.841250 2015] [:error] [pid 4028] [client 78.167.168.253:52438] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpEB1a3s' to '/var/www/html/eShop/Capture.PNG' in /var/www/html/ftp/index.php on line 16, referer: http://xxx.xx.xx.xx/ftp/index.php
mrahmat
  • 617
  • 6
  • 21
  • Check this link : http://www.w3schools.com/php/php_file_upload.asp – Fky Sep 23 '15 at 15:28
  • @saurabh2836 http://php.net/manual/en/function.getcwd.php ;) – Fky Sep 23 '15 at 15:30
  • What does the response state? `fail` or `success`? Check the php Log, on the server machine, to be sure that there are no errors there. Also, can you post the `index.php` form as well? Finally, I suggest using `isset($_FILES["submit"]) && $_FILES["submit"]["error"]== UPLOAD_ERR_OK)` as well. – Bonatti Sep 23 '15 at 17:01
  • @Bonatti added the rest of code now, it should be bit more clear as to what Im trying to get done – mrahmat Sep 23 '15 at 17:46
  • @mrahmat Before the `if(move_uploaded_file...)` function, echo the `$upload_path` and post in your question. I think you may not have writte permissions there. By default, most servers can only read www and below. `getcwd()`should be outside of that... check the result of `echo __FILE__ `below it as well – Bonatti Sep 23 '15 at 18:16
  • @mrahmat Also [check the folder writte permissions](http://stackoverflow.com/questions/13723174/php-warning-move-uploaded-file-unable-to-move) – Bonatti Sep 23 '15 at 18:18
  • @Bonatti thx, the problem was with permissions, its working now I used chmod 777 for now for quick solution but I will fix it later with correct permissions, can you write an answer so I can accept it and close the question? – mrahmat Sep 24 '15 at 10:25

1 Answers1

0

Check if you have writte permissions.

Then, by documentation also check if the file name is valid, and remeber the warning:

Warning If the destination file already exists, it will be overwritten.

As a suggestion, always check that :

isset($_FILES["FileInput"]) && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK)

is true before continuing.

Community
  • 1
  • 1
Bonatti
  • 2,778
  • 5
  • 23
  • 42