0

Ok So. This has been driving me crazy. I want a form to 'add a product' which places data X Y Z into database which works fine and post to self without issues.(As I removed the image part).

I now want to upload a file, I presume use

<form action="thissameurl.php" method="post" enctype="multipart/form-data">
<input type="file" name="productimage">
</form>

To add to my form (just the input bit would be added).

Now, what do I need to add to my 'if post' section in the header to allow me to..

1.) Upload the file to a permanent folder

2.) Keep the original file name

3.) Have the filename (not the path) in a variable so that I can store it in the database.

Now im a sure this isn't as difficult as it seems to be. I am open to other technologies if they make it easier than using PHP. The files aren't going to be very large and have check that PHP.ini allows for them to be good enough.

I have looked at http://www.w3schools.com/php/php_file_upload.asp without much success.

Many thanks in advance.

4
  • 1
    Don't stop/limit yourself to W3 Schools, which in many a people's opinion is not the best resource. Keep Googling, you WILL find answers that may very well lead you to Q&A's here on SO. Keywords "php upload database mysqli pdo" that are sure to get you results. Commented Oct 5, 2013 at 15:19
  • Have you at least read the manual: php.net/manual/en/features.file-upload.post-method.php Commented Oct 5, 2013 at 15:26
  • I have checked the php.net example 3 just gives an error. Invalid argument supplied for 'in each' Commented Oct 5, 2013 at 15:37
  • @MattHorner A foreach is for multiple uploads at once or for an array of allowed files, depending on what "your" foreach is. Commented Oct 5, 2013 at 15:44

1 Answer 1

1

Here is what you need

1.) Upload the file to a permanent folder --> use move_uploaded_file(). http://www.php.net/manual/en/function.move-uploaded-file.php

2.) Keep the original file name --> $_FILES["file"]["name"] has the original name

3.) Have the filename (not the path) in a variable so that I can store it in the database. --> Store value of $_FILES["file"]["name"].

Sign up to request clarification or add additional context in comments.

Comments

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.