php 使用输入文件类型编辑图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18207155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:10:02  来源:igfitidea点击:

editing image by using input file type

phpmysqlimagefileedit

提问by Mj Jam

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.

我做了一个简单的编辑来编辑 mysql 中的数据,一切正常,除了当我想编辑输入文件类型图像时它不起作用,它没有给出错误消息它只是不编辑任何东西,当我删除它工作的输入文件类型图像。并通过编辑图像我的意思是输入一个新图像将替换旧图像。

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>

回答by Saranya Sadhasivam

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

在表单 enctype="multipart/form-data" 中丢失,并且在您的表单中没有 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.

此外,如果更新时未选择图像,您应该通过 sql 获取先前的图像值并更新。

回答by Parfait

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

回答by zkanoca

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

您必须使用input:type=fileelement 而不是input:type=text为了使用$_FILES. 或者您无法获取图像文件。所以你的 if 语句返回 false 并且什么也没有发生。

<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>