-1

I cant figure out how to rename a filename inside the file upload script that I have.

Any help is really appreciated!!

Below is the script I am working on.

<?php
$target_dir = "../user_profile_pictures/";
$target_file = $target_dir . basename($_FILES["profile_pic"]["name"]);
$uploadOk = 1;
$imageFileType  = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
    echo "Lo siento, el archivo ya existe.";
    $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "jpeg" && $imageFileType !=     "png") {
    echo "Lo siento, solo se permiten imagenes tipo jpg, jpeg o png.";
$uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Lo siento, el imagen no se ha subido.";

    // if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $target_file)) {
        echo "El imagen ". basename( $_FILES["profile_pic"]["name"]). " se ha subido.";
    } else {
        echo "Ha habido un error al subir el archivo.";
    }
}
?>
adenis82
  • 35
  • 8
  • 3
    Similar question you can find it here: http://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-into-a-directory – Khalil Malki Jul 17 '16 at 22:55

3 Answers3

1

Try this, I have added a newname variable to your original code.

<?php
$target_dir = "../user_profile_pictures/";
$target_file = $target_dir . basename($_FILES["profile_pic"]["name"]);
$newname = "chosen-name";
$uploadOk = 1;
$imageFileType  = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "Lo siento, el archivo ya existe.";
$uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "jpeg" && $imageFileType !=     "png") {
echo "Lo siento, solo se permiten imagenes tipo jpg, jpeg o png.";
$uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Lo siento, el imagen no se ha subido.";

// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["profile_pic"]["tmp_name"],  $target_dir.$newname)) {
    echo "El imagen ". basename( $_FILES["profile_pic"]["name"]). " se ha subido.";
} else {
    echo "Ha habido un error al subir el archivo.";
}
}
?>
Angie
  • 140
  • 1
  • 8
0

in move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $target_file) Replace $target_file with the directory and the new file.

move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $directory.$newname.'.'.$extension)
Matteo Enna
  • 1,285
  • 1
  • 15
  • 36
0

use rename() just like this

else {
rename($target_file,'newname.txt');
    if (move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $target_file)) {
        echo "El imagen ". basename( $_FILES["profile_pic"]["name"]). " se ha subido.";
    } else {
        echo "Ha habido un error al subir el archivo.";
    }
Ahmad ghoneim
  • 844
  • 7
  • 13