Click here to Skip to main content
15,886,530 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
here is the code
i hope this is enough information because there is not much to tell
<?php include('server.php') ?>

  <?php
  session_start();
    if (!isset($_SESSION['username'])) {
    $_SESSION['msg'] = "You must log in first";
    header('location: Login.php');
  }

  $username = $_SESSION['username'];
  ?>

<!DOCTYPE html>
<html>
<head>
  <title>Upload - wfsecs-kingdom</title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

        <div class="navbar">
    <a class='btn' href="../main.php">Main</a>
    <a class='btn' href="../Credits/">Credits</a>
    <a class='btn' href="../Chat/">Chat</a>
    <a class='btn' href="index.php">Profile</a>

  </div>
  <div id="content">
  
<form action="upload.php" method="post" enctype="multipart/form-data">
  <style type="text/css">
    color:white ;
  </style>
    <label>Upload Image</label>
    <input type="file" name='myfile'>
    <br/>
    <input type="submit" value="upload" name="submit">
  </form>

      </body>
    </html>

     <?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["myfile"]["tmp_name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["myfile"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    upload();
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists. ";
  $uploadOk = 0;
}

// Check file size
if ($_FILES["myfile"]["size"] > 1000000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
  echo "Sorry, only JPG, JPEG, PNG files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  upload();
  function upload() {
  if (move_uploaded_file($_FILES["myfile"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["myfile"]["tmp_name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
  }
}
?>
it just doesn't work the file i'm trying to upload doesnt appear in /uploads/ its png file and 1kb

What I have tried:

i tried re learning file uploading from w3schools and i tried adding functions but didnt work i even readed the whole code line by line
Posted
Updated 5-Feb-22 9:56am
Comments
Richard MacCutchan 5-Feb-22 7:58am    
You need to gather more information, as it is impossible to guess what is happening. Add some logging code to your source to try to find out what is going on.

1 solution

$target_file

is an unknown variable inside function upload() unless you declare it "global".

:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900