Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to upload images in a form to a specific folder. But it doesn't work.
can anyone help me?

HTML Code

HTML
 <div>
     <p><font size="+1" color = 'red'>image must rename as category id & only upload .jpg format </br> max (h-95px / W 165px)  </font></p>
    <label for="img">Image:</label>
<input type="file" name="image" />
    <input type="submit" name="submit" value="submit" />
    </div>



php code


PHP
    if(isset($btnadd)){
        $sql = "insert into category (cat_id,cat_name) values (:id,:cname)";
        $stmt = $con ->prepare($sql);
        $stmt->execute(array(':id'=>$id,':cname'=> $cname));


        $result = $stmt->fetch(PDO::FETCH_ASSOC);




    $tmp=$_FILES["file"]["tmp_name"];
  $extension = explode("/", $_FILES["file"]["type"]);
  $name=$id.".".$extension[1];

move_uploaded_file($tmp, "images/" . $id.".".$extension[1]);
Posted
Comments
Richard Deeming 2-Oct-15 8:47am    
"It doesn't work" is not enough information for anyone to diagnose the problem.

Use the "Improve question" button at the bottom of your question to update your question with the missing details. Make sure you give a full explanation of what's not working, including the full details of any error message.
Withanam 2-Oct-15 12:33pm    
it says undefined index "file" in the following line

$tmp=$_FILES["file"]["tmp_name"];
Anirudh Mangalvedhekar 14-Oct-15 17:19pm    
The index in your code is image and not file. Also I do believe you need a form like
<form action="filename" method="post" enctype="multipart/form-data">
the attribute enctype is crucial for the script to execute and also ensure within php.ini for following setting :
file_uploads = On
Good source of information to begin with :
http://www.w3schools.com/php/php_file_upload.asp
ZurdoDev 2-Oct-15 9:14am    
What do you mean it doesn't work? How can we help?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900