Home > Net >  Converting files to binary code using php and saving it in database
Converting files to binary code using php and saving it in database

Time:11-22

I am creating something like google drive or some cloud using PHP and HTML where you can save your files but i have problem with saving files to binary code and uploading it to database. I mean i want to do something like that:

User is uploading file by form in html -> converting file to binary -> saving it in database. User can download his file by some button or smh like that -> file is converting from binary to file.

I tried with doing a script what will save bin code in my database but when i am trying to send some files i am getting error like that:

Fatal error: Uncaught TypeError: fopen(): Argument #1 ($filename) must be of type string, array given in C:\xampp\htdocs\fileshub\src\send_file.php:12 Stack trace: #0 C:\xampp\htdocs\fileshub\src\send_file.php(12): fopen(Array, 'rb') #1 {main} thrown in C:\xampp\htdocs\fileshub\src\send_file.php on line 12

This is my form in html:

                <form  action="./src/send_file.php" method="post" enctype="multipart/form-data"><br>
                    <input type="text" name="filename" id="filename" placeholder="File name">
                    <input type="file" name="file" id="file"><br>
                    <button type="sumbit" >Submit</button>
                </form>

And this is my script in php:

<?php
session_start();
include "../src/db_conn.php";

if(isset($_SESSION['id'])) {
    
  $id = $_SESSION['id']; // id usera
  $filename = $_POST['filename']; // nazwa pliku

  $file = $_FILES['file'];

  $data = fopen ($file, 'rb');
  $size = filesize ($file);
  $contents = fread ($data, $size);
  fclose ($data);

  $binfile = base64_encode($contents);

  if(!$filename|| !$file) {
    header("Location: ../index.php?error=Enter your data!");
    exit();
  } else {
    $check = "SELECT bin_code FROM files WHERE user_id = '$id' AND bin_code = '$binfile' AND file_name = '$filename'";
    $result = mysqli_query($conn, $check);

    if(mysqli_num_rows($result) === 1){
      header("Location: ../index.php?error=Your file exsist.");
      exit();
    }else {
      $save = "INSERT INTO files (user_id, file_name, bin_code) values('$id', '$filename', $binfile)";
      $saveresult = mysqli_query($conn, $save);
      $saveresult;
      header("Location: ../index.php?error=Your file has been saved");
      exit();
    }
  }
}
?>

db_conn:

<?php 
    $server = "localhost";
    $user ="root";
    $password = "";
    $db = "fileshub";

    $conn = mysqli_connect($server, $user, $password, $db);
?>

If you know any solutions for my problem please help :)

Files table Users table and example user

CodePudding user response:

I believe you know that uploading an image to a directory is a more efficient way to store it.

Please note that $_FILES['file'] is an array containing all sorts of information of the uploaded file, for your case you have to use the filename of the uploaded file. (which is "tmp_name")

So change

 $file = $_FILES['file'];
 $data = fopen ($file, 'rb');

to

 $file = $_FILES['file']["tmp_name"];
 $data = fopen ($file, 'rb');

On the other hand, an alternative way is to use file_get_contents instead of fopen, fread, etc.

$contents = file_get_contents($_FILES['file']["tmp_name"]);

CodePudding user response:

So firstly you need to ensure you have a directory that PHP has access and has permissions on but that is not publicly accessible. Usually, on web hosts the web root folder is a sub folder of the home directory, so you can create a new folder there for file storage. Eg:

/home/myuser/public_html/ <-- might be the web root (some installations differ eg: htdocs or html or httpdocs)
/home/myuser/files/ <-- Create this folder for storing files.

Alternatively, if youre web server is Apache, you can create a folder inside your web root, and protect that folder using a .htaccess file

The easiest way then to store an uploaded file on the file server is to use the move_uploaded_file command that PHP provides, here is how I would achieve this:

$postname = 'file';
$root = '/home/myuser/files';

//cleanse the filename to prevent SQL injections when saving to DB
$filename = preg_replace("/[^a-zA-Z0-9\.]/","_",$_FILES[$postname]['name']);

$path = "$root/$filename";

//Create the files folder if it doesnt already exist...
if(!file_exists($root)) if(!mkdir($root,0775,true)) die("Failed to create root folder $root");

//Store the uploaded file in the files folder
if(!move_uploaded_file($_FILES[$postname]['tmp_name'],$path)) die('Failed to move uploaded file into asset storage');

//Store the file location in the database...
$saveresult = mysqli_query($conn,"INSERT INTO `files` (`filename`,`path`) VALUES ('$filename','$path')");
  • Related