Jump to content

upload image to folder and add to database


nbbcj

Recommended Posts

hi all

 

i need a script that will allow me upload images to 2 dif folders on my server and then add the info to my database along with some other form data.

iv been looking all over for code or scripts for days now and have been playing with cut and copyed code but no look

 

again any help i will be greatful for as im a noob to php but al learning quick

 

here is my html form

 

 
<html>
<body>

<form action="add.php" method="post">
Project Name: <input type="text" name="pro_name" /><br>
Thumbnail: <input type="file" name="thumbnail" /><br> ////// this image to ../thum
Short Details: <input type="text" name="short_details" /><br>
Full Details: <input type="text" name="full_details" /><br>
Category: <input type="text" name="cat" /><br>
Image1: <input type="file" name="image1" /><br>//// and image1,2,3,4 to ../images
Image2: <input type="file" name="image2" /><br>
Image3: <input type="file" name="image3" /><br>
Image4: <input type="file" name="image4" /><br>
<input type="submit" />
</form></body></html>

 

here is my code for add.php witch only adds the info to the DB

 

<?php
error_reporting(E_ALL);

include ("../includes/db_config.php");

$con = mysql_connect($db_hostname,$db_username,$db_password);
@mysql_select_db($db_database) or die( "Unable to select database");

$sql="INSERT INTO $db_table (pro_name, thumbnail, short_details, full_details, cat, image1, image2, image3, image4)
VALUES
('$_POST[pro_name]','$_POST[thumbnail]','$_POST[short_details]','$_POST[full_details]','$_POST[cat]','$_POST[image1]','$_POST[image2]','$_POST[image3]','$_POST[image4]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)


?>

 

Link to comment
Share on other sites

1) Your type="file" form fields should use an array for the field name, then you can simply iterate over the array in the php form processing code to test for upload errors and make use of the uploaded file information. See Example #3 at this link - http://us2.php.net/manual/en/features.file-upload.post-method.php If any of the uploaded file field are required or optional, you would need to address that in the processing logic (an upload error #4 UPLOAD_ERR_NO_FILE means that no file was picked for that upload field.)

 

2) Your php code needs to first check that a form was submitted. Since one of the possible upload error conditions (see the next item on this list) will cause the $_FILES and $_POST arrays to be empty, you need to use the following logic to check if a form was submitted - if($_SERVER['REQUEST_METHOD']=='POST'){ your form processing code goes here...}

 

3) You also need to test that the $_FILES array is not empty, since that would indicate that the total of all the posted data from the form exceeds the post_max_size setting. See this link - http://us2.php.net/manual/en/ini.core.php#ini.post-max-size

 

4) Then you need to check the uploaded file information for errors (see item 1 on this list) and process the successfully uploaded file(s).

 

5) Finally, after you have processed any/all of the uploaded files and validated all of the data from the form, you need to escape the string data values before putting them into your query statement.

 

I also notice that your <form tag is missing the needed enctype attribute. Reading the entire upload handling section of the php.net documentation would probably help - http://us2.php.net/manual/en/features.file-upload.php

Link to comment
Share on other sites

hi there PFMaBiSmAd

 

i read over the info and links you posted but im still running in to probs could you take a look at my new code and correct me please

( i started with just 2 images thumbnail,image1 for testing and when its running ok add the other image2,3,4 code )

FYI the info is going in to the db but not the thumbnail name or the image1 name and

it dont seem to be uploading the thumbnail's to thum folder but is uploading the image1 to the images folder both folders have 777 chmod.

 

im hoping iv got at least some of the code right like i said be for im a noob lol THANK YOU for any help.

 

the new add.php

<?php
error_reporting(E_ALL);
include ("../includes/db_config.php");
/////
// Connects to your Database
$con = mysql_connect("$db_hostname", "$db_username", "$db_password") or die(mysql_error()) ;
@mysql_select_db("$db_database") or die(mysql_error()) ;
///////
if($_SERVER['REQUEST_METHOD']=='POST'){

foreach ($_FILES["thumbnail"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["thumbnail"]["tmp_name"][$key];
        $name = $_FILES["thumbnail"]["name"][$key];
        move_uploaded_file($tmp_name, "../thum/$name");
    }
}
///
foreach ($_FILES["image1"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["image1"]["tmp_name"][$key];
        $name = $_FILES["image1"]["name"][$key];
        move_uploaded_file($tmp_name, "../images/$name");
    }
}

}
////
$sql="INSERT INTO $db_table (pro_name, thumbnail, short_details, full_details, cat, image1, image2, image3, image4)
VALUES
('$_POST[pro_name]','$_POST[thumbnail]','$_POST[short_details]','$_POST[full_details]','$_POST[cat]','$_POST[image1]','$_POST[image2]','$_POST[image3]','$_POST[image4]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>

 

and the new form page

 

<html>
<body>

<form method="post" action="add.php" enctype="multipart/form-data">
pro name <input type="text" name="pro_name"/><p>
short details <input type="text" name="short_details"/><p>
full_details <input type="text" name="full_details"/><p>
cat <input type="text" name="cat"/><p>

<input type="hidden" name="MAX_FILE_SIZE" value="1029120" />
thumbnail:  <input name="thumbnail[]" type="file" /><p>

<input type="hidden" name="MAX_FILE_SIZE" value="1029120" />
image1  <input name="image1[]" type="file" /><p>

<input type="hidden" name="MAX_FILE_SIZE" value="1029120" />
image2  <input name="image2[]" type="file" /><p>

<input type="hidden" name="MAX_FILE_SIZE" value="1029120" />
image3  <input name="image3[]" type="file" /><p>

  <input type="hidden" name="MAX_FILE_SIZE" value="1029120" />
image4  <input name="image4[]" type="file" /><p>

<input TYPE="submit" name="upload" title="Add data to the Database" value="Add"/>
          </form>
</body>
</html>

 

also im getting this error

 

Warning: Invalid argument supplied for foreach() in /customers/0/8/d/nbbcj.co.uk/httpd.www/project/backend/add.php on line 11 Notice: Undefined index: thumbnail in /customers/0/8/d/nbbcj.co.uk/httpd.www/project/backend/add.php on line 31 Notice: Undefined index: image1 in /customers/0/8/d/nbbcj.co.uk/httpd.www/project/backend/add.php on line 31 Notice: Undefined index: image2 in /customers/0/8/d/nbbcj.co.uk/httpd.www/project/backend/add.php on line 31 Notice: Undefined index: image3 in /customers/0/8/d/nbbcj.co.uk/httpd.www/project/backend/add.php on line 31 Notice: Undefined index: image4 in /customers/0/8/d/nbbcj.co.uk/httpd.www/project/backend/add.php on line 31 1 record added

 

i have tryed what i can but no look 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.