-1

I am wanting to change the name of a file that the user uploads to the 'job_id'.fasta. Right now it just stays as whatever the user called it when uploaded. I also want to keep the extension of .fasta on the file.

This form is from home.php. It is the form that the user uploads the file with

<form enctype="multipart/form-data" action="upload.php" method="POST" class="form-inline">
                    <input type="file" name="fileToUpload" id="fileToUpload" class="form-control"/>
                            <input type="submit" value="upload" name="upload" class="form-control"/>
                            <input type="reset" value="reset" name="reset" class="form-control"/>
            </form>

This file is upload.php. My comments are going to explain my thought process. But if someone could help me finish it off I would greatly appreciate it.

<?php

//get the max job_id from the Job table
$fileID = mysqli_query("SELECT MAX(job_id) FROM Job");

//I have to increment it once because we do not actually insert a new job_id yet.
$fileID = $fileID + 1;

// declare the file path
$target_dir = "uploads/";

//here we are creating the file name. I think I need the change something here
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

//get the file extension
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);


//$target_file = $target_dir . $fileID . "'.'" $FileType;

$uploadOk = 1;
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);


// Allow certain file formats
if($FileType != "fasta" ) {
echo "Sorry, only fasta 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.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
header('Location: blast.php');
?>
pala_
  • 8,901
  • 1
  • 15
  • 32
  • 1
    http://stackoverflow.com/questions/2562851/rename-an-uploaded-file-with-php-but-keep-the-extension Enjoy ... Do some searches before posting. – zeflex May 07 '15 at 04:56

2 Answers2

0

Just rename generate the new filename with extension set the target. Try with -

//get the max job_id from the Job table
$fileID = mysqli_query("SELECT MAX(job_id) FROM Job");

//I have to increment it once because we do not actually insert a new job_id yet.
$fileID = $fileID + 1;

// declare the file path
$target_dir = "uploads/";

//get the file extension
$ext = explode('.', $_FILES["fileToUpload"]["name"]);
$fileName = $fileID.'.'.$ext[count($ext)-1];
//here we are creating the file name. I think I need the change something here
$target_file = $target_dir . $fileName;


//$target_file = $target_dir . $fileID . "'.'" $FileType;

$uploadOk = 1;
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0

Try this very easy approach.

//get the max job_id from the Job table
$fileID = mysqli_query("SELECT MAX(job_id) FROM Job");

//I have to increment it once because we do not actually insert a new job_id yet.
$fileID = $fileID + 1;

// declare the file path
$target_dir = "uploads/";

//here we are creating the file name. 
$file = explode('.', $_FILES["fileToUpload"]["name"]);
$filename = $file[0].time().$file[1];

$target_file = $target_dir . $filename;
Iffi
  • 608
  • 1
  • 7
  • 16