0
Saving image on local file system
i have a web page containing a input of type file and a button. input is used to select any image file. once the image file is selected and button is clicked, i want to send this image via ajax to php, save this image file on local file system and save the path of this image file in the mysql database. I need some guidance/code example of how can i do this. i know ajax, what i can't figure out is sending the selected image via ajax to php and then storing this image on local file system.
1 Réponse
+ 8
use FormData
var data = new FormData();
data.append("file", event.target.files[0])
$.ajax({
type: 'post',
url: 'file.php',
data: data,
processData: false,
contentType: false
});
$path = "path/to/file/" . $_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"], $path);
$q = $db->prepare("INSERT INTO files (path) VALUES (:path));
$q->bindParam(":path", $path);
$q->execute();