Home > OS >  How to direct the uploaded image to the file i want
How to direct the uploaded image to the file i want

Time:12-07

i'm asking user to input the image as;

Fotoğraf <input type="file" id="foto" name="foto" accept="image/*">
                    <br> <br> <br>

Then i am storing that data into my database with the following;

$j=$_POST['foto'];

$sql = "INSERT INTO customer_list (ad_soyad,telefon,e_posta,cari_kart_kodu,olusturma_tarihi,guncelleme_tarihi,sifre,foto2) VALUES('$a','$b','$c','$d','$e','$f','$h','$j')";


And then i am listing the files and the image i get from the user in the function below;

 $sql = "SELECT ad_soyad,id,telefon,e_posta,cari_kart_kodu,olusturma_tarihi,guncelleme_tarihi,foto2 FROM customer_list";

$tdStyle='background-color:grey;';
 echo "<td style=\"$tdStyle\"> <img src = ".$row['foto2']." width=200 height=200 ></td>";

However when user uploads the image,i want it to upload the image into my C:\xampp\htdocs\ea file.

Tried this Storing images in MySQL couldn't do it properly.Appriciated for the help.

CodePudding user response:

you must set the enctype in form like this to upload the file

<form action="post" enctype="multipart/form-data">
<input type="file" id="foto" name="foto" accept="image/*">
</form>

to access the uploaded file in the action page you need to do this instead of your code

$j=$_POST['foto'];  // you can't access file in $_POST

Use $_FILES to get file data

$j=$_FILES["foto"]["name"]; // get the name of image
echo $j;  // print the image name just to check 

upload the file to your destination if you want to upload the file in C:\xampp\htdocs\ea folder. If your form file exists in this format then use below code htdocs- ea (your image path) yourfoldername - -form.php (your form)

if(is_uploaded_file($_FILES["foto"]["tmp_name"])){
    move_uploaded_file($_FILES["foto"]["tmp_name"], $path1="../ea/".$_FILES["foto"]["name"])or die("couldn't copy.");
}

after file upload save the data to database

$sql = "INSERT INTO customer_list (ad_soyad,telefon,e_posta,cari_kart_kodu,olusturma_tarihi,guncelleme_tarihi,sifre,foto2) VALUES('$a','$b','$c','$d','$e','$f','$h','$j')";

CodePudding user response:

Maybe try this solution for file upload with php.

  • Related