0

I am using this function to upload the image:

<?php

    session_start();

if(!isset($_SESSION["user"]) or !is_array($_SESSION["user"]) or empty($_SESSION["user"])) {
      // redirect to login page
}



$target_dir = ('users/'.$_SESSION["user"]["id"].'/');
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image


// Check if file already exists

// Check file size

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF 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 {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo header('Location: main.php');
    }
}
?>

How would I change the file name to: profilepicture for example. As any user that uploads an image, it's uploaded to the users ID file but with the name of there actual image, rather than profilepicture.

1
  • You would change the information about $target_file capturing the file's actual name in the form during the upload. Commented Feb 2, 2015 at 22:45

1 Answer 1

2

You can change the following line:

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$_FILES["fileToUpload"]["name"] is the original filename, so change it to something like:

$target_file = $target_dir . 'profilepicture.'.pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.