0

I am using this code to upload photo to server:

<?php

$currentName = $_SESSION["uID"];

// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
    // Check if file was uploaded without errors
    if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
        $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
        $filename = $_FILES["photo"]["name"];
        $filetype = $_FILES["photo"]["type"];
        $filesize = $_FILES["photo"]["size"];
    
        // Verify file extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
    
        // Verify file size - 5MB maximum
        $maxsize = 5 * 1024 * 1024;
        if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
    
        // Verify MYME type of the file
        if(in_array($filetype, $allowed)){
            move_uploaded_file($_FILES["photo"]["tmp_name"], "img/profile_pictures/" . $filename);
            // Add code to redirect back
            
            header("location:".$_SERVER['HTTP_REFERER']);
            exit;
            
        } else{
            echo "Error: There was a problem uploading your file. Please try again."; 
        }
    } else{
        echo "Error: " . $_FILES["photo"]["error"];
    }
}
?>

Now when the user uploads, the file name inside img/profile_pictures is the same name as the file choosen, but I want to change the name to the value from $currentName. I have tried replacing $filename with $currentName, but it does not work. How can i accomplish this?

I tried changing $filename, but then extension is gone?:

move_uploaded_file($_FILES["photo"]["tmp_name"], "img/profile_pictures/" . $currentName);
Erik Auranaune
  • 1,384
  • 1
  • 12
  • 27
  • you can't change the name before uploading as Javascript cannot modify the filesystem as such. – Professor Abronsius Jul 30 '20 at 13:01
  • Just add the extension too? `move_uploaded_file($_FILES["photo"]["tmp_name"], "img/profile_pictures/" . $currentName.'.'.$ext);` – Qirel Jul 30 '20 at 13:02
  • @ProfessorAbronsius What javascript? This is PHP. – Qirel Jul 30 '20 at 13:05
  • You're being too literal ;-) if you look at the question, it's about changing the name when processing it in PHP, before its moved from the temporary request to storage on the server. – Qirel Jul 30 '20 at 13:08

2 Answers2

0

For the extension you can use the following:

move_uploaded_file($_FILES["photo"]["tmp_name"], "img/profile_pictures/" . $currentName.$ext);

$_FILES["photo"]["type"] is the extension of the file you want to upload.

Or you set a full new name for example:

$newname = "newfile.png";
move_uploaded_file($_FILES["photo"]["tmp_name"], "img/profile_pictures/" . $newname);
Lukas
  • 305
  • 2
  • 11
0

You missed add extension. If you are use $_FILES["photo"]["type"] for extension, you will get error. So :

if($_FILES['photo']['type'] == "image/jpeg"){
   $ext= ".jpg";
}else{
   $ext= ".png";
}
...
move_uploaded_file($_FILES["photo"]["tmp_name"], "img/profile_pictures/" . $currentName.$ext);

My recommended for image name :

$currentName = uniqid().$ext;
xNoJustice
  • 516
  • 5
  • 8