PHP FORM UPLOAD

时间:2016-08-01 11:26:21

标签: php

它返回成功上传的内容,但不显示图像。我该怎么做才能解决此问题或移动未找到的上传文件流。我怎么能正确地做到这一点。请帮忙。谢谢      我的代码;

<?php include('includes/connect.php'); ?>
<?php include('includes/function.php'); ?>
<?php
if($_POST['add']){
            $h1  = trim($_POST['heading']);
            $t1  = trim($_POST['text']);
            $sql = "INSERT INTO home ( heading,text)VALUES('{$h1}', '{$t1}')" ;

            //die(print($sql));
            $result = mysql_query($sql); 
            confirm_query($result);


        if($_FILES['photo']['name'])
{
    //if no errors...
    if(!$_FILES['photo']['error'])
    {
        //now is the time to modify the future file name and validate the file
        $new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
        if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
        {
            $valid_file = false;
        }
        if($valid_file)
        {
            move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
        }
    }
    else
    {
        //set that to be the returned message
        $msg = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
        header("Location:home_add.php?msg=$msg");
        exit;
    }
}

            //die(printf($sql));
            if($result){
                $msg="Content uploaded Successfully!";
                //$_SESSION['loggein_msg'] = 'Content updated Successfully!';
                header("Location:home_add.php?msg=$msg");
                exit;

            }else{
                $msg= "Content upload failed!";
                header("Location:home_add.php?msg=$msg");
            }
        }
?>      
?>

它会返回成功上传的内容,但不显示图像

3 个答案:

答案 0 :(得分:0)

move_uploaded_file第二个参数应该是图片的正确路径,您不应该使用tmp_name。因此,请替换此行:

move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);

到:

move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$_FILES['photo']['name']);

将原始文件名保留在服务器中,否则根据您的要求使用相同的扩展名更改文件名。

答案 1 :(得分:0)

更改此内容。

$new_file_name = strtolower($_FILES['photo']['tmp_name']);

对此。

$new_file_name = strtolower($_FILES['photo']['name']);

已完成。

答案 2 :(得分:0)

首先确保您的表单有enctype='multipart/form-data'

<form enctype='multipart/form-data' ..... >

然后

move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$_FILES['photo']['name']);

确保表单上传字段的名称为photo