phpmysqlfileredbean

How to save all generated unique names of downloaded photos to the MySQL database in Redbean PHP?


How to save all generated unique names of downloaded photos to the MySQL database in Redbean PHP

Just started learning the Redbean PHP library.

Here is my code.

I can not understand how to do it, I studied the Redbean documentation but failed to find the necessary one.

    require_once"Rb.php";
     require_once"Db.php";

 $data = $_POST;
 $photo = $_FILES;
    
    if(isset($data['submit']))       { 
     

      /* Uploads Folder */
      

           $uploads_dir = '../Images';
      
      
      $source = $uploads_dir;
      
foreach ($photo["file"]["error"] as $key => $error) {
  
    if ($error == UPLOAD_ERR_OK) {
      
       $original_name = $photo["file"]["name"][$key];

      
         $tmp_name = $photo["file"]["tmp_name"][$key];
      
      
           $size = $photo["file"]["size"][$key];
  
      
    // Get extension 
     
      
         $ext = pathinfo($original_name, PATHINFO_EXTENSION);
     
 
      
      /* New names */

      
          $token = random_bytes(7);   
       
     $p = '.'; 
      
          $name =  bin2hex(microtime() . $token) . ($photo["file"][$key])  . $p . $ext;
    
      
      /* Upload Photos  */
      
           
                    move_uploaded_file($tmp_name, "$uploads_dir/$name");         
}


  /*Show DATA */ 
  
   echo'<pre>';
      print_r($data);
   echo'</pre>';
  
      
       echo'<pre>';
         print_r($photo);
       echo'</pre>';  
  } 
}

Solution

  • For anyone who uses RedBean PHP, never name tables with capital letters or underscores, this will throw an error. I renamed the table and everything worked out.

    // Upload Photos 
    if(move_uploaded_file($tmp_name, "$uploads_dir/$name"))   {
        // SAVE names of photos
        $image = R::dispense('photorent');
        $image->photo = $name;
        R::store($image);
        echo "Success";
    }