繁体   English   中英

PHP无法将图像上传到数据库或服务器?

[英]PHP not uploading images to database or server?

在我的网站上有此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错误检查已退出,做了最后一分钟)。 谢谢!

您需要在表单html中具有enctype属性,例如:

<form method="post" action="" enctype="multipart/form-data">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM