【问题标题】:How to change the name of a uploaded file to 'job_id'.extension? [duplicate]如何将上传文件的名称更改为 'job_id'.extension? [复制]
【发布时间】:2015-07-17 12:05:25
【问题描述】:

我想更改用户上传到“job_id”.fasta 的文件的名称。现在它只是保持用户在上传时调用它的任何内容。我还想在文件中保留 .fasta 的扩展名。

此表单来自 home.php。它是用户上传文件的形式

<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。我的 cmets 将解释我的思考过程。但如果有人能帮我完成它,我将不胜感激。

<?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');
?>

【问题讨论】:

标签: php mysql database file primary-key


【解决方案1】:

只需重命名生成带有扩展名的新文件名设置目标。试试 -

//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;

【讨论】:

    【解决方案2】:

    试试这个非常简单的方法。

    //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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 1970-01-01
      相关资源
      最近更新 更多