【问题标题】:Why I couldn't get the file path and write into db?为什么我无法获取文件路径并写入数据库?
【发布时间】:2015-01-05 01:54:11
【问题描述】:
$allowedMimeTypes = array( 
'image/jpeg',
'image/png'
);
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ( 500000 < $_FILES["file"]["size"]  ) 
{
    echo"Please provide a smaller file [E/1].";
}
if ( in_array( $_FILES["file"]["type"], $allowedMimeTypes ) ) 
 {  
    $file = $_FILES["file"]["name"];
    $filePath = "../upload/" . $file;    
    move_uploaded_file($_FILES["file"]["tmp_name"],$filePath); 
}
else if(!is_uploaded_file($_FILES["file"]["tmp_name"]))
{
    $file='';
    $filePath='';
}
if(! get_magic_quotes_gpc() )
{
   $name = addslashes ($_POST['name']);
   $path = $filePath='';
}
else
{
   $name = $_POST['name'];
   $path = $filePath='';
}
$sql = "INSERT INTO slides ".
       "(name,path) ".
         "VALUES ".
       "('$name','$path')";
mysql_select_db('emtas');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>

我尝试从表单添加图像。它上传了图像,但我无法将它的路径写入数据库。它还从表格中写入图像的名称。为什么我无法获取路径并写入数据库?

【问题讨论】:

  • 因为你总是将 path 和 filePath 设置为空字符串? $path = $filePath='';
  • 您在 IF() 的 truefalse 路径中有 $path = $filePath='';

标签: php mysql database image upload


【解决方案1】:

在执行查询之前,您始终将 $path$filePath 都设置为空字符串。把你的代码改成这样:

$name = $_POST['name'];
if(!get_magic_quotes_gpc()) {
  $name = addslashes ($name);
}
// execute query
$sql = "INSERT INTO slides ".
       "(name,path) ".
       "VALUES ".
       "('$name','$filePath')";

请注意,如果没有上传文件,则不应运行查询。 由于不推荐使用 mysql 扩展名,因此您应该改用 mysqlipdo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 2016-04-14
    相关资源
    最近更新 更多