0

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>';  
  } 
}
Eric7777777
  • 111
  • 7
  • Does this answer your question? [php upload images and save file name to mysql](https://stackoverflow.com/questions/48903811/php-upload-images-and-save-file-name-to-mysql) – nbk May 30 '21 at 21:48
  • @nbk No, because I use RedBean PHP and saving to the database there is completely different. – Eric7777777 May 30 '21 at 21:51
  • Redbean can use raw sql if everything else fails, the rest is following the given examples and expand a little https://stackoverflow.com/questions/18416321/batch-insert-into-mysql-by-redbean and following the basics https://redbeanphp.com/index.php?p=/quick_tour – nbk May 30 '21 at 22:01
  • @nbk I need a code for my example, I already found these links but did not understand how to use them in my case. PHP I use recently, usually write on C ++ – Eric7777777 May 30 '21 at 22:05
  • after checking ´some sites, the create a bean with dispense then add the information and the stires it. of the datatables doesn't exits it will create it, seem striht forward to me – nbk May 30 '21 at 22:15
  • @nbk I don't understand without the code. – Eric7777777 May 30 '21 at 22:31
  • look at the answer https://stackoverflow.com/a/36206776/5193536 and then try to adept it, it will work – nbk May 31 '21 at 10:27
  • @nbk Thanks, but it didn't help. I have the names of the photos can be 10 and 50 pieces and they need to be written in the Foreach loop how to enter data separately in the RedBean table I know and so. – Eric7777777 May 31 '21 at 15:39
  • redbeans doesn't to support prepared stements, https://www.cs.wcupa.edu/rkline/wp/redbean-orm.html so create a bean for every entry or create a insert like the link, as i said when everything fails use raw sql – nbk May 31 '21 at 15:48
  • @nbk Can you please provide the code, how would you do it yourself? – Eric7777777 May 31 '21 at 15:51
  • i have shown you two methis one as rwa sql and one with a beans, as you are learningtry fiorast th beans solution linked 5 hours ago and when tat is to slow switch to the Sel INSERT INTO .... – nbk May 31 '21 at 16:24
  • @nbk Tried like this. But it's not working. Error - Uncaught RedBeanPHP\RedException: Invalid type: sell_photo My code: if(move_uploaded_file($tmp_name, "$uploads_dir/$name")) { R::freeze(false); /* SAVE names of photos */ $photo = R::dispense('sell_photo'); $photo->photo = $name $photo->user_id = $id; // $photo->user_id = ; R::storeAll($photo); } – Eric7777777 May 31 '21 at 16:39

1 Answers1

0

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";
}
lemon
  • 14,875
  • 6
  • 18
  • 38
Eric7777777
  • 111
  • 7