0

我有下面的代码,我希望在上传到用户 ID 时更改/重命名图像名称,这样我就可以避免文件覆盖并将名称插入数据库,在我添加重命名代码后,该代码无法上传图像或更新数据库我们显示任何错误,但如果我删除重命名代码一切正常。有人可以帮我解决它还是有更好的方法可以解决它?

<?php
$user_id = htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');
$username = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
require("connection.php");
if(@$_POST ['submit']) {
    $file = $_FILES ['file'];
    $name1 = $file ['name'];
    $type = $file ['type'];
    $size = $file ['size'];
    $tmppath = $file ['tmp_name'];
    if($type == 'jpeg' || $type == 'png' || $type == 'jpg') {
        $name1 = $user_id.$type; // rename image
        if($name1!="") {
            if(move_uploaded_file ($tmppath, 'users/'.$name1)) {
                $sql=("INSERT INTO USERS set photo='$name1' WHERE username='$username'");
                mysql_query ($sql) or die ('could not updated:'.mysql_error());
                echo ("Profile picture updated");
            }
        }
    } 
}
?>
4

4 回答 4

1

你可以试试这个,可能对你有帮助...

<?php
$user_id = htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');
$username = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
require("connection.php");
if(@$_POST ['submit']) {
$file = $_FILES ['file'];
$name1 = time().$file ['name']; // rename image
$type = $file ['type'];
$size = $file ['size'];
$tmppath = $file ['tmp_name'];
if($type == 'image/jpeg' || $type == 'image/png' || $type == 'image/jpg') {
    if($name1!="") {
        if(move_uploaded_file ($tmppath, 'users/'.$name1)) {
            $sql=("INSERT INTO USERS set photo='$name1' WHERE username='$username'");
            mysql_query ($sql) or die ('could not updated:'.mysql_error());
            echo ("Profile picture updated");
        }
    }
} 
}}
?>
于 2013-05-15T14:06:15.230 回答
0

试试这个把你$_FILES的数组重新组织成一个你可以理解并轻松使用的数组。

无耻的插头

https://gist.github.com/lukeoliff/5531772#file-quickrearrangefiles-php

<?php
  function rearrangeFiles($arr) {
    foreach($arr as $key => $all){
      foreach($all as $i => $val){
        $new[$i][$key] = $val;
      }
    }
    return $new;
  }

像这样使用:

<?php
  $user_id = htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');
  $username = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');

  require("connection.php");

  if(!empty($_POST) && !empty($_FILES)) {
    $files = rearrangeFiles($_FILES)
    foreach ($files as $key => $file) {
      $name    = $file['name'];
      $type    = $file['type'];
      $size    = $file['size'];
      $tmppath = $file['tmp_name'];
      if($type == 'jpeg' || $type == 'png' || $type == 'jpg') {
        $name = time() . '_' . $user_id.'_'.$name.'.'.$type; // TIMESTAMP, USERID and FILENAME RENAME
        if(!empty($name)) {
          if(move_uploaded_file($tmppath, 'users/'.$name)) {
            $sql = "INSERT INTO users (photo,username) values ('$name','$username')";
            mysql_query($sql) or die('could not updated:'.mysql_error());
            $successes[] = $file['name'] . " picture saved as " . $name;
          }
        }
      } 
    }
    if (!empty($successes)) {
      echo implode('. ',$successes);
    }
  }

通过在单个查询中插入数据库进一步改进:) 此外,您确实需要根据 php.net http://www.php.net/manual/en/function.mysql从 mysql_ 函数移动到 mysqli_ 或 PDO:: 函数-connect.php很快就会贬低 mysql_ 功能。

于 2013-05-15T14:05:59.557 回答
0

首先改变 $name1 = $user_id.$type;

$name1 = $user_id.".".$type; 其次,清理你的 sql。

还。file_type 是 image/jpeg 所以这就是它不起作用的原因。它永远不会超过你的如果。

创建一个开关来检查文件类型或只取文件的最后 3 个字符。

于 2013-05-15T13:54:43.433 回答
-1

您可以使用该概念,但根据您的要求对其进行编辑。

<?php
if ($_FILES['imagepath']['name'] != "")
{
    $uploaddir = 'images/';
    $uploadfile = $uploaddir . basename($_FILES['imagepath']['name']);
    if (move_uploaded_file($_FILES['imagepath']['tmp_name'], $uploadfile))
    {
    $rename = $_FILES['imagepath']['name'];
                $rename = rand(0,1500000000).$rename;
                $filename = strtolower(($rename));
                if (file_exists(($uploaddir.$_FILES['imagepath']['name'])))
                    rename(($uploaddir.$_FILES['imagepath']['name']), ($uploaddir.$filename));
    echo $_FILES['imagepath']['name']."&nbsp;with name&nbsp;".$filename."&nbsp;file uploaded successfully";
    }
}
?>
于 2014-06-30T05:44:47.050 回答