简体   繁体   中英

PHP not uploading images to database or server?

Have this html form code on my site to upload images:

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

Also have this PHP script to process it:

    <?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). Thanks!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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