重命名 PHP 上传到字段条目
我正在尝试实现一个提交系统,人们可以在其中填写个人信息并附加上传的文件。它们的信息应记录到 MySQL 数据库中,并且该文件应重命名为其中一个字段,然后是扩展名。这是我到目前为止所拥有的..
这是表单页面:
<html>
<head>
<title>Submissions Script DEV</title>
<style type="text/css">
/**** Start page styles ****/
body {
background: #DFA01B;
font-family: arial, sans-serif;
font-size: 14px;
}
#wrap {
max-width: 600px;
margin: 30px auto;
background: #fff;
border: 4px solid #FFD16F;
-moz-border-radius: 15px;
-webkit-border-radius: 15px;
border-radius: 15px;
padding: 20px;
}
</style>
<body>
<div id="wrap">
<div align="center">
<a href="http://siteurl.com"><img src="logo.png" /> </a>
<h1 style="font-family:arial">Submissions</h1>
<p style="font-family:helvetica"> <i> Welcome to the submission page</i> </p>
</div>
<form method="POST" action="upload.php" enctype="multipart/form-data">
<p>
Please enter your first name: <input type="text" name = "fname">
<p>
Please enter your last name: <input type="text" name= "lname">
<p>
Student #: <input type="text" name= "snumber"/>
<p>
Grade: <select name= "grade">
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
<p>
<hr>
<!---Upload file section begins--->
Please attach your Powerpoint (ppt/pptx/zip) file. The file should be named student#.zip. (For example; 123456.ppt)
</p>
<input type="hidden" name="size" value="1024000">
<input type="file" name="upload">
<p>
<br/>
<br/>
<div align="center">
<input type=button onClick="location.href='instructions.html'" value='Back'>
<input TYPE="submit" name="upload" title="Send your submission" value="Submit Portfolio"/>
</div>
</form>
<p>
<!---Footer Styling--->
<div style="font-family: Arial;
font-size: 10px;
color: grey;
align: center;">
<!---Footer Contents--->
<p>Copyright <a href="http://sitename.com">site</a>site</p>
</div>
</div>
</body>
</html>
这是 upload.php:
<?php
//This is the directory where images will be saved
$target = "uploads/";
$target = $target . basename( $_FILES['upload']['name']);
//This gets all the other information from the form
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$upload=($_FILES['upload']['name']);
$snumber=$_POST['snumber'];
$grade=$_POST['grade'];
// Connects to your Database
mysql_connect("localhost", "db_user", "dbuserpass") or die(mysql_error()) ;
mysql_select_db("sub-data") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `Submissions` VALUES ('$fname', '$lname', '$snumber', '$grade', '$upload')") ;
//Writes the upload to the server
if(move_uploaded_file($_FILES['upload']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been recorded";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
最后,我希望使用表单中的 snumber 字段来命名文件上传。例如,如果有人在 snumber 字段中填写 12345 并上传 .zip 文件,则该文件应自动重命名为 12345.zip 并存储在适当的目录中。
我该怎么做呢?我对 PHP 很陌生,但我已经做了一些与这个问题相关的研究,尽管我还没有找到任何适合这种需求的东西。
I'm trying to implement a submission system where people can fill out personal information and attach a file upload. Their information should be recorded into a MySQL database and the file should be renamed to one of the fields and then the extension. Here is what I have so far..
This is the form page:
<html>
<head>
<title>Submissions Script DEV</title>
<style type="text/css">
/**** Start page styles ****/
body {
background: #DFA01B;
font-family: arial, sans-serif;
font-size: 14px;
}
#wrap {
max-width: 600px;
margin: 30px auto;
background: #fff;
border: 4px solid #FFD16F;
-moz-border-radius: 15px;
-webkit-border-radius: 15px;
border-radius: 15px;
padding: 20px;
}
</style>
<body>
<div id="wrap">
<div align="center">
<a href="http://siteurl.com"><img src="logo.png" /> </a>
<h1 style="font-family:arial">Submissions</h1>
<p style="font-family:helvetica"> <i> Welcome to the submission page</i> </p>
</div>
<form method="POST" action="upload.php" enctype="multipart/form-data">
<p>
Please enter your first name: <input type="text" name = "fname">
<p>
Please enter your last name: <input type="text" name= "lname">
<p>
Student #: <input type="text" name= "snumber"/>
<p>
Grade: <select name= "grade">
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
<p>
<hr>
<!---Upload file section begins--->
Please attach your Powerpoint (ppt/pptx/zip) file. The file should be named student#.zip. (For example; 123456.ppt)
</p>
<input type="hidden" name="size" value="1024000">
<input type="file" name="upload">
<p>
<br/>
<br/>
<div align="center">
<input type=button onClick="location.href='instructions.html'" value='Back'>
<input TYPE="submit" name="upload" title="Send your submission" value="Submit Portfolio"/>
</div>
</form>
<p>
<!---Footer Styling--->
<div style="font-family: Arial;
font-size: 10px;
color: grey;
align: center;">
<!---Footer Contents--->
<p>Copyright <a href="http://sitename.com">site</a>site</p>
</div>
</div>
</body>
</html>
This is the upload.php:
<?php
//This is the directory where images will be saved
$target = "uploads/";
$target = $target . basename( $_FILES['upload']['name']);
//This gets all the other information from the form
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$upload=($_FILES['upload']['name']);
$snumber=$_POST['snumber'];
$grade=$_POST['grade'];
// Connects to your Database
mysql_connect("localhost", "db_user", "dbuserpass") or die(mysql_error()) ;
mysql_select_db("sub-data") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `Submissions` VALUES ('$fname', '$lname', '$snumber', '$grade', '$upload')") ;
//Writes the upload to the server
if(move_uploaded_file($_FILES['upload']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been recorded";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
In the end, I hope to use the snumber field from the form to name the file upload. For example, if someone fills out 12345 in the snumber field and uploads a .zip file, it should automatically be renamed to 12345.zip and stored in the appropriate directory.
How would I go about doing this? I'm very new to PHP but I have done some research pertaining to this problem although I haven't found anything that suits this need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用目标文件名的值更新
$target
变量即可。例如,更改为
Just update the
$target
variable with the value of the destination file name. For example, changeto