-1

I would like to change a pictures name while uploading it.

Here is my code:

if(isset($_POST['submit']))
{
    $target_dir = "pictures/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

    // Allow certain file formats
    if($imageFileType != "gif" ) {
        echo "Sorry only 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 "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
            header('Location:field_medal_winner.php');
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
}

First I tried to do it whith changing the move_uploaded_file function. But I just got the else message:

if (move_uploaded_file("text.gif", $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}

Does anyone have an Idea what I am doing wrong?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • you're outputting before header - check it http://php.net/manual/en/function.error-reporting.php and the form's unknown. *"But I just got the else message"* - because there are errors. – Funk Forty Niner Dec 01 '16 at 15:17
  • Possible duplicate of [How to fix “Headers already sent” error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Funk Forty Niner Dec 01 '16 at 15:18
  • 1
    Possible duplicate of [How to rename uploaded file before saving it into a directory?](http://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-into-a-directory) – Funk Forty Niner Dec 01 '16 at 15:20

1 Answers1

0

Replace these 2 lines

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

With

$target_file = $target_dir . "text.gif";
$imageFileType = pathinfo($_FILES["fileToUpload"]["name"],PATHINFO_EXTENSION);

It will save as text.gif in your desired folder. But, keep in mind, whatever file you will upload, It will override old text.gif file to text.gif as file with same name is uploaded.

Updated Code

if(isset($_POST['submit']))
{
  $target_dir = "pictures/";
  $target_file = $target_dir . "text.gif";
  $uploadOk = 1;
  $imageFileType = pathinfo($_FILES["fileToUpload"]["name"],PATHINFO_EXTENSION);

  // Allow certain file formats
  if(($imageFileType != "gif") || ($imageFileType != "GIF")) {
    echo "Sorry only 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.";
  } else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
      echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
      header('Location:field_medal_winner.php');
    } else {
      echo "Sorry, there was an error uploading your file.";
    }
  }
}
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • @AaronWerlen : Please don't forget to mark this answer as correct answer. As, it will help other user to find this answer easily. If you don't know how to mark answer as appropriate one, then, check this link. http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Nana Partykar Dec 07 '16 at 09:32