Hi this is Scorpionz
Can any have an idea...
How to Upload image,file in PHP
and store it in Database....
and How to call it to view on Front End means to my Html Page Web page....
and How to Specify Space that Uploading File size

Any Help will be Highly Appreciated....

Thanks and Regards
ScorpionZ

first of all Thanks Mr carobee for referring such articles ... now let me define things of my side

I have done it With uploading , first of all let me give u my Piece of code.

function upload_img($img)
{

if((!empty($_FILES["product_upload_img"])) && ($_FILES['product_upload_img']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['product_upload_img']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["product_upload_img"]["type"] == "image/jpeg") && 
    ($_FILES["product_upload_img"]["size"] < 350000)) {
    //Determine the path to which we want to save this file
      //$newname = './dataAuthor/'.$filename;
     // $newname = 'C:\xampp\htdocs\online_shopping\uploaded_images\ $filename';
      $newname = 'C:\xampp\htdocs\online_shopping\uploaded_images\$filename';
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['product_upload_img']['tmp_name'],$newname))) {
           echo "It's done! The file has been saved as: ".$newname;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["product_upload_img"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .jpg images under 350Kb are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}
}

This is a code of separate file of uploading... i have made it as a function... but i want this to be done without function

Now let me give u the HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Add Product Information</title>
</head>
<body>
<?php

if(isset($_POST['add_product']))
{
$prod_img               =   $_FILES["product_upload_img"]["name"];

    upload_img($_FILES["product_upload_img"]["name"]);
            $sql_insert_product     =   "insert into product_information (product_upload_img) values ('$prod_img')";
            $result_insert_product  =   mysql_query($sql_insert_product);
            echo $sql_insert_product;

        }
    ?>
<form name="form1" method="post" action="" enctype="multipart/form-data">
  <table class="box-admin-product-detail" width="567" border="1">
<tr>
      <td colspan="2" bgcolor="#F5F5F5"><em><strong>Upload Image</strong></em></td>
      <td colspan="4" bgcolor="#F5F5F5"><em><strong>
        <input type="file" name="product_upload_img" />
      </strong></em></td>
    </tr>
 <tr >
      <td colspan="6" bgcolor="#F8F8FF"><div align="center">
          <input type="submit" name="add_product" id="add_product" value="Add Product" onclick="return check_empty();"/>
      </div></td>
    </tr>
</table>
</form>


</body

</html>

Now Description: I have just shorten the code for easiness.
If I click on Add It just add Products other details and as well as the file that has been browse upload only first time ... but when next time uploading another image
it gives this message

Error: File camera_electronics.jpg already exists

and also in the destination Folder it is saving this file with this name

$filename

Now where i am wrong?

Thanks and Regards
ScorpionZ

Edited 3 Years Ago by mike_2000_17: Fixed formatting

first of all Thanks Mr carobee

its not Mr but Ms

for referring such articles ... now let me define things of my side

I have done it With uploading , first of all let me give u my Piece of code.

function upload_img($img)
{

if((!empty($_FILES["product_upload_img"])) && ($_FILES['product_upload_img']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['product_upload_img']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["product_upload_img"]["type"] == "image/jpeg") && 
    ($_FILES["product_upload_img"]["size"] < 350000)) {
    //Determine the path to which we want to save this file
      //$newname = './dataAuthor/'.$filename;
     // $newname = 'C:\xampp\htdocs\online_shopping\uploaded_images\ $filename';
      $newname = 'C:\xampp\htdocs\online_shopping\uploaded_images\$filename';

end quote.

try this

$newname="C:\xampp\htdocs\online_shopping\uploaded_images\";
$newname=$newname.basename($_FILES['product_upload_img']['name']);

start quote:

 //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['product_upload_img']['tmp_name'],$newname))) {
           echo "It's done! The file has been saved as: ".$newname;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else

end quote.

also print the error number here $_FILES['product_upload_img']['error']

start quote:

 {
         echo "Error: File ".$_FILES["product_upload_img"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .jpg images under 350Kb are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}
}

This is a code of separate file of uploading... i have made it as a function... but i want this to be done without function

Now let me give u the HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Add Product Information</title>
</head>
<body>
<?php

if(isset($_POST['add_product']))
{
$prod_img               =   $_FILES["product_upload_img"]["name"];

    upload_img($_FILES["product_upload_img"]["name"]);
            $sql_insert_product     =   "insert into product_information (product_upload_img) values ('$prod_img')";
            $result_insert_product  =   mysql_query($sql_insert_product);
            echo $sql_insert_product;

        }
    ?>
<form name="form1" method="post" action="" enctype="multipart/form-data">
  <table class="box-admin-product-detail" width="567" border="1">
<tr>
      <td colspan="2" bgcolor="#F5F5F5"><em><strong>Upload Image</strong></em></td>
      <td colspan="4" bgcolor="#F5F5F5"><em><strong>
        <input type="file" name="product_upload_img" />
      </strong></em></td>
    </tr>
 <tr >
      <td colspan="6" bgcolor="#F8F8FF"><div align="center">
          <input type="submit" name="add_product" id="add_product" value="Add Product" onclick="return check_empty();"/>
      </div></td>
    </tr>
</table>
</form>


</body

</html>

Now Description: I have just shorten the code for easiness.
If I click on Add It just add Products other details and as well as the file that has been browse upload only first time ... but when next time uploading another image
it gives this message

Error: File camera_electronics.jpg already exists

and also in the destination Folder it is saving this file with this name
$filename

Now where i am wrong?

Thanks and Regards
ScorpionZ

Edited 3 Years Ago by mike_2000_17: Fixed formatting

i don't recommend using the orginal file name. you will run into the same problem over and over again if you upload the same file at all. use the time() function to give each uploaded image a unique name.

This article has been dead for over six months. Start a new discussion instead.