Cara mengupload file di PHP

Cara mengupload file di PHP

Cara mengupload file di PHP

Cara mengupload file di PHP, Untuk mengupload file di MySQL terlebih dahulu kita harus membuat tabel di database.

Di sini kami menggunakan 2 file untuk mengupload file:

  • index.php HTML form yang memungkinkan pengguna untuk memilih file yang ingin mereka upload.
  • upload.php Untuk mengolah data pengguna dan mengupload file.

Form upload file

<!DOCTYPE html>

<html>

<head>

<title>File Upload</title>

</head>

<body>

<form action="upload.php" method="post" enctype="multipart/form-data">

<input type="file" name="file" />

<button type="submit" name="upload">upload</button>

</form>

</body>

</html>

<?php

include_once 'database.php';

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

{




$file = rand(1000,100000)."-".$_FILES['file']['name'];

$file_loc = $_FILES['file']['tmp_name'];

$file_size = $_FILES['file']['size'];

$file_type = $_FILES['file']['type'];

$folder="upload/";




/* new file size in KB */

$new_size = $file_size/1024;

/* new file size in KB */




/* make file name in lower case */

$new_file_name = strtolower($file);

/* make file name in lower case */




$final_file=str_replace(' ','-',$new_file_name);




if(move_uploaded_file($file_loc,$folder.$final_file))

{

$sql="INSERT INTO image(file,type,size) VALUES('$final_file','$file_type','$new_size')";

mysqli_query($conn,$sql);







echo "File sucessfully upload";







}

else

{




echo "Error.Please try again";




}

}

?>

 

 

Baca Juga  Cara Upload Gambar Di MySQL Menggunakan PHP

Tinggalkan Komentar

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Need Help? Chat with us