Move jpg file from /ploads dir to database table

Users input jpg files to an existing db table after using a resize function. Sometimes the images are reversed so I found code to rotate jpg’s with SimpleImage. The rotated jpg’s are saved to the same /uploads folder as the original jpg’s. I want to move the jpg from the /uploads folder to the db table. I can get the code to move all the existing table data with $_SESSION but not the jpg data.

Here is code for rotating the jpg
<?php

session_start();

$image_name = $_SESSION['image_name'];
	
$file = '//WDP/DFS/30/1/0/8/3067916801/user/sites/4232274.site/www/uploads/';
$angle = $_POST['Radio1'];

include_once '//WDP/DFS/30/1/0/8/3067916801/user/sites/4232274.site/www/require/SimpleImage.php';

// Ignore notices
error_reporting(E_ALL & ~E_NOTICE);

try {
// Create a new SimpleImage object
$image = new claviska\SimpleImage();

// Manipulate it
$image
->fromFile($file.$image_name) // load .jpg
->autoOrient() // adjust orientation based on exif data
->bestFit(300, 600) // proportinoally resize to fit inside a 300 x 600 box
->flip($angle) // flip
->toFile($image_name) // output to file
->toScreen(); // output to the screen

} catch(Exception $err) {
// Handle errors
echo $err->getMessage();

}

?>

Here is code snippet which gets original jpg to the db table just fine. Like I said, I need to get the jpg now from the /uploads folder to the same db table to replace that original jpg. In other words, the source for the rotated jpg is not the website user but the /uploads folder. If only I could have the user search the /uploads folder for the jpg file through a browse field. Or, is there another solution? I’ve come up with two, but much less elegant solutions.

Well, normally, you would never store images in a database. That could cause problems as the table grows and grows over time. Normally, you save the image inside a folder and just store a link to it in the database. This depends a lot how you handle filenames. If you keep images based on users, you can store them in folders using the user’s ID from your user’s table. And, when they register, you can create a folder for them in your images/user-id/ folder structure. Then, since you always know the currently logged in user, including their user-id, you can access their images without a lot of database overhead.

Also, PHP already has built-in graphics functions for resizing. You can use a library as you posted, but, why? Here is the link to the built-in graphics functions available with most PHP systems: PHP-GD

Sponsor our Newsletter | Privacy Policy | Terms of Service