繁体   English   中英

如何获取添加到目录中的最后一个文件的名称

[英]How to get the name of the last file added to a directory

我有一个程序,它可以获取用户输入并将其更改为文件,或者允许用户上传文件。 我在从用户处获取上传的文件时遇到问题。 现在,我已经为上载的示例文件sample.fasta进行了硬编码。 我希望能够获得用户上传的文件的名称,然后使用该文件的名称调用我的程序。

我将发布有关此问题的所有相关代码。

此页面称为blast.php

<?php
if(isset($_POST['submit2'])){
            //echo "submit2";
    //      echo $_FILES['uploadedfile']['name'];

     //declare variables to what the user defines them as
            $db = $_POST['database'];
            $evalue = $_POST['evalue'];
            $sequence = $_POST['BlastSearch'];
            $hits = $_POST['hits'];
            $userid = $_SESSION['uid'];

            //insert the values into the database
            $mysqli->query("INSERT INTO `Job` (`uid`, `input`, `status`, `start_time`, `finish_time`) VALUES ('1', 'used a file', 'running' , NOW(), NOW())");

            $mysqli->query("INSERT INTO `BLAST`(`db_name`, `evalue`, `job_id`) VALUES ('" . $db . "','" . $evalue . "', '".$mysqli->insert_id."')") or die(mysqli_error($mysqli));

            //need to change the name of sample.fasta to whatever file uploaded
            exec('/students/groups/cs4380sp15grp4/blast/blast-2.2.26/bin/blastall -p blastp -d db -i /students/groups/cs4380sp15grp4/public_html/home/uploads/sample.fasta -m '.$evalue.' -o outputFILE -v '.$hits.' -b '.$hits);
?>
          <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>

该文件称为upload.php,是我用来上传文件的表单。

 <?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$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: http://babbage.cs.missouri.edu/~cs4380sp15grp4/home/blast.php');
?>

因此,基本上在我的exec函数中,而不是读取sample.fasta,我需要读取用户上传的文件...

您可以简单地将表单发布回包含表单的页面,然后该表单将具有您上载的文件名和位置,以执行您需要对表单进行的任何其他操作。

为此,您可以检查是否在PHP文件的开头设置了帖子值,并像在upload.php文件中一样进行处理。

将您的upload.php最后一行更改为:

header('Location: http://babbage.cs.missouri.edu/~cs4380sp15grp4/home/blast.php?file='.urlencode($target_file));

并在blast.php检查$_GET

if(isset($_GET['file'])){
    $fastaFile = $_GET['file']
....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM