Click here to Skip to main content
15,881,801 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "";
// if everything is ok, try to upload file
} else {

  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename("image has been uploaded."));
    rename('../Chat/uploads/'.["fileToUpload"],'../Chat/uploads/'.$username);
  } else {
    echo "";
  }
}
?>
it gets uploaded to the right place but it doesnt get renamed
heres the form code
<form action="upload.php" method="post" class="form-act" enctype="multipart/form-data">
  <style type="text/css">
    color:white ;
  </style>
    <input type="file" class="fileToUpload" name='fileToUpload'>
    <br/>
    <input type="submit" value="upload" class="submit-image" name="submit-image">
  </form>

and this is the full code
<?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>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

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

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

      </body>
    </html>

     <?php
$target_dir = "../Chat/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["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-image"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "";
    $uploadOk = 1;
  } else {
    echo "";
    $uploadOk = 0;
  }
}

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

// Check file size
if ($_FILES["fileToUpload"]["size"] > 1000000) {
  echo "ur file is too large";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
  echo "";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "";
// if everything is ok, try to upload file
} else {

  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename("image has been uploaded."));
    rename('../Chat/uploads/'.["fileToUpload"],'../Chat/uploads/'.$username);
  } else {
    echo "";
  }
}
?>


What I have tried:

searching from stackoverflow asked help from my friend and tried changing stuff in the code
Posted
Updated 6-Feb-22 3:27am

Again, you need to read your code more closely:
PHP
rename('../Chat/uploads/'.["fileToUpload"],'../Chat/uploads/'.$username);

Look at the name you are using as the source file to be renamed, there is no such name. And you should check the result of the rename function; don't just assume that it succeeds.

And rather than repeating expressions like $_FILES["fileToUpload"] multiple times, set the name into a variable at the beginning when you check it for validity.
 
Share this answer
 
I also noticed a very similar set of questions from yesterday by Member 15524347 - Professional Profile[^]. Is that you or someone working with you? If it is you then please close the account that you are no longer using.
 
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