0

I would like to know if its possible to add title of the article to file name when uploading it ?

Example: I am uploading files with simple code

    $directory = '../images/adds/';
    $temp_file = $directory . basename($_FILES['file']['name']);

    if(move_uploaded_file($_FILES['file']['tmp_name'], $temp_file)) {
        echo "<p style='color:#1dff03;'>file uploaded.</p>";
    }
    else{
        echo "<p style='color:#f00;'>File cant be uploaded.</p>";
    }

I am saving file name in database with title : my-article-name-filename.png but cant upload to server with article title, is it possible to get some help?

  • they are not same questions to me, other two questions are about random naming files but mine is renaming file name with article name, I am not a professinal coder, sorry if I made a mistake. –  Mar 05 '18 at 19:21

1 Answers1

0

You just have to put your desired file name in the $temp_file variable. In the following example I used the $new_name variable in the first line to define it:

$new_name='my-article-filename.png';

$directory = '../images/adds/';
$temp_file = $directory . $new_name;

if(move_uploaded_file($_FILES['file']['tmp_name'], $temp_file)) {
    echo "<p style='color:#1dff03;'>file uploaded.</p>";
}
else{
    echo "<p style='color:#f00;'>File cant be uploaded.</p>";
}
ERap320
  • 36
  • 1
  • 6