在我的网站上使用此html表单代码上传图片:
<input type="file" name="photo">
还有这个PHP脚本来处理它:
<?php
if (isset($_POST['postnewstory'])){
$newusername = $user_data['username'];
$newemail_address = $user_data['email_address'];
$newnews_title = $_POST['newnews_title'];
$newnews_body = $_POST['newnews_body'];
$newnews_photo=($_FILES['photo']['name']);
$newbutton = $_POST['newbutton'];
$newnews_link = $_POST['newnews_link'];
$newnews_tags = $_POST['newnews_tags'];
//This is the directory where images will be saved
$target = "newsimages/";
$target = $target . basename( $_FILES['photo']['name']);
//update the info in database
mysql_query ("INSERT INTO news (`news_title` ,`news_body` ,`news_photo` ,`news_date` ,`username` ,`news_tags` ,`button`,`news_link`)
VALUES ('$newnews_title', '$newnews_body', '$newnews_photo', now(), '$newusername', '$newnews_tags', '$newbutton', '$newnews_link');") or die (mysql_error());
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target));
echo "<div class='alert alert-success'><strong>This post was sent!</strong></span></div> ";
} else echo "<strong><font color=red>Update did not work, please try again.</font></strong>";
?>
我尝试过改变很多东西,但似乎无法上传照片的照片或照片名称?在我添加上传照片代码之前,刚刚使用了:
$newnewsphoto = $_POST['photo'];
它上传的照片名称很好,但现在我已经添加了图片本身的实际上传代码,它似乎无法正常工作?除图像外,表格中的所有其他信息是否正在上传?任何人对我在这里做错了什么都有任何想法? (PS。错误检查已经离开,做了最后一分钟)。谢谢!
答案 0 :(得分:3)
您需要在表单html中使用enctype属性,如:
<form method="post" action="" enctype="multipart/form-data">