WebDevDanno
WebDevDanno

Reputation: 1122

Insert image file path into PostgreSQL database

I have a mobile application that sends some data to my PHP web service. The data consists of some textual items and a base64 encoded string (which will be decoded into an image).

I want to store the textual data into some data columns and then a final text column containing the file path to the uploaded image. I have a directory called usersImages/, which is where I would like to store all images received by the user, but when I upload the data, my PHP script currently doesn't insert anything into the database.

PHP file:

$conn = pg_connect("database_credentials");
/* GET DATA */
$name = $_POST['name'];
$s_name = pg_escape_string($name);
$description = $_POST['desc'];  
$s_desc = pg_escape_string($description);
$latitude = $_POST['lat'];
$longitude = $_POST['lng'];
$project = $_POST['project'];

$encoded_photo = $_POST['snap'];
echo $encoded_photo;
$photo = base64_decode($encoded_photo);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('usersImages/test.jpg', 'wb');
fwrite($file, $photo);
fclose($file);

$res = pg_query("INSERT INTO records (name, description, latitude, longitude, project, imagepath) VALUES ('$s_name', '$s_desc', '$latitude', '$longitude', '$project', '$file')");

What I want to do:

I want all the data to be stored as text with the final column imagepath containing the path to the image I have just uploaded, but I'm having no success with this at the moment. Any ideas?

Upvotes: 0

Views: 5490

Answers (1)

Devon Bernard
Devon Bernard

Reputation: 2300

I believe the problem you are running into is for the "imagepath" column you are storing the $file variable which is fopen('usersImages/test.jpg', 'wb');. You cannot store a function call into a column cell, instead you should presumably be storing a data-type that would suit your needs (since you want to store a path-string a varchar should work fine).

What I assume you would like to do is just store the path for instance:

$filePath = "usersImages/test.jpg";
$res = pg_query("INSERT INTO records (name, description, latitude, longitude, project, imagepath) VALUES ('$s_name', '$s_desc', '$latitude', '$longitude', '$project', '$filePath')");

(As long as you are properly connected to your database this method should work)

Might I even further suggest that you store just the variable name to save space and hardcode the variables that are always the same.... for instance if the folder is always the same and the image type is always jpg you can just store $filePath = "test"; and change your loading code with the hard-coded variables and not just a database query.

As for your other question for actually uploading the image to your website, that is difficult to tell you an exact answer because you did not provide the relevant code; but it looks like you are getting a false file due to an improper uploading technique. You should try something like this instead of the fopen,fwrite,fclose method.

<input type="file" name="file" id="file"> // In your form (seperate from PHP)
$filePath = "userImages/test.jpg";  //setting file path
$success = move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $filePath); //actually storing the file into your file-system into the selected path

Upvotes: 1

Related Questions