PHP - 上传文件时出错

时间:2016-07-27 09:15:38

标签: php html image-uploading

我最近在php中编写代码,通过move_upload_file()上传图片/文件,如下所示:

if (isset($_POST["title"]) && isset($_POST["content"]) && isset($_POST["category"])) {

        //if (!isset($_POST[]))
        $title = $_POST['title'];
        $desc = $_POST['content'];
        $lat = $_POST['latitude'];
        $long = $_POST['longitude'];
        $category = $_POST['category'];
        $image = $_POST['userfile'];

        if(isset($_FILES['userfile'])) {

            $fileName = $_FILES['Filename']['name'];
            $target = "uploads/"; 
            $fileTarget = $target.$fileName; 
            $tempFileName = $_FILES["Filename"]["tmp_name"];

            $result = move_uploaded_file($_FILES["Filename"]["tmp_name"],$fileTarget);

            /*
            *   If file was successfully uploaded in the destination folder
            */
            if($result) { 
                header('Location: post.php?success'); 
                $query = "INSERT INTO public_info (title, content, category, imagePath) VALUES ('$title','$desc','$category', '$fileTarget')";
                $link->query($query) or die("Error : ".mysqli_error($link)); 
                }
            else { 
                header('Location: post.php?errimg'); 
                }
            mysqli_close($link);
        }
        else {
            $query = "INSERT INTO public_info (title, content, category) VALUES ('$title','$desc','$category')"; 

            $result = mysqli_query($link, $query);

            if ($result) {
                header('Location: post.php?success');
            }

            else {
                header('Location: post.php?error');
            }
        }
        // $query = "INSERT INTO public_info (title, content, category) VALUES ('$title','$desc','$category')";   
    }

以及html

中的此类表单

<form enctype="multipart/form-data" action="getPublicInfo.php" method="post" class="form">
				<?php if($result) {echo $hasil;} ?>            
                <div class="form-group">
					<label> Judul </label>
						<input type="text" name="title" class="form-control" placeholder="title" required/>
				</div>
				<div class="form-group">
					<label> Description </label>
						<textarea type="text" name="content" class="form-control" required rows="3"> </textarea>
				</div>
                <div class="form-group">
					<label> Pick a category </label><br>
						<label class="radio-inline">
                            <input type="radio" name="category" value="1"/> Headlines
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="category" value="2"/> Event
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="category" value="3" checked/> Info lain
                        </label>
				</div>
				<!--store image-->
				<input name="MAX_FILE_SIZE" value="10000000" type="hidden">
				<div class="form-group">
					<label> Upload an image </label>
						<input name="userfile" type="file">
				</div>
				<input value="Submit" type="submit" class="btn btn-default">
			</form>

但此代码显示实际将弹出错误的Location: post.php?errimg。有人可以帮忙吗?感谢。

1 个答案:

答案 0 :(得分:1)

你使用了错误的变量 位置:

$fileName = $_FILES['Filename']['name'];
$target = "uploads/"; 
$fileTarget = $target.$fileName; 
$tempFileName = $_FILES["Filename"]["tmp_name"];
$result = move_uploaded_file($_FILES["Filename"]["tmp_name"],$fileTarget);

使用:

$fileName = $_FILES['userfile']['name'];
$target = "uploads/"; 
$fileTarget = $target.$fileName; 
$tempFileName = $_FILES["userfile"]["tmp_name"];

$result = move_uploaded_file($_FILES["userfile"]["tmp_name"],$fileTarget);