mayce
mayce

Reputation: 95

File upload in PHP not working?

My file upload system just isn't working properly, it doesn't return true when I call move_uploaded_file(). Here is my code, not sure if i'm just blind:

HTML:

<form action="updatecheat.php" method="POST">
                            <label for="version">Version:</label>
                            <input type="text" class="form-control" id="version" name="version" placeholder="New Version Number" />
                            <input type="hidden" name="referrer" id="referrer" value="coven-updates.php" />
                            <div class="custom-file-upload">
<label for="covenupdate">Upload "Coven.exe"</label>
<input type="file" id="covenupdate" name="covenupdate" />
</div>

<br /><br />

<button type="submit" name="submit" id="submit" class="btn btn-success waves-effect waves-light m-r-10">Update</button>
</form>

PHP:

$errors= array();
          $file_name = $_FILES['covenupdate']['name'];
          $file_size =$_FILES['covenupdate']['size'];
          $file_tmp =$_FILES['covenupdate']['tmp_name'];
          $file_type=$_FILES['covenupdate']['type'];



if(move_uploaded_file($file_tmp,"../Coven/Utilites/Update/".$file_name)){
                $fn = "../Coven/Utilities/Version.txt"; 
            $file = fopen($fn, "w+"); 
            $size = filesize($fn); 

            fwrite($file, $_POST['version']); 

            $text = fread($file, $size); 
            fclose($file);

            header("Location: urlhere");

             } else {
                header("Location: urlhere");
             }

I have no clue why it isn't uploading properly. Any help is appreciated! Thanks!

Upvotes: 0

Views: 65

Answers (3)

Rp9
Rp9

Reputation: 1963

Try with add attribute enctype='multipart/form-data' and

File will be stored in temporary location, use tmp_name instead of name

if(move_uploaded_file($file_tmp,"../Coven/Utilites/Update/".$file_name)){

Upvotes: 0

Hetal Chauhan
Hetal Chauhan

Reputation: 807

With your form tag you are missing one attribute enctype='multipart/form-data' which is must while you are working with upload file.

So, just change your form tag to this:

<form action="updatecheat.php" method="POST" enctype='multipart/form-data'>

Upvotes: 3

AmalJo
AmalJo

Reputation: 106

you need to include enctype for a file ulpload to work .i.e enctype="multipart/form-data"

Upvotes: 1

Related Questions