所以我尝试使用W3Schools示例创建一个基本的PHP / MySQL图片上传系统作为参考,但是我不能为我的生活解决为什么上传的文件没有被复制到指定的目的地。 / p>
这是我的PHP代码:
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$uploads_dir = 'img/houses';
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
$uploads_dir . $_FILES["file"]["name"]);
echo "Stored in: " . $uploads_dir . $_FILES["file"]["name"];
}
} else {
echo "Invalid file";
}
这是HTML上传表单:
<form action="uploadHousePic.php" method="post" enctype="multipart/form-data">
Image: <input type="file" name="file" id="file"/>
<br><br>
Price: £ <input type="text" name="Price">
<br><br>
Type of house: <input type="text" name="Type">
<br><br>
Location: <input type="text" name="Location">
<br><br>
Description: <textarea rows="6" cols="60" input type="text" name="Description"></textarea>
<br><br>
<input type="submit" value="Submit" />
</form>
最后,我将目标代码放在另一个php页面上,其中将插入图片+信息:
<?php
// this is used to connect to the database on the mysql server called houselist
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();// if there is a problem with the connection and it can't connect it will display this error to the user.
}
$result = mysqli_query($con,"SELECT * FROM tbl_house_info");//this setups the query to be used in order to gather the data to be displayed on the page. it will select all the data from the tbl_house_page.
while($row = mysqli_fetch_array($result)) {//this then will run a loop and fetch all the results from the database in an array and display them in order row by row.
echo '<div class="housepost">';//this is used to add the format of the web page that was used.
echo '<img src="img/houses/', $row['Image'],'" width="270px" height="190px" alt="" />';// this line adds the image to the div and adds in the destination of the image, the image will have been placed here from the upload and now adding in the file name from the database it will display the full image on the page.
echo '<h2>' , '£' ,$row['Price'] , '</h2>' , '<p>'. $row['Type'] , '</p>' , '<p>' . $row['Location'] , '</p>' , '<p>' , $row['Description'] , '</p>';// this will then add some more formatting as well as displaying the other columns of data which are the price, type, location and destination on the web page to the user.
echo "</div>";
}
?>
非常感谢任何帮助。这是我正在研究的一个大学项目,所以我想尝试自己解决这个问题。但是,当你和你的老师都找不到问题时,会出现问题......
谢谢:)
答案 0 :(得分:0)
将您的路径从$uploads_dir = 'img/houses';
更改为$uploads_dir = 'img/houses/';
另请检查您是否具有此目录的写权限。
您还可以使用is_writable()
函数来检查是否可写。
答案 1 :(得分:0)
检查php.ini中的最大文件大小和最大帖子大小是否足够大! :)
file_uploads = On
upload_max_filesize = xxxM
post_max_size = xxxM
对于70%,这是错误的配置。
编辑:重启服务器(Xampp,apache,wampp或其他):)