Sunday, 3 March 2019

File upload with new name (PHP)

This post will help you to upload an image file to server with new name . File name will be database auto increment field value . So no name conflict will be arise.

function SaveSliderImage($file)
{

$stmt = $this->conn->stmt_init();
if ($stmt->prepare("insert into  ueks98_si_jsj_os9 (id,id1,id2)   values(NULL,?,?)"))
{
//Get File Extension
$tmpstr=strtolower($_FILES[$file]['name']);
$extnsn=end(explode(".",$tmpstr));


$stmt->bind_param("ss", $a1, $a2);
$a1 = $extnsn;
$a2 =  date("Y-m-d");
$stmt->execute() or die("<h1>Error slider image</h1>".mysqli_error($this->conn));  

$last_id = mysqli_insert_id($this->conn);
$file_name=$last_id . "." . $extnsn;
$stmt->close();
move_uploaded_file($_FILES[$file]['tmp_name'],"../../images/slider/". $file_name);
return true;
}
else
{
return false;
}
}

HTML


<form method="post" enctype="multipart/form-data">
     <input type="file"  id="file" name="file" accept="image/x-png,image/jpeg" />
     <input type="submit" name="submit"  value="Join" >

</form>

No comments:

Post a Comment