editing image by using input file type

守給你的承諾、 提交于 2019-12-22 14:58:31

问题


I made a simple editing for to edit data in mysql, everything works fine except when I want to edit an input file type image it doesn't work, it doesn't give an error message it just doesn't edit anything and when I remove the input file type image it works. and by editing an image I mean entering a new image the will replace the old image.

here is my code:

<?php

require("db.php");

$id     = $_REQUEST['theId'];
$result = mysql_query("SELECT * FROM table WHERE id  = '$id'");
$test   = mysql_fetch_array($result);

$name   = $test['Name'] ;
$email  = $test['Email'] ;                  
$image  = $test['Image'] ;

if (isset($_POST['submit']))
{   
    $name_save  = $_POST['name'];
    $email_save = $_POST['email'];

    if (isset($_FILES['image']['tmp_name']))
    {
        $file       = $_FILES['image']['tmp_name'];
        $image      = addslashes(file_get_contents($_FILES['image']['tmp_name']));
        $image_name = addslashes($_FILES['image']['name']);

        move_uploaded_file($_FILES["image"]["tmp_name"],"photos/" . $_FILES["image"]["name"]);
        $image_save ="photos/" . $_FILES["image"]["name"];

        mysql_query("UPDATE table SET Name ='$name_save', Email  ='$email_save',Image ='$image_save' WHERE id = '$id'") or die(mysql_error()); 

        header("Location: index.php");
    }
}
?>



<form method="post">
    <table>
        <tr>
            <td>name:</td>
            <td>
                <input type="text" name="name" value="<?php echo $name ?>"/>
            </td>
        </tr>
        <tr>
            <td>email</td>
            <td>
                <input type="text" name="email" value="<?php echo $email ?>"/>
            </td>
        </tr>
        <tr>
            <td>image</td>
            <td>
                <input type="file" name="image" value="<?php echo $image ?>"/>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
                <input type="submit" name="submit" value="submit" />
            </td>
        </tr>
    </table>

回答1:


In form enctype="multipart/form-data" is missing and in your form there is no type="file".

Give the below code and try.

<?php
require("db.php");
$id =$_REQUEST['theId'];

$result = mysql_query("SELECT * FROM table WHERE id  = '$id'");
$test = mysql_fetch_array($result);

$name=$test['Name'] ;
$email= $test['Email'] ;                    
$image=$test['Image'] ;

if(isset($_POST['submit'])){    
$name_save = $_POST['name'];
$email_save = $_POST['email'];
$image_save=$image //Added if image is not chose from the form post

if (isset($_FILES['image']['tmp_name'])) {
$file=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
move_uploaded_file($_FILES["image"]["tmp_name"],"photos/" . $_FILES["image"]["name"]);
$image_save ="photos/" . $_FILES["image"]["name"];
}
mysql_query("UPDATE table SET Name ='$name_save', Email  ='$email_save',Image ='$image_save' WHERE id = '$id'")
or die(mysql_error()); 
header("Location: index.php");      }
?>



<form method="post" enctype="multipart/form-data">
<table>
<tr>
<td>name:</td>
<td><input type="text" name="name" value="<?php echo $name ?>"/></td>
</tr>
<tr>
<td>email</td>
<td><input type="text" name="email" value="<?php echo $email ?>"/></td>
</tr>

<tr>
<td>image</td>
<td><input type="file" name="image" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="submit" /></td>
</tr>
</table>

Moreover you should get the previous image value through sql and update if image is not chose while updating.




回答2:


<tr>
<td>image</td>
<td><input type="file" name="image" ></td>
</tr>



回答3:


You have to use input:type=file element instead of input:type=text in order to handle image file using $_FILES. Or you cannot get the image file. So your if statement returns false and nothing happens.

<form method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td>name:</td>
            <td><input type="text" name="name" value="<?php echo $name ?>"/></td>
        </tr>
        <tr>
            <td>email</td>
            <td><input type="text" name="email" value="<?php echo $email ?>"/></td>
        </tr>

        <tr>
            <td>image</td>
            <td><input type="file" name="image" /></td>
        </tr>
        <tr>
            <td>image preview</td>
            <td><img src="photos/<?php echo $image ?>" /></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="submit" value="submit" /></td>
        </tr>
    </table>
</form>


来源:https://stackoverflow.com/questions/18207155/editing-image-by-using-input-file-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!