0

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!

7
  • 2
    I see no error checking in your code? How do you expect to find errors/issues if you aren't looking for them? Commented Mar 20, 2014 at 15:18
  • Leaving that out until I have the code working properly. Commented Mar 20, 2014 at 15:19
  • 1
    Now you can see why you should have them in first. They help you develop your code. Commented Mar 20, 2014 at 15:19
  • Have you chmod 777 (read+write) newsimages directory? Commented Mar 20, 2014 at 15:21
  • Just changed permissions, still not uploading image or image name though? Commented Mar 20, 2014 at 15:26

1 Answer 1

3

You need to have enctype attribute in your form html like:

<form method="post" action="" enctype="multipart/form-data">
Sign up to request clarification or add additional context in comments.

6 Comments

No? I will add that and try.
That should be the reason
Yes! Working now! What does that do exactly? Why did my form not work without it? Thanks for your help though!
I found this answer describing the reason: stackoverflow.com/questions/1695246/…
I concur. enctype="multipart/form-data is code that is being used when you used POST so that way the server will recognize it and process it accordingly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.