将行ID添加到上载文件的文件名中

时间:2014-07-13 08:50:02

标签: php html

我想将行的ID号添加到上传的文件文件名中。 例如如果文件名在上传之前是stack.pdf,则在上传之后应该更改为stack-ID#.pdf。

这是用于上传的PHP代码

$sp=mysqli_connect("localhost","root","","ara");
         if($sp->connect_errno){
                echo "Error <br/>".$sp->error;
}

$path="pdf/";

if(isset($_POST['upload']))
{

$path=$path.$_FILES['file_upload']['name'];

if(move_uploaded_file($_FILES['file_upload']['tmp_name'],$path))
{
echo " ".basename($_FILES['file_upload']['name'])." has been uploaded<br/>";
echo '<img src="gallery/'.$_FILES['file_upload']['name'].'" width="48" height="48"/>';
$img=$_FILES['file_upload']['name'];
    $query="insert into library (path,CreatedTime) values('$img',now())";
    if($sp->query($query)){
     echo "<br/>Inserted to DB also";    
    }else{
        echo "Error <br/>".$sp->error;        
    }
}
else
{
echo "There is an error,please retry or ckeck path";
}
}

这是表格

<form action="accept-file.php" method="post" enctype="multipart/form-data">
<table width="384" border="1" align="center">
<tr>
<td width="108">Select File</td>
<td width="260"><label>
<input type="file" name="file_upload">
</label></td>
</tr>
<tr>
<td><label>
<input type="submit" name="upload" value="Upload File">
</label></td>
<td>&nbsp;</td>
</tr>
</table>
</form>

我将非常感谢你的帮助。感谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

$uploadFileName = $_FILES['file_upload']['name'];


//get extention of upload file
$attachment_ext =  explode('.', $uploadFileName);
$ext_pt = $attachment_ext[1];

//Give a new name for the file
$newName = '123'.$uploadFileName.".".$ext_pt;

$path = "YOURPATHHERE/";

$save_attchment = $path.$newName ; //setting the path

move_uploaded_file($_FILES['file_upload']['tmp_name'], $save_attchment);