嘿,伙计们,我想我需要一个新的眼睛。我有一个插入脚本,将数据插入到数据库与图像的URL一起被上传到一个名为uploads的文件夹。问题是,虽然所有信息都进入数据库,但图像从未上传到文件夹。有人知道为什么吗?
<?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"/>
我在手机上,所以不能给你一个例子,但我很确定你必须使用完整路径作为目的地。
try __DIR__。"/"。或者使用document_root
move_uploaded_file() -将上传的文件移动到新位置
is_uploaded_file() -告诉文件是否通过HTTP POST上传
这将为您工作:
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.'n";
}
更多细节和示例