2011-10-11 105 views
1

我试图实现提交系统,人们可以在其中填写个人信息并附加文件上传。他们的信息应该被记录到MySQL数据库中,并且该文件应该被重命名为其中一个字段,然后是扩展名。以下是我迄今为止..将PHP重命名为字段条目

这是表单页面:

<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很陌生,但是我已经做了一些关于这个问题的研究,虽然我没有找到任何适合这个需求的东西。

+0

为什么你不只是更新'$ target'? –

+0

你是什么意思?对不起,这真是新鲜事。 –

+0

看起来你已经在你的应用程序中写入了一些[SQL Injection](http://en.wikipedia.org/wiki/SQL_injection)漏洞。我建议学习如何使用[PHP的准备语句](http://php.net/manual/en/pdo.prepared-statements.php),这使得编写数据库代码对您和您的用户来说更安全。另外,你可能写了一个[目录遍历漏洞](http://en.wikipedia.org/wiki/Directory_traversal_attack),因为在这里我没有看到任何文件名消毒。 (也许它在'move_uploaded_file()'?) – sarnold

回答

1

只需使用目标文件名的值更新$target变量即可。例如,更改

$target = "uploads/"; 
$target = $target . basename($_FILES['upload']['name']); 
if(move_uploaded_file($_FILES['upload']['tmp_name'], $target)) 

$extension = explode(".", $_FILES['upload']['name']); 
$extension = $extension[count($extension)-1]; 
$target = "uploads/"; 
$target = $target . $_POST['snumber'] . "." . $extension; 
if(move_uploaded_file($_FILES['upload']['tmp_name'], $target)) 
+0

完美!有效。 P.S.还有一个问题涉及到整个系统[这里](http://stackoverflow.com/questions/7509132/need-php-upload-form-to-sort-files-into-sub-directory-based-upon-落下)。任何输入也赞赏这个问题! –