简体   繁体   English

PHP使用文本名称制作目录并重命名上传文件

[英]Php make dir using text name and rename upload file

Html sample code: HTML示例代码:

<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
Company Name:<input type="text" name="company"/><br />
ClientName: <input type="text" name="ClientName"/><br />
<br />
<input type="hidden" name="MAX_FILE_SIZE" value="5120000" />
 Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
</body>

Sample Php code: 样本PHP代码:

<?php

$file_name = $_POST['company'];
$random_digit = rand(0000,9999);
$new_file_name = $random_digit.$file_name;

$uploaddir = 'c:/temp/'.$new_file_name;
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}

echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
echo '['.$new_file_name.']';
print "</pre>";
?> 

I am having problem with the rename. 我在重命名时遇到问题。 It renames the file but it keeps the original name too. 它重命名文件,但也保留原始名称。 So if I upload a file name joey.doc, it changes to random# = 4567, company name = whatever, file name = joey.doc output = 4567whateverjoey.doc. 因此,如果我上载文件名joey.doc,它将更改为random#= 4567,公司名称=任意,文件名= joey.doc输出= 4567whateverjoey.doc。 What I am looking for is for the file name to change to the random number append the company only = 4567whatever.doc. 我要寻找的是将文件名更改为随机数,仅在公司后面添加= 4567whatever.doc。 Later I will write code to create company name directory store all file from that company in their directory. 稍后,我将编写代码以创建公司名称目录,并将该公司的所有文件存储在其目录中。 Store the location tag in xml then pull up a table with to reference. 将位置标签存储在xml中,然后提取一个表以供参考。 the information. 信息。 Plus I use the client name input box to decode what file go with what. 另外,我使用客户端名称输入框来解码什么文件随何而去。

If writing xml is like asp.net C# I should have no problem 如果编写xml就像asp.net C#,我应该没问题

You are appending the old name again: 您将再次附加旧名称:

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

If you only want to append the extension have a look at pathinfo() 如果只想添加扩展名,请查看pathinfo()

change this line: 更改此行:

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

in particular just do some string manipulation on the basename(...) portion. 特别是只需对basename(...)部分进行一些字符串操作。 Basically, all you care about is the extension...and depending on your filename restrictions and desired robustness the answer could be easy, or a little bit tricky. 基本上,您只关心扩展名...并且根据您的文件名限制和所需的健壮性,答案可能很简单,也可能有些棘手。

one example (so you get the idea): 一个例子(这样你就知道了):

$uploadfile = $uploaddir . '.' . split('.',basename($_FILES['userfile']['name']))[1];

or use built-in function Pathinfo 或使用内置函数Pathinfo

In the line: 在该行中:

$uploaddir = 'c:/temp/'.$new_file_name;

You add the new file name to the directory it's to be located in. 您将新文件名添加到要位于的目录中。
But in this line: 但在这一行:

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

You also append the old name it was uploaded with to that string. 您还可以将其上载的旧名称附加到该字符串中。 You should do what @TimWolla suggested and find out about pathinfo() if you want to find just the extension. 如果您只想查找扩展名,则应执行@TimWolla建议的操作并查找有关pathinfo()的信息。

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

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