在mysql中存储图像路径以便以后使用

时间:2014-11-02 04:16:21

标签: php android mysql

我有一个Android应用程序,它将图像发送到我的Web服务器,图像成功保存在目录中但不保存在路径中(路径应该保存在mysql中)。反正有没有这样做,以便我可以在需要时使用路径恢复图像?

这是我的尝试:)     

$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!";
}

?>;

1 个答案:

答案 0 :(得分:3)

相反,如果将临时路径保存到数据库,请保存最终上传目标:

$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!";