Henk de B
Henk de B

Reputation: 55

PHP: define name for file upload

I have a peace of code that stores profile images in the map "images/profiles" and stores the URL in the database. I want to define the name of the uploaded profile picture to be the $ID of the user. How can I do this?

include("../../core/init.inc.php");

$target = "../images/profiles/";
$target = $target . basename($_FILES['photo']['name']);

$pic = $_FILES['photo']['name'];

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
    echo "The file ". basename($_FILES['photo']['name']). " has been uploaded";
} else {
    echo "ERROR";
}

mysql_query("UPDATE users SET image_url='includes/images/profiles/$pic' WHERE username = '".mysql_real_escape_string($_SESSION['username'])."'");

Now when someone uploads his profile picture (lets call it pf1.png) it saves it as "pf1.png". I want it to be saved like "$ID.png" (.png being a random extension). I want to accomplish this both for the move_upload_file function and updating the 'image_url' database column correctly.

Upvotes: 0

Views: 649

Answers (4)

Meneer Venus
Meneer Venus

Reputation: 1045

try changing

$target = $target . basename($_FILES['photo']['name']);

to:

$filename=$_FILES["file"]["tmp_name"];
$extension=end(explode(".", $filename));
$target = target . $_SESSION["ID"].".".$extension;  

side note: You are not escaping $pic this makes your site vulnerable to sql-injection

Upvotes: 1

Robert Dean Pantino
Robert Dean Pantino

Reputation: 284

you can get the last inserted id and make it as a name of your image/uploaded file

$qry = mysqli_query($dbconnection,"INSERT INTO statements here");
$last_id = mysqli_insert_id($dbconnection);

//$ext= you get the extension of the file uploaded

move_uploaded_file($tmpname,$target.$last_id.$ext);

now if you want it to be accomplished in updating also.

you can always get the ID of the data you want to fetch and make it as a basis in updating.

ex.

$id = $_GET['id'] || $id = $row['id']; //anything depends on how you want it to retrieve

then you can do the query and move_uploaded_file function

$qry = mysqli_query($dbconnection,"UPDATE tblname SET field='$anything' WHERE id = '$id'");
move_uploaded_file($tmpname,$target.$id.$ext);

of course $tmpname will be the basis on what file you have uploaded, $tmpname will be the file you want to move into your desired directory

Upvotes: 0

JiFus
JiFus

Reputation: 968

I don't know how you saved the ID of the user, but to make it easy let's assume you stored the ID in a session.

Then simply change the $target.

$target = $target . $_SESSION['ID'];

Also change your query as follows:

$url = "includes/images/profiles/" . $_SESSION['ID'];

SET image_url="$url"

Note: I don't know why you got an image folder inside an includes folder, but I guess you made that choice for yourself.

Upvotes: 0

cen
cen

Reputation: 2943

According to the example in the documentation you can provide the filename in the destination of move_uploaded_file(). If that fails you can simply rename() the file after saving it.

Upvotes: 1

Related Questions