简体   繁体   English

重命名上传的文件(php)

[英]Rename uploaded file (php)

I'm trying to rename a file I'm uploading. 我正在尝试重命名要上传的文件。

I will be uploading a xml or pdf file, and I want it to be in a folder called "files/ orderid /" and the filename should also be orderid .extension 我将上传xml或pdf文件,并且希望将其保存在名为“ files / orderid /”的文件夹中,并且文件名也应为orderid .extension

The file uploads fine, and the folder id created with the correct name, but all the ways I have tried to rename it fails. 文件上传正常,并且使用正确的名称创建了文件夹ID,但我尝试重命名的所有方法均失败。

Below is my code. 下面是我的代码。

// include database connection
 include 'config/database.php';

// get passed parameter value, in this case, the record ID
 $id=isset($_GET['orderid']) ? $_GET['orderid'] : die('FEJL: Ordren kunne ikke findes.');

// page header
 $page_title="Upload pdf og/eller xml fil";
 include_once "layout_head.php";

 echo "Ordrenummeret er ";
 echo $id;
 echo "<br>";

 if($_POST){

mkdir("files/$id");
$target_dir = "files/$id/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);


// Check if file already exists
if (file_exists($target_file)) {
    echo "Filen eksisterer allerede.";
    $uploadOk = 0;
}


// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Filen er for stor.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "xml" && $imageFileType != "pdf") {
    echo "Kun xml og pdf filer kan uploades.";
    $uploadOk = 0;
}


// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Filen blev ikke uploaded.";

// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "Filen ". basename( $_FILES["fileToUpload"]["name"]). " er uploaded.";
    } else {
        echo "Der skete en fejl ved upload, prøv igen.";
    }
}
}
?>

 <!DOCTYPE html>
 <html>
 <body>

 <form action="upload_files.php?orderid=<?php echo htmlspecialchars($id); ?>" method="post" enctype="multipart/form-data">
Vælg xml eller pdf fil:
<input type="file" name="fileToUpload" id="<?php echo htmlspecialchars($id); ?>">
<input type="submit" value="Upload fil" name="submit">
 </form>

 </body>
 </html>

Rename the file as below 重命名文件如下

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$target_file = $target_dir . $id . '.' . $imageFileType;

And then 接着

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);

You set the target file to be the name of the file. 您将目标文件设置为文件名。 You should be setting a different name. 您应该设置一个不同的名称。

Try this: 尝试这个:

$target_dir = "files/$id/";
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$target_file = "{$target_dir}{$id}.{$imageFileType};

Thanks guy, it all helped. 谢谢家伙,这一切都有帮助。

My solution was to make a variable with the extension, and then use that through out the file. 我的解决方案是使用扩展名创建一个变量,然后在整个文件中使用它。

//Check file extension
$path = $_FILES["fileToUpload"]["name"];
$ext = pathinfo($path, PATHINFO_EXTENSION);

and then 接着

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "files/$id/$id.$ext"

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

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