如何将文件名更改为主键ID?

时间:2015-05-06 04:40:05

标签: php html mysql file

所以我有一个程序,用户将文本输入到文本框中,然后将该文本创建到文件中。每次按下提交按钮,数据库都会创建一个作业并获得一个jobID。我想知道是否有任何方法使文件的名称为job_id.fasta而不是仅仅称为new.fasta。这样我就可以存储更多文件!提前致谢。我将发布创建文件的代码并存储它。

<form method="POST", action="/~cs4380sp15grp4/home/blast.php" class="form-inline">
<textarea id="BlastSearch" type="textarea" name="BlastSearch" class="form-control" placeholder="Protein sequence in fasta format"></textarea>
<button id="run" type="submit" name="submit" class="btn btn-primary"><span class="glyphicon glyphicon-wrench"></span> Run BLAST</button>
</form>

<?php
    session_start();



    require_once '../secure/database.php';
    $mysqli = new mysqli($dbhost,$dbuser,$dbpass,$dbname);

    if($mysqli->connect_error){
            exit('CON Error: ' . $mysqli->connect_errno . ' ' . $mysqli->connect_error);
    }

    //Insert the values into the database

    if(isset($_POST['submit'])){


            $sequence = $_POST['BlastSearch'];

            //create a new .fasta file and put the sequence the user wants to search for in that file
            $file = 'uploads/new.fasta';
            $current = $sequence;
            file_put_contents($file, $current);
?>

2 个答案:

答案 0 :(得分:1)

您可以在创建新文件之前简单地在数据库中插入记录,并使用job_id.fasta获取job_id以生成文件名<?php session_start(); require_once '../secure/database.php'; $mysqli = new mysqli($dbhost,$dbuser,$dbpass,$dbname); if($mysqli->connect_error){ exit('CON Error: ' . $mysqli->connect_errno . ' ' . $mysqli->connect_error); } //Insert the values into the database if(isset($_POST['submit'])){ $sequence = $_POST['BlastSearch']; $query = "INSERT INTO `jobs` (`BlastSearch`) VALUES ('".$sequence."')"; $mysqli->query($query); $job_id = $mysqli->insert_id; //create a new .fasta file and put the sequence the user wants to search for in that file $file = "uploads/".$job_id.".fasta"; $current = $sequence; file_put_contents($file, $current); } ?>

promisifyAll

答案 1 :(得分:0)

假设job_id是自动插入的ID,您可以使用last_id http://php.net/manual/en/mysqli.insert-id.php

//Insert the values into the database

// Fictitious MySQL
$sql = "INSERT INTO search (keyword) VALUES ('mysearch')";
$last_id = "";

if ($mysqli->query($sql) === TRUE) {
  $last_id = $mysqli->insert_id;
} else {
  echo "Error: " . $sql . "<br>" . $mysqli->error;
  die();
}
// End fictitious MySQL

if(isset($_POST['submit'])){
  $sequence = $_POST['BlastSearch'];

  //create a new .fasta file and put the sequence the user wants to search for in that file
  $file = 'uploads/'.$last_id.'.fasta';  // < Use insert_id here.
  $current = $sequence;
  file_put_contents($file, $current);