sasuri
sasuri

Reputation: 992

Storing image path in mysql to be used later on

I have an android app that sends an image to my web server, the image is saved successfully in the directory but not the path(path should be saved in mysql). Is there anyway to do that so that I can use the path to restore the image when needed?

Here is my try :)

$target_path  = "./images";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);


$pic=($_FILES['photo']['name']); 

$file_path=$_FILES['tmp_name'];

// include db connect class
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/android_connect/db_connect.php');

// connecting to db
$db = new DB_CONNECT();

$result = mysql_query("INSERT INTO scb(photo, name) VALUES('$pic', '$file_path')");// 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
{
echo "The file ".  basename( $_FILES['uploadedfile']['name']).
 " has been uploaded";
} 
else
{
echo "There was an error uploading the file, please try again!";
}

?>;

Upvotes: 2

Views: 289

Answers (1)

Rasclatt
Rasclatt

Reputation: 12505

Instead if saving the temp path to your database, save your final upload destination:

$target_path    =   "./images".basename( $_FILES['uploadedfile']['name']);
$pic            =   ($_FILES['photo']['name']); 
$file_path      =   $_FILES['tmp_name'];

// include db connect class
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/android_connect/db_connect.php');

// connecting to db
$db             =   new DB_CONNECT();

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name'])." has been uploaded";
        // Make your database insert only if successful and insert $target_path, not $file_path
        // Use mysqli_ or PDO with prepared statements
        $result =   mysql_query("INSERT INTO scb(photo, name) VALUES('$pic', '$target_path')");// 
    }
else
    echo "There was an error uploading the file, please try again!";

Upvotes: 3

Related Questions