【问题标题】:php file path/file extension not saving to database in sqlphp文件路径/文件扩展名未保存到sql中的数据库
【发布时间】:2012-11-18 10:41:26
【问题描述】:

我成功地将图像加扰并上传到我的数据库,但是文件路径没有保存到我的 sql 数据库中的“配置文件”选项卡中。这里有什么问题?我如何解决它。

include 'core/init.php';

function change_profile_image($user_id, $file_temp, $file_extn) {
$file_path = 'profile/' . substr (md5(time()), 0, 10) . '.' . $file_extn;
move_uploaded_file($file_temp, $file_path);
mysql_query("UPDATE `users` SET `profile` = " . $file_path . "' WHERE `user_id` = " . (int)$user_id);

}

 if (isset($_FILES['profile']) === true) {
    if (empty($_FILES['profile']['name']) === true) {
       echo 'y u no choose file!';
  } else {
       $allowed = array ('jpg', 'jpeg', 'gif', 'png');

       $file_name = $_FILES['profile']['name'];
       $file_extn = strtolower(end(explode ('.', $file_name)));
       $file_temp = $_FILES['profile']['tmp_name'];

       if (in_array($file_extn, $allowed) === true) {
        change_profile_image($session_user_id, $file_temp, $file_extn);

        header('Location: dontdelete.php');
        exit();

       }else {
        echo 'y u no jpg or png or gif';       

       }
  }
 }

if (empty($user_data['profile']) === false) {
    echo '<img src"', $user_data['profile'], '" alt="">'; 
}


?>

【问题讨论】:

  • 为什么只选择 md5 哈希的前 10 个字符(40 位数据而不是 128 位)?你为什么要使用md5?如果两个上传同时完成会发生什么?
  • 我不确定我只是觉得 10 个字符更漂亮。
  • 是的,但无论如何没人会查看文件名,这会增加名称冲突的可能性。我只会使用用户 ID 作为文件名。

标签: php sql image filepath


【解决方案1】:

你的代码行:

mysql_query("UPDATE `users` SET `profile` = " . $file_path . "' WHERE `user_id` = " . (int)$user_id);

看看

`profile` = " . $file_path . "'

你忘记了$file_path开头的';)

【讨论】:

  • 天啊,你 php。我希望这能解决它。我现在就给它一个漩涡!
  • hmmm 似乎不是问题所在。我把“.$file_path.”'改为'”.$file_path.“'
  • @KiaDull 嗯,这很奇怪,你能在后面加一个var_dump(mysql_error());吗?
  • 我得到了什么 bool(true) 警告:无法修改标头信息 - 标头已由
  • 我修复了标题问题...仍然没有修复文件路径
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 2017-09-25
  • 1970-01-01
相关资源
最近更新 更多