So guys.. I want to upload an image to a certain folder name "uploads" and insert the name of that image on database, the problem is that he isn't uploading the image..
So i have this basic HTML code:
<form action="newimage.php" method="POST">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<input type="file" name="pic" id="pic">
</div>
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
And this is my PHP page
<?php
$mysqli = new mysqli("localhost", "root", "", "homepage");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$name = "somethinghere";
#do upload
$target_dir = "uploads";
$temp = explode(".", $_FILES["pic"]["name"]);
$newfilename = $target_dir . '/Foto-' . $name . '.' . end($temp);
if (move_uploaded_file($_FILES['pic']['tmp_name'], $newfilename) ) {
$productimage = 'Foto-' . $name . '.' . end($temp);
}
else
{
$productimage = '';
}
$sql = "INSERT INTO galeria_front (image_name) VALUES ('$productimage')";
if ($mysqli->query($sql) === TRUE) {
header("Location: galery.php");
} else {
echo "Error: " . $sql . "<br>" . $mysqli->error;
}
$mysqli->close();
?>
As you can see i have a conection which works, and tried to insert the productimage into the database table but for some reason , it's entering the else{} and making my productimage empty on the database which means the above upload isn't working.
P.S(Pardon me for any bad english)
try adding these
<form action="newimage.php" method="POST" enctype="multipart/form-data">
</form
Alter your script into this
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !=
"jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been
uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
https://www.w3schools.com/php/php_file_upload.asp
you are missing enctype="multipart/form-data"
<form action="/process.php" method="post" enctype="multipart/form-data"></form>
Related
This question already has an answer here:
How i can change the image name before uploading it to folder?
(1 answer)
Closed 3 years ago.
I just need to change the image name before uploading it to folder.
Whatever user type in here:
<input name="yname" placeholder="Your Name"class="input-group-field" >
the image name will be changed to given name.
html
<form action="upload.php" method="post" enctype="multipart/form-data">
<h3>Select image to upload:<br></br></h3>
<input name="yname" placeholder="Your Name"class="input-group-field" ><br>
<input type="file" class="button" id="fileToUpload" name="fileToUpload">
<input type="submit" class="button" value="Upload Images" name="submit">
</form>
<button class ="button" onclick="window.location.href = 'showimgs.php';">show images</button>
php
<?php
$target_dir = "photos/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
Echo "<b><button class=button onclick=location.href='images.html'>Click here to go back</button></b>";
?>
I am beginner and this is not my own php code.
Try this something like this (It will also make sure the file name is unique)
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '_' . $_POST['yname']. '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);
You can then also use the $newfilename value to push the details to your Database (If you have one)
Source: How to rename uploaded file before saving it into a directory?
I want to Upload imagename into database and directory then want to show it on preview page (preview.php ). but its not working at all. as i am new to programming, so i need support. here the problem is, neither the image save into directory nor into the database.
here is the form page (form5.php) where we select image and after click on submit it goes to next page pptupload.php.
<form method="post" action="pptupload.php?appid=<?php echo $appid; ?>" enctype='multipart/form-data'>
<table>
<tbody>
<tr>
<img id="pptimg" src="#" width="600px" height="300px" alt="Scanned Passport" />
<input type="file" id="file" name="file" />
</tr>
<tr>
<input name='but_upload' type="submit" class="btn btn-primary" value="Save and Continue">
</tr>
</tbody>
</table>
</form>
here is the next Page (pptupload.php ) . On this Page, I want redirect to next page preview.php after successful upload of image and if it failed then show error and return back to the image selection page called form5.php .
<?php
session_start();
$appid = $_GET['appid'];
include("connect.php");
if(isset($_POST['upload'])){
$name = $_FILES['file']['name'];
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
// Select file type
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Valid file extensions
$extensions_arr = array("jpg","jpeg","png","gif");
// Check extension
if( in_array($imageFileType,$extensions_arr) ){
// Upload file
move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name);
}
}
// Insert record
$query = "insert into payments(app_id, pptimg) values('$appid','$name')";
if (mysqli_query($connect,$query)){
echo "Image were updated successfully.";
header("Location: preview.php?appid=".$appid);
}else{
echo "Photo not uploaded".mysqli_error($connect);
}
?>
Check below code :
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
I am wondering if how can i change this to upload any kind of files(not just an image file). I am using these set of codes on my test project.
<form action="" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="submit">
</form>
<?php
if ($_POST['submit']) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
//Check if image file is a actual image or fake image
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if ($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
//Allow certain file formats
if($imageFileType != "jpg"
&& $imageFileType != "png"
&& $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
Can anyone help me, i tried removing the check if actual image , but it didnt help me.
Just delete image validation from your copied php code
<?php
if($_POST['submit']){
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
$target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
<?php
if($_POST['submit']){
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
$target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
How do I upload a picture with name,address,phone,email in a html form and store the inputs in mysql database and display as a Icard in php?
HTML
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
PHP
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
I want to upload an file from form and when I click submit it uploads the file to the server and insert filename, filesize and filetype from that file. but I always get errors like:
Warning: "filesize(): "stat failed for "uploadedfile" in C:\wamp\www\Project2\members\uploaded.php on line 14"
Warning: filetype(): "Lstat failed for "uploadedfile" in C:\wamp\www\Project2\members\uploaded.php on line 15"
Here is my uploadfile.php:
<html>
<body>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<title>Uploading Page</title>
<b>Hello again,<?php echo $username ?>. From here, you can select your image or file and upload our database.</b>
<form action="uploaded.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form></tr>
</table>
</form>
</body>
</html>
and here is my uploaded.php:
<html>
<body>
<title>Uploading Page</title>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$filename = basename($_FILES["fileToUpload"]["name"]);
$filesize = filesize($_FILES["fileToUpload"]["name"]);
$filetype = filetype($_FILES["fileToUpload"]["name"]);
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "<br>File is an image - " . $check["mime"] . ".</br>";
$uploadOk = 1;
} else {
echo "<br>File is not an image.</br>";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "<br>Sorry, file already exists.</br>";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000000000000) {
echo "<br>Sorry, your file is too large.</br>";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "<br>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</br>";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<br>Sorry, your file was not uploaded.</br>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$sql=mysqli_query($con,"insert into datas(username,imagesnotes,size,type) values('$username','$filename','$filesize')");
if(!$sql)
{
echo "<br>there is a problem in MySQL please check again.</br>";
}
echo "<br>The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.</br>";
} else {
echo "<br>Sorry, there was an error uploading your file.</br>";
}
}
mysqli_close($con);
?>