0

I am trying to upload an image to a server, then get that image name and insert it into mysql database. Before I got to the database bit, the upload isn't working. I have narrowed it down to the fact there is no data in the $_FILES array, when I echo out $imageFileName is it blank.

Here is part of my form:

<form name="addItem" method="post" action="add-new-item.php">   
    <input name="name" placeholder="Portfolio Item Name" type="text" id="itemName"/><br />
    <input type="file" name="imageName" id="imageName" /><br />

</form>

Script, which is part of the same page the form is on:

   $target = "images/";
   $target = $target . basename( $_FILES['imageName']['name']);        
   $imageFileName=($_FILES['imageName']['name']);

    if(move_uploaded_file($_FILES['imageName']['tmp_name'], $target)){

          echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
    }else {
          echo "Sorry, there was a problem uploading your file.";
    }

 $query1="INSERT INTO portfolio_items (item_name,full_size_image)  VALUES('$itemName','$itemImage')"; 

1 Answer 1

4

You need to include the following in your <form> tag :

enctype="multipart/form-data"

So it would become :

<form name="addItem" method="post" action="add-new-item.php" enctype="multipart/form-data">   

Documented very well here - see the note at the bottom of the first page ...

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

3 Comments

@Nicola no probs ... no doubt one day you will be answering this type of question with the same answer ...
does there need to be an open database connection for it to work?
@Nicola to upload a file no ... but to run a query against a database yes ... see mysql examples here

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.