Saving image with PHP on localhost uploaded with a form - php

i'm trying to save an image uploaded with a HTML form on my localhost server (working with Xampp), but although there are no errors, the file isn't saved anywhere.
This is the form, really simple:
<form id="form1" action="result.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="imguploaded" id="imguploaded" accept=".png, .jpg, .jpeg"></br>
<input class="btn btn-outline-danger btn-lg" id="inputbtn" type="submit" value="Upload Image" name="submit">
</form>
and this is the PHP code (on result.php):
<?php
$result=false;
$error=false;
if (isset($_FILES['imguploaded'])){
$nomefile = strtolower($_FILES['imguploaded']['name']);
$path = "caricamenti/$nomefile";
move_uploaded_file($_FILES['imguploaded']['name'], $path);
echo($path);
}
?>
the path exists and it is in the same folder of the .PHP files.

In move_uploaded_file($_FILES['imguploaded']['name'], $path); the first parameter isn't correct. It should be the temporary path where php stored it intermediatly, which you find in $_FILE['imguploaded']['tmp_name'].
So change that line to
move_uploaded_file($_FILES['imguploaded']['tmp_name'], $path);
relevant docs
Be sure to:
sanitize filename & extention first
check for allowed mimetypes, size, ..
Right now I could easily upload a php script and execute that.

Try this:
$file_name= $_FILES['imguploaded']['name'];
$file_tmp_name= $_FILES['imguploaded']['tmp_name'];
$div= explode('.',$file_name );
$file_txt= strtolower(end($div));
$unique_image= substr(md5(time()),0,10).".".$file_txt;
$uploaded_image= "caricamenti/".$unique_image;
move_uploaded_file($file_tmp_name,$uploaded_image);

Related

How can I save an image in a directory with php

noob programer here. I'm doing a form and my teacher asked me to upload an image from any location of my computer and save it in a specific directory(also of my commputer). The code that i have only works if the image is in the same location as my script. Hope you can help me.
Note: Also i have to save it in the database because i'm going to use it to make a pdf.
HTML:
<html>
<head>
</head>
<form method="post" action="IConductores.php" >
<p></p>
<label>Foto</label>
<input type="file" id="Foto" name="Foto">
<input type="submit" id="Enviar" name="Enviar">
</form>
</html>
IConductores.php:
<?php
$Rfc = $_POST['Rfc'];
$Foto = $_POST['Foto'];
$SQL = "INSERT INTO Conductores VALUES ('$Rfc', '$Foto');";
$destdir = 'ImagenesPerfil/'; // path destination
$img=file_get_contents($Foto);
file_put_contents($destdir.substr($Foto, strrpos($Foto,'/')), $img);
add enctype="multipart/form-data" in your form tag
<form method="post" action="IConductores.php" enctype="multipart/form-data">
Uploaded file's name will be in $_FILES["Foto"]["name"]
And uploaded file location will be $_FILES["Foto"]["tmp_name"]
You need to use move_uploaded_file function like this
move_uploaded_file($_FILES["Foto"]["tmp_name"], $destdir.$_FILES["Foto"]["name"])

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);
}
?>

Php upload image to directory not working

I know there's questions on this issue but I'm trying to get images uploaded to a directory using move_uploaded_file. I'm using the example from the php manual http://www.php.net/manual/en/function.move-uploaded-file.php but I'm getting an Undefined index: file. File is the name of my file input type so I'm not sure why it's undefined.
Here is my form:
<form action="MoveImages.php" class="dropzone" id="my-awesome-dropzone" method="post">
<input type="file" name="file" />
<input TYPE="submit" name="upload" title="Add Images" value="Add Photo"/>
</form>
Here's my PHP file:
<?php
$uploads_dir = '/Users/Jane/Desktop/NE';
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
?>
Can anyone see where I am going wrong?
Add enctype="multipart/form-data" in your form tag.
That attribute is required when you are uploading file.

Image uploading via PHP returning empty array

I am unable to successfully upload an image/file to my server. The php is as follows:
//This is the directory where images will be saved
$uploadDir = "./";
$uploadFile = $uploadDir . basename( $_FILES['photo']['name']);
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $uploadFile)){
echo "The file has been uploaded successfully.";
} else {
print_r($_FILES);
}
I chose the directory at which this script lives, to ensure the functionality before I upload to the final directory. I want to upload photo's, and will check for file extensions later - but for now I at least need the upload functionality to work.
I get an empty array returned.
The form is as follows:
<form id="imageUploadForm" name="imageForm" enctype="multipart/form-data" action="imageController.php">
<label for="photo" class="blogLabel">Upload an Image</label>
<input type="file" name="photo" id="imageUpload">
<input type="submit" name="submit" id="imageSubmit" class="btn btn-primary" value="Upload">
</form>
You forgot the most important thing about a form -- the method!
If there is no method set it defaults to get.
You want post!
Add method="post" to your form.

Categories