PHP图像上传并将您的路径插入数据库

时间:2013-10-17 14:03:21

标签: php file-upload sql-insert

我有文件输入错误。没错,图像被移动到服务器的文件夹中 - isset($ _ FILES ['avatar']),没问题 - 但是user_avatar的字段没有填写你的名字。它在数据库中以“default_130x130.png”命名。如何将“$ user_avatar”变量从upload_image.php发送到new_user.php?


烯/用户/ new.html

    <form id="avatar_file_upload_form" action="../../utils/upload_image.php" method="post" enctype='multipart/form-data'style="position:absolute;z-index:-10;height:1px;width:1px;overflow:hidden;visibility:hidden;">
        <input type="file" name="avatar" id="avatar_file_upload_field" accept="image/jpeg,image/pjpeg,image/bmp,image/gif,image/jpeg,image/png"/>
        <input type="submit" />
    </form>

utils的/ upload_image.php

<?php
ob_start();
$mimeExt = array();
$mimeExt['image/jpeg'] ='.jpg';
$mimeExt['image/pjpeg'] ='.jpg';
$mimeExt['image/bmp'] ='.bmp';
$mimeExt['image/gif'] ='.gif';
$mimeExt['image/x-icon'] ='.ico';
$mimeExt['image/png'] ='.png'; 
if(isset($_FILES["avatar"])) { 
     //Begins image upload
        $user_avatar = md5(uniqid(time())).$mimeExt[$_FILES["avatar"]["type"]]; //Get image extension
        $user_avatar_dir = "../img/".$user_avatar; //Path file
        move_uploaded_file($_FILES["avatar"]["tmp_name"], $user_avatar_dir); 

} else {
    $user_avatar = "default_130x130.png";
}
?>

new_user.php

<?php
ob_start();
include "config.php";
include "utils/upload_image.php";
$sql = mysql_query("insert into user(user_avatar) values('$user_avatar')", $db_connection) or die("Error: ".mysql_Error());
ob_end_clean();
mysql_close($db_connection);
?>

注意:“config.php”文件工作正常。

2 个答案:

答案 0 :(得分:0)

我认为您应该将表单的操作属性设置为new_user.php。现在,您将其路由到upload_image.php,我不知道如何在此之后加载new_user.php。当没有文件发送到该页面时,您实际上在upload_image.php中包括new_user.php

这应该是你的HTML:

<form id="avatar_file_upload_form" action="new_user.php" method="post" enctype='multipart/form-data'style="position:absolute;z-index:-10;height:1px;width:1px;overflow:hidden;visibility:hidden;">
    <input type="file" name="avatar" id="avatar_file_upload_field" accept="image/jpeg,image/pjpeg,image/bmp,image/gif,image/jpeg,image/png"/>
    <input type="submit" />
</form>

这应该是您的upload_image.php

<?php
$mimeExt = array();
$mimeExt['image/jpeg'] ='.jpg';
$mimeExt['image/pjpeg'] ='.jpg';
$mimeExt['image/bmp'] ='.bmp';
$mimeExt['image/gif'] ='.gif';
$mimeExt['image/x-icon'] ='.ico';
$mimeExt['image/png'] ='.png'; 
if(isset($_FILES["avatar"])) { 
     //Begins image upload
        $user_avatar = md5(uniqid(time())).$mimeExt[$_FILES["avatar"]["type"]]; //Get image extension
        $user_avatar_dir = "../img/".$user_avatar; //Path file
        move_uploaded_file($_FILES["avatar"]["tmp_name"], $user_avatar_dir); 

} else {
    $user_avatar = "default_130x130.png";
}
?>

这应该是您的new_user.php

<?php
ob_start();
include "config.php";
include "utils/upload_image.php";
$sql = mysql_query("insert into user(user_avatar) values('$user_avatar')", $db_connection) or die("Error: ".mysql_Error());
ob_end_clean();
mysql_close($db_connection);
?>

答案 1 :(得分:0)

表单操作是utils/upload_image.php,代码和流程表示您需要将表单操作设置为new_user.php文件。

此外,我不确定为什么插入查询在向utils/upload_image.php页面提交后仍然有效,utils/upload_image.phpnew_user.php之间没有任何关系。您的插入查询在这种情况下不应该工作

更新:

  

我怎么能做真插入呢?

将表单转到new_user.php,更改action="../../new_user.php"

<form id="avatar_file_upload_form" action="../../new_user.php" method="post" enctype='multipart/form-data'style="...">

</form>