0

嘿,伙计们,我想我需要对此有新的看法。我有一个插入脚本,可以将数据连同上传到名为 uploads 的文件夹的图像的 URL 一起插入数据库。问题是,虽然所有信息都进入数据库,但图像永远不会上传到文件夹。有谁知道为什么?

<?php
$date=$_POST['date'];
$title=$_POST['title'];
$body=$_POST['body'];
$month=$_POST['month'];
$file = $_FILES['file'];
$name = $file['name'];
$path = "uploads/" . basename($name);

$sql="INSERT INTO content (date, title, body, month, pic_id) VALUES ('$date','$title', '$body', '$month', '" . mysql_real_escape_string($path) . "')";
$result=mysql_query($sql);

if($result && move_uploaded_file($file['tmp_name'], $path) ){
echo 'Query has been inputted into the database';
}

else{
echo 'An error occured';
}
?>

我的 html 看起来像这样:

    <h1>Post A Blog </h1></br>
    <form name="personal_information" method="post" enctype="multipart/form-data" action="insert.php">    
            <label>
                <span>Date:<br></span><input id="date" type="date" name="date" />
            </label><br><br>
            <label>
                <span>Title:<br></span><input id="title" type="text" name="title" />
            </label><br><br>
            <label>
                <span>Body:<br></span><textarea id="body" type="text" name="body"/></textarea>
            </label><br><br>
            <label>
            <span>Month:<br></span><input id="month" type="text" name="month" />
            </label><br><br>
            <br>

Upload file:</br>
<input type="file" name="file" id="fileupload"></br></br>
    <input type="submit" name="submit" value="Submit" id="submit"/>

4

2 回答 2

0

move_uploaded_file() -将上传的文件移动到新位置。

is_uploaded_file() -判断文件是否通过 HTTP POST 上传

这将为您工作:

if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
   echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.\n";
}

更多细节和示例

于 2013-07-25T18:22:48.113 回答
0

我在我的手机上,所以不能给你一个例子,但我很确定你必须使用完整路径作为目的地。

尝试 __DIR__ 。'/'。$path 或使用 document_root

于 2013-06-14T22:55:29.963 回答