简体   繁体   中英

Upload images to MySQL PHP

I'm trying to upload images to mysql, when i try with this code, it seems like it only insert databse, but no image filename uploaded to my server, can i know what went wrong

<form method="post" action="">
    <?php
    include ("setting/connect.php");
    $g = mysql_query("select max(id) from abc");
    while($id=mysql_fetch_array($g))
    {
    ?>
    <input type="text" name="id" value="<?php echo $id[0]+1; ?>" hidden/>
    <?php }
    ?>


    <div class="form-group">
        <label>Image</label>
        <input name="image" type="file">
    </div>

    <div class="form-group input-group">
        <span class="input-group-addon">Title</span>
        <input name="title" type="text" class="form-control" placeholder="">
    </div>

    <center><input class="btn btn-primary" type="submit" name="cmdadd" value="ADD" />
    <button type="button" class="btn btn-danger">BACK</button></center>
    </div>
    <!-- /.panel-body -->
</form>

My php:

<?php   
        $id = $_POST['id'];
        $title= trim($_POST['title']);
    $path = "uploads/"; 
        $tmp_name = $_FILES['image']['tmp_name'];
        $name = $_FILES['image']['name'];
    if(isset($_POST['cmdadd'])){
        if(empty($id)||empty($title))
        {
            echo "<center>Error!</center>";
        }
    if($_FILES["image"]["error"] > 0)
        {
            echo "<font size = '5'><font color=\"#e31919\">Error: NO CHOSEN FILE <br />";
            echo"<p><font size = '5'><font color=\"#e31919\">INSERT TO DATABASE FAILED";

        }


    else{

        move_uploaded_file($tmp_name,$path.$name);
        $file="uploads/".$name;
        include "setting/connect.php";
        mysql_query("SET NAMES 'UTF8'");
        $i = mysql_query("insert into abc values ('".$id."','".$file."','".$title."')");
        if($i==true){
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=danhsachtindang.php">';
                    }
            //if($i==true){
            //header('Location:index.php');
            //exit;
            //mysql_close();
            //}
        }
    }
    ?>

Result from this: when i try to upload eg, picture.jpg, in the mysql table, it only came out "avatars/" on the column and HAVEN'T ANYTHING UPLOADED TO SERVER.

Your form lacks enctype="multipart/form-data" it's required when uploading files.

Modify it to read as:

<form method="post" action="" enctype="multipart/form-data">

Consult the manual: http://php.net/manual/en/function.move-uploaded-file.php

Also make sure that the folder has proper permissions set to write to.

  • Consult chmod on PHP.net for more information on setting folder/files permissions.

Sidenote #1:

Since you're using $path = "uploads/"; am assuming that you are running your code from the root of your server.

If this isn't the case, you will need to adjust it accordingly.

Ie: $path = "../uploads/"; depending on the location of your file's execution.

Just an insight .


Sidenote #2:

Your present code is open to SQL injection . Use prepared statements , or PDO with prepared statements .

mysql_* functions deprecation notice:

http://www.php.net/manual/en/intro.mysql.php

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/ .

Documentation for MySQL can be found at » http://dev.mysql.com/doc/ .


Add error reporting to the top of your file(s) which will help during production testing.

error_reporting(E_ALL);
ini_set('display_errors', 1);

it only came out "avatars/" on the column and HAVEN'T ANYTHING UPLOADED TO SERVER

Are you trying to upload to avatars/ or uploads/ ?

Check that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM