重命名上传的文件(php) [英] Rename uploaded file (php)

查看:149
本文介绍了重命名上传的文件(php)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重新命名正在上传的文件。


我将上传一个xml或pdf文件,我希望它位于名为files / orderid /的文件夹中,文件名也应该是 orderid .extension



文件上传正常,文件夹id用正确的名字创建,但是我尝试重命名的所有方式失败。

以下是我的代码。


$ $ p $ 包含数据库连接
include'config / database.php';

//获得传递的参数值,在这种情况下,记录ID
$ id = isset($ _ GET ['orderid'])? $ _GET ['orderid']:死('FEJL:Ordren kunne ikke findes。');

//页眉
$ page_title =上传pdf og / eller xml fil;
include_oncelayout_head.php;

回声Ordrenummeret呃;
echo $ id;
echo< br>;

if($ _ POST){

mkdir(files / $ id);
$ target_dir =files / $ id /;
$ target_file = $ target_dir。基名($ _ FILES [ fileToUpload] [ 名称]);
$ uploadOk = 1;
$ imageFileType = pathinfo($ target_file,PATHINFO_EXTENSION);


//检查文件是否已经存在
if(file_exists($ target_file)){
echoFilen eksisterer allerede。;
$ uploadOk = 0;


$ b $ //检查文件大小
if($ _FILES [fileToUpload] [size]> 500000){
echo 锉刀锉刀。
$ uploadOk = 0;
}
//允许某些文件格式
if($ imageFileType!=xml&& $ imageFileType!=pdf){
echoKun xml og pdf filer kan uploades。;
$ uploadOk = 0;



//检查$ uploadOk是否被错误
设置为0 if($ uploadOk == 0){
echoFilen blev ikke上传了。;

//如果一切正常,尝试上传文件
if else {
if(move_uploaded_file($ _ FILES [fileToUpload] [tmp_name],$ target_file) ){
echoFilen。 basename($ _FILES [fileToUpload] [name])。 呃上传。
} else {
echoDer skete en fejl ved upload,prøvigen。;
}
}
}
?>

<!DOCTYPE html>
< html>
< body>

< form action =upload_files.php?orderid =<?php echo htmlspecialchars($ id);?> method =postenctype =multipart / form-data>
Vælgxml eller pdf fil:
< input type =filename =fileToUploadid =<?php echo htmlspecialchars($ id);?>>
< input type =submitvalue =Upload filname =submit>
< / form>

< / body>
< / html>


解决方案


$ b

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

然后

  move_uploaded_file($ _ FILES [fileToUpload] [tmp_name],$ target_file); 


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

The file uploads fine, and the folder id created with the correct name, but all the ways I have tried to rename it fails.

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

这篇关于重命名上传的文件(php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆