php上传电脑本地文件并且存到指定目录,并且存放到数据库怎么个思路?

2024-11-15 16:24:16
推荐回答(2个)
回答1:

if(is_uploaded_file($_FILES['img']['tmp_name'])){
if(move_uploaded_file($_FILES['img']['tmp_name'], $target_name)){
//这就算上传成功了,插入数据库
if (!$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
echo 'Could not connect to mysql';
exit;
}

if (!mysql_select_db('mysql_dbname', $link)) {
echo 'Could not select database';
exit;
}

$sql = 'insert into table_name(id, img_path) values(img_id, $target_name)';
$result = mysql_query($sql, $link);

if (!$result) {
echo "DB Error, could not create table the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}

mysql_free_result($result);
}
else{
echo "上传失败";
}
}

回答2:

上传-移动到本地目录-把路径存放在数据库