2

I really checked everything and went through all the examples but didn't find solution. Basically what I'm trying to do is to simply upload XML file and store it to the specific folder.The folder is created and I'm using XAMPP localhost.

My code looks like this:

HTML

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" enctype="multipart/form-data">
    <div class="row">
        <div class="form-group col-lg-4 col-md-4 col-xs-4 col-sm-4">
            <input type="file" name="fileToUpload" id="fileToUpload" accept="text/xml">
        </div>
    </div>
    <div class="row">
        <div class="col-lg-4 col-md-4 col-xs-4 col-sm-4">
            <button type="submit" class="btn btn-default" id="uploadFile" name="uploadFile" style="width: 100%">Upload</button>
        </div>
    </div>
</form>

PHP

<?php
if (isset($_POST['uploadFile'])) {
    $name = $_FILES["fileToUpload"]["name"];
    $tmp_name = $_FILES['fileToUpload']['tmp_name'];
    $location = 'uploads/';
    move_uploaded_file($tmp_name, $location.$name);
//also tried this -> copy($tmp_name, $location.$name);
} 
?>

Thanks in advance,

B.

bkfloyd94
  • 29
  • 5
  • try to change button element to input type="submit". I was also getting this issue but after change it is working – B. Desai Feb 04 '17 at 04:19
  • @Xorifelse I used that htmlspecialchars in some previous project so I just copied it.. But in this case it doesn't change the output string so it's not the problem (I tested it). I checked whether my folder is writeable and it returned true so it's ok. The thing is that IF condition from the example never occurs (condition is never TRUE)... I added header('Location: http://www.google.com/'); just as a test inside that IF block and it never occurs. B. Desai I tried that before and now again, doesnt change anything. – bkfloyd94 Feb 04 '17 at 04:19

3 Answers3

0

If you're trying to check for an uploaded file, that's not the way to go. Use something like is_uploaded_file:

if(is_uploaded_file($_FILES['fileToUpload']['tmp_name'])) { # note the tmp_name, don't change this
    # handling goes here
}

Your code doesn't work because you're checking for a parameter that does not have a value (since you never specify the value attribute for your button). Additionally, it's not recommended to pass values in a button; if you need this type of behaviour (in this case, you don't) then use an <input type="hidden" name="your-name" value="your-value" />.

jake2389
  • 1,166
  • 8
  • 22
  • Thanks for the answer. Unfortunately is_uploaded_file($_FILES['fileToUpload']['tmp_name']) throws an exception -> Undefined index... – bkfloyd94 Feb 04 '17 at 12:55
  • If that's the case, then you're either not showing the whole code, or your action is getting messed up... try putting the whole URL instead of the `$_SERVER['PHP_SELF']`, so for example `http://localhost/path/to/your/file.php` and see if it works – jake2389 Feb 04 '17 at 18:36
  • Check my answer above.. None of these help. It's something related to XAMPP/server. – bkfloyd94 Feb 06 '17 at 02:10
0

So after 2 days of agony I still didn't find a concrete solution to this problem but at least I figured out it's nothing realted to PHP/HTML code but it's something with server. To connect to local server I use XAMPP.

Firstly, I tried to reinstall it (to same path - D disk partition) - Didn't change anything.

Secondly, I tried installing it on C disk - Still nothing

Thirdly, I installed WAMP instead and it still doesn't work. I tested my code on other server and it worked like a charm.

Conclusion: server doesn't even recognize that form was submitted (that IF condition is never true).

If somebody knows which part of XAMPP or any other AMP has to do anything with it, please let me know. I will really appreciate it.

Thanks in advance

bkfloyd94
  • 29
  • 5
0

[FIXED] So, FINALLY.. In the end as it uses to be.. The most stupid thing cause the biggest mess.

Anyway, I'm using PhpStorm as my IDE and it has its own built-in server. Even tho I did set my parameters (php interpreter and run configuraton), it happens that PhpStorm was automatically using another server and not mine one (I should've paid attention to port earlier, but ye..). Why was this happening in a first place is because xampp's document root directoy wasn't the one I've stored my project in.

You can do 2 things:

  1. If using XAMPP, store your project at htdocs or
  2. Stop your Apache and MySql, go to Apache config -> Apache (httpd.conff) -> find DocumentRoot "C:/xampp/htdocs" & "Directory "C:/xampp/htdocs"

and replace "C:/xampp/htdocs" with your own path including project folder itself and start Apache and MySql again. NOTE that if you don't include the main project folder but you include its parent folder then your path while running the code will be localhost:port/projectMainFolder/others

As for me, you can lock this topic since I found a solution. Once again thanks everybody for jumping in to help!

bkfloyd94
  • 29
  • 5