I am creating a plugin that will allow the admin to manually input users. One of the options is including an image. I am unable to get the image to transfer from the computer into the directory. Here is what I have

The form looks like this

   <form action="" method="post" enctype="multipart/form-data" >
            First name: <input type="text" name="firstname"><br>
            Last name: <input type="text" name="lastname"><br>
            Picture:<input type="file" name="picture"><br>
            <input type="submit" name="adminsubmit" value="Submit"/>

I have this at the top to be called once the submit button is clicked

    include ('adminInsert.php');

My adminInsert.php file looks like this

$target_path = $target_path . basename( $_FILES['picture']['name']); 
if(move_uploaded_file($_FILES['picture']['temp_name'], $target_path)) {
  echo "The file ".  basename( $_FILES['picture']['name']). 
" has been uploaded";
} else{
echo "There was an error uploading". ( $_FILES['picture']['name']).", please try again!";

Here is what I get when I print the $_FILES

Array ( [picture] => Array ( [name] => morningGloryForSite.png [type] => image/png [tmp_name] => /tmp/php3j4sls [error] => 0 [size] => 119880 ) )

I just keep returning the ELSE portion of the IF statement. I can not get the file to transfer into the desired directory. Can someone just look at this and suggest any answers?

Thank you in advance!

  • I would suggest using built in WP functions and sanitizing your data. – Wyck Oct 28 '13 at 6:19
  • Thank you. Are there any particular WP functions that you have in mind? – brandon-estrella-dev Oct 28 '13 at 16:54
  • Apparently, not everyone understands that my $target_path is not really only "http//..";. I only included that bit of the directory so that people would see how I began to declare the variable, hence the ellipses. I'm wondering if WP has functions that I am overlooking that will accomplish or are needed to complete the task that I am trying to accomplish. That's it. I'm not really looking for someone to complete the code for me, just get me going the right way with specific functions that are needed here. Outside of Wordpress, this code works. Inside wordpress, it does not. Why is that? – brandon-estrella-dev Oct 29 '13 at 5:16

doesn't look like you $target_path is a valid directory. you need to replace the url you have there with the actual directory to which it is mapped to on the server, and make sure you have the permissions to write to that directory. (-1 the question because it is not truly related to WordPress)

| improve this answer | |
  • Not related to wordpress?? Not sure how you figure that. I was hoping that I was just missing a wp function that would help accomplish what I am looking to do. And obviously, my $target_path isn't www..., that is just a holder so that everyone doesn't see my path. I want everyone to see that I started it with http rather than ../wp_content/ to see if that could be an issue. Honestly, if you can't contribute, please don't answer. You're not helping anyone. – brandon-estrella-dev Oct 28 '13 at 16:52
  • lol, you got the answer and you still complain. Have you even tried to fix your target_path as suggested? – Mark Kaplun Oct 28 '13 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.