简体   繁体   English

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

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

Have this html form code on my site to upload images: 在我的网站上有此html表单代码可以上传图像:

<input type="file" name="photo">

Also have this PHP script to process it: 也可以使用以下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>";

?>

I've tried changing a lot of things, but cannot seem to get it to upload photos the photos or names of the photos? 我尝试了很多更改,但似乎无法让它上​​传照片或照片名称吗? Before I added the upload photo code, and just used: 在添加上传照片代码之前,仅使用它:

$newnewsphoto = $_POST['photo'];

It uploaded the photo name fine, but now I have added the actual upload code for the image itself, it doesn't seem to work? 它可以很好地上传照片名称,但是现在我为图像本身添加了实际的上传代码,它似乎不起作用? All other information in the form is being uploaded except the image? 表格中的所有其他信息都将被上传(图片除外)? Anyone have any ideas of what I am doing wrong here? 有人对我在这里做错什么有任何想法吗? (PS. ERROR CHECKING HAS BEEN LEFT OUT, DOING THAT LAST MINUTE). (PS错误检查已退出,做了最后一分钟)。 Thanks! 谢谢!

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

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

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

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