-1

I selected a image using:

<input type="file" id="pimg" name="pimg" accept='image/*'/>  

My javascript code:

p_img =document.getElementById("pimg").value;  
param= 'pn='+p_img;  
xmlhttp.open("GET","add_prod.php?"+param,false);  
xmlhttp.send();  

My php code:

p_img=$_GET['img'];
$con = mysqli_connect('localhost', 'admin', 'admin', 'products');
$sql="INSERT INTO prod (img) VALUES ('$p_img')";
if (!mysqli_query($con,$sql))
{
    die('Error: ' . mysqli_error($con));
}  

This will store only the name of the file. But i want to copy the file from pc to directory. Its necessary to use Javascript as I'm using complete add product to pass values using AJAX

Joren
  • 3,068
  • 25
  • 44
Ramesh Yadav
  • 143
  • 1
  • 2
  • 9
  • This will only save the filename as you said. You havent uploaded the file into a directory on your server. You could check this http://www.w3schools.com/php/php_file_upload.asp it's a tutorial - How to upload file with javascript and php. Unless you want to store your file into your db. – 0x_Anakin Sep 09 '13 at 12:37
  • 1
    You can't access a file on a pc from javascript, you can take a look at creating a iframe with a small form that uploads the image. – Terry Sep 09 '13 at 12:37
  • 2
    `GET` is not for `POST`ing images. – Daniel Sep 09 '13 at 12:37
  • Do you want to upload the file into some directory! If yes then please upload this using some php code. If you want to user ajax then there are lots of plugin available in jquery – Rohit Choudhary Sep 09 '13 at 12:38

4 Answers4

-1

Create index.html file

<!DOCTYPE html>
<html>
<body>
    <form action="upload_file.php" method="post" enctype="multipart/form-data">
        <label for="file">Filename:</label>
        <input type="file" name="file" id="file"><br>
        <input type="submit" name="submit" value="Submit">
    </form>
</body>
</html>

Create Php file upload_file.php

<?php

if ($_FILES["file"]["error"] > 0) {
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
}

?>
Black
  • 18,150
  • 39
  • 158
  • 271
Dilip Godhani
  • 2,065
  • 3
  • 18
  • 33
-1

Use POST (form) to send data to the php file.

With $_FILES["pimg"]["tmp_name"] you can move the uploaded file (with the php function move_uploaded_file to you webserver.

Link to PHP Function http://php.net/manual/de/function.move-uploaded-file.php

q0re
  • 1,401
  • 19
  • 32
-1

You can upload the file without page refresh using simple JavaScript and PHP. Using JavaScript, the file would be passed to the PHP file and using move_uploaded_file() function file would be uploaded to the server.

The live demo and source code would found from here - Upload file using JavaScript and PHP

JoyGuru
  • 1,803
  • 20
  • 11
-1
xmlhttp.open("GET","add_prod.php?"+param,false);  

i think open method's parameter must contain true.

K55555
  • 7
  • 4