How to direct the uploaded image to the file i want - php

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.

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')";

Maybe try this solution for file upload with php.

Related

Check if image has been selected for upload

I have a form were a user can enter text and also upload images.
Saving the values and images works fine. If the user does not select an image, I get the error message that no file has been selected (Error 4).
How can I run the upload image part only if the user selected an image?
I have tried:
if (!empty($_FILES['files']['name'])){ Upload the Image }
if (!empty($_FILES['files'])){ Upload the Image }
if (!empty($_FILES['files']['name'])) { Upload the Image }
if ($_FILES['files']['error'] == UPLOAD_ERR_OK) { Upload the Image }
This the form:
<input type="file" name="files[]" title="Title" maxlength="10" accept="gif|jpg|jpeg|png|tiff">
Use is_uploaded_file() function to check if the user has uploaded any file or not, and then process inputs accordingly, like this:
if(is_uploaded_file($_FILES['files']['tmp_name'][0])){
// user has uploaded a file
}else{
// user hasn't uploaded anything
}
Above solution code is based on your name attribute of input tag,
<input ... name="files[]" ... />
If it was <input ... name="files" ... /> then the if condition would be like this:
if(is_uploaded_file($_FILES['files']['tmp_name'])){
...
}else{
...
}
Sidenote: Use var_dump($_FILES); to see the complete array structure.

php - Upload .dat file to be stored in json

I am trying to upload .dat file, I want to get the content inside the file and have it in json.
I have this code:
HTML:
<from action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload">
<button type="submit" name"btnSubmit">Upload</button>
</form>
PHP:
If(isset($_POST["btnSubmit"])) {
$file = file_get_contents($_FILES["fileUpload"]["name"], true);
$r = json_encode($file);
}
The error I get is file_get_contents("fileName.dat"): failed to open stream
I am not trying to upload the file to my server or a folder, I am trying to get the data inside it and store it into json.
A file upload will save the file in the php's defined temporary directory. From there you can either move the file to a desired location, or get the content from it. In your case you can simply get the content by using "tmp_name" instead of "name".
Future more, you have to make sure you have the enctype set in your form.
<?php
// check if file is given
if(!empty($_FILES)) {
// get file from temporary upload dir
$file = file_get_contents($_FILES["fileUpload"]["tmp_name"], true);
$r = json_encode($file);
// show restult
var_dump($r);
}
?>
<!-- add multipart -->
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload">
<button type="submit" name"btnSubmit">Upload</button>
</form>

Using PHP code and rename an image file before uploading to XAMPP database?

I am new to php and is it possible to rename an image file before uploading to the database?
EDIT: would be using a form to upload the file to a database.
<input type="file" name="image">
<input type="submit" name="upload" value="Add" action="viewpage.php">
EDIT: IMAGE OF DATABASE:
The second image above still shows the original image file name in the database while the image name in the directory already changed.
Yor can try this..
<?php
if(isset($_POST['submit_btn']))
{
$tmp_file = $_FILES['uploadedfile']['tmp_name'];
$ext = pathinfo($_FILES["uploadedfile"]["name"], PATHINFO_EXTENSION);
$rand = md5(uniqid().rand());
$post_image = $rand.".".$ext;
move_uploaded_file($tmp_file,"../post_imgs/".$post_image);
}
?>

Image Upload & storing the link in the database

I'm using this form to add the title,link of the image and the text of the article to the database.
I'm using type="text" for the image link,now,it's getting borring to upload an image on a external image upload service and copy the link.
I want to upload the image with this for and store THE LINK of the image in the database.
The form:
<?php if (!$_POST["go"]){ ?>
<form method="post" action="">
<input name="article_title" type="text">
<input name="article_image_url" type="text"> <!-- i want here type="file" -->
<textarea name="article_text"></textarea>
<input type="submit" name="go" value="Submit">
</form>
<?php
} else {
$date=date("Y.m.d");
$title = $_POST["article_title"];
$image_url = $_POST["article_image_url"];
$text = $_POST["text"];
$sql="INSERT INTO articles (title,image_url,text,date) VALUES ('$title', '$image_url', '$text', '$date')";
if (mysql_query($sql)){
echo "done";}
else {echo "error<br>" . mysql_error();}}
?>
Please help me with this :)
ps:sorry for my English :$
The first thing you should do, and it seems you are clueless about SQL escaping, is add following before you access the first $_POST var:
$_POST = array_map("mysql_real_escape_string", $_POST);
Then you seemingly want to use a file upload for the image. If so change the url field to:
<input type=file name=image>
This uploaded file will show up in $_FILES. Use it like this, preferrably after you've read the other fields from $_POST:
if ($img = $_FILES["image"]["tmp_name"]) {
$image_url = md5_file($img) . ".jpeg";
move_uploaded_file($img, "./upload/$image_url");
$image_url = "http://www.example.org/where/$image_url";
}
There are lot's of security concerns with that. But that's out of scope here, so I hardwired it to .jpeg. There's lots of information in the manual and its comments: http://de2.php.net/manual/en/features.file-upload.php

How do I edit form and script to upload two files instead of one -file to server, file name to mysql

The form and PHP code included here has been used to successfully upload a file name to the database and the file itself to a folder on the server. I need to edit the form and the code to allow for simultaneous upload of two files instead of just one. The filenames will go to the database while the files themselves will go to a folder on the server. In the mysql database fields each of the filenames have to be proceeded with static strings thusly; "images/" and "flash/". The one for images is that way already in the script, I just need to amend the script to accept a second file upload (the files uploaded will be jpeg and flash instead of just jpeg the way it is now. On the form the second file field could carry the name Flash. I have edited the mysql database so it will have an additional field named "flash".
In short, I need this to work exactly ad it does now, only uploading 2 files instead of one. How do i do this?
<form enctype="multipart/form-data" action="add.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Phone: <input type="text" name = "phone"><br>
Photo: <input type="file" name="photo"><br>
STK: <input type="text" name = "STK"><br>
<input type="submit" value="Add">
</form>
</body>
</html>
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$ID=$_POST['ID'];
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);
$STK=$_POST['STK'];
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("employees") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$ID','$name', '$email', '$phone', 'images/$pic','$STK')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
</body>
first of all the form needs to be changed:
Photo 1: <input type="file" name="photo[]"><br>
Photo 2: <input type="file" name="photo[]"><br>
second:
you would a: need another field in your database to store the second photo file name
then just use a foreach() loop to run through the $_Files array
If you want to separate the photos from the Flash files, you would change your form like so:
Photo: <input type="file" name="photo"><br />
Flash file: <input type="file" name="flash"><br />
and then add:
$pic=($_FILES['photo']['name']);
$flash=($_FILES['flash']['name']);
And finally add to your INSERT query accordingly. Like sfmoe said, if you want to allow multiple photos and multiple flash files, just add more fields, turn photo and flash into photo[] and flash[] (in the input tags), and use a foreach loop.

Categories