0

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?

2
  • 2
    As there is the risk of name collision, I would not use a human given name as the file name but an unique identifier in instead using the uuid-ossp module. Commented May 13, 2013 at 17:07
  • @ClodoaldoNeto thank you for your advice. I am purely testing out code at the moment, the user will enter their own filename on the mobile application and it will be assigned a unique id. Commented May 13, 2013 at 17:37

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

14 Comments

thanks but still hasn't worked. Could you provide a code edit with writing the uploaded and decoded image to the $filepath variable?
@DanielD Is the filePath variable being properly stored in your data-base? Also are you stating that you are unable to save the file onto your website file-system?
the filePath variable is NOT being written to my database. The image test.jpg appears in my chosen directory but is of 0kb size and the PostGreSQL database remains blank.
@DanielD Alright since you are unable to store that variable properly I assume you are not connected properly... Are you able to store other variables in your database on each upload attempt?
If I comment out the code concerning the filepath/image then yes the textual data is inserted successfully.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.