How to upload image in a specific folder php? - php

I have xampp installed and I have a program that you choose a file and it gets uploaded to my server. I have a folder in htdocs called uploads thats meant for storing the pictures. When I upload them, it goes in the htdocs, but not the folder in htdocs i want. I did specify that i needed it to go there. Can someone help?
Heres the code:
<?php
#$name = $_FILES['file']['name'];
#$size = $_FILES['file']['size'];
#$type = $_FILES['file']['type'];
#$tmp_name = $_FILES['file']['tmp_name'];
if (isset($name)) {
if (!empty($name))
{
$location = 'uploads/';
if (move_uploaded_file($tmp_name, $location. $name));
echo 'Uploaded';
}
else
{
echo 'Please choose a file';
}
}
?>
<form action="first.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="Submit" value="Submit">
</form>

Try below code:
Note: Make sure your uploads folder have write permission.
<?php
define("DOC_ROOT", $_SERVER['DOCUMENT_ROOT']."/");
define("PDF_UPLOADS", DOC_ROOT."uploads/");
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
if (isset($name))
{
if (!empty($name))
{
if(move_uploaded_file($tmp_name, PDF_UPLOADS. $name))
echo 'Uploaded';
else
echo "Not Uploaded";
}
else
{
echo 'Please choose a file';
}
}
?>

Related

How to upload to a different drive in PHP?

I have a php page that lets you upload files, and the files just get uploaded to my computer, but I need them to be uploaded to a different hard drive. I need them to be uploaded to
"D:\uploads". I know there are some other problems with this code but i just need this out of the way. Heres the code itself:
<link rel="icon" type="image/png" href="favicon.ico.ico"/>
<?php
define("DOC_ROOT", $_SERVER['DOCUMENT_ROOT']."/");
define("PDF_UPLOADS", DOC_ROOT."uploads/");
#$name = $_FILES['file']['name'];
#$size = $_FILES['file']['size'];
#$type = $_FILES['file']['type'];
#$tmp_name = $_FILES['file']['tmp_name'];
if (isset($name))
{
if (!empty($name))
{
if(move_uploaded_file($tmp_name, PDF_UPLOADS. $name))
echo 'Uploaded';
else
echo "Not Uploaded";
}
else
{
echo 'Please choose a file';
}
}
?>
<form action="first.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="Submit" value="Submit">
</form>
Wouldn't it be as simple as this:
define("PDF_UPLOADS", "D:/uploads/");

file uploaded successfully but I can't find the file

I'm using Azure server to open the website and linking to dropbox to store all the code files. I have an folder called 'image' but after uploading, though it shows "Uploaded!", I can't find that file.
Here's my code:
<?php
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
if (isset($name)){
if(!empty($name)){
$location = 'image/';
if(move_uploaded_file($tmp_name, $location.$name)){
echo 'Uploaded!';
}else{
echo 'There was an error.';
}
}else{
echo 'Plz choose a file.';
}
}
?>
<form action="add.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="file" id="file">
<input type="submit" value="Upload Image" name="submit">
</form>
Your uploaded file is in the image folder that is in the same folder of your page.php.
See your code:
$location = 'image/';
if(move_uploaded_file($tmp_name, $location.$name)){
The final location of your file is $location.$name. This is image/your_file_name.your_extension

how to upload and convert image in base64 without storing the image in php

I can upload the image and save it in any location, then I convert that image in base64. but how can we do that we convert image in base64 without saving it. any solution please?
$photoTmpPath = $_FILES['filename']['tmp_name'];
$data = file_get_contents($photoTmpPath);
$base64 = base64_encode($data);
and if you want to test:
header('Content-type: image/png');
echo $data;
exit;
If you have a Web Form which manages your Image upoad through $_POST. You could use this ..
<?php
if(isset($_POST)){
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
if(isset($name)){
if(!empty($name)){
$target_path = basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['file']['name'])." has been uploaded";
$im = file_get_contents($_FILES['file']['name']);
$imdata = base64_encode($im);
var_dump($im);
unlink($_FILES['file']['name']);
}
}else{
echo 'Please choose a File';
}
}
}
?>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>

php image file upload and convert to base64 without saving image

I know how to upload image file and save to other location by using the following code. However, I need to do in such a way that user upload image and automatically convert to base64 without saving that image in my location. How should I do?
<?php
//print_r($_FILES);
if(isset($_FILES['image']))
{
$errors=array();
$allowed_ext= array('jpg','jpeg','png','gif');
$file_name =$_FILES['image']['name'];
// $file_name =$_FILES['image']['tmp_name'];
$file_ext = strtolower( end(explode('.',$file_name)));
$file_size=$_FILES['image']['size'];
$file_tmp= $_FILES['image']['tmp_name'];
echo $file_tmp;echo "<br>";
$type = pathinfo($file_tmp, PATHINFO_EXTENSION);
$data = file_get_contents($file_ext);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
echo "Base64 is ".$base64;
if(in_array($file_ext,$allowed_ext) === false)
{
$errors[]='Extension not allowed';
}
if($file_size > 2097152)
{
$errors[]= 'File size must be under 2mb';
}
if(empty($errors))
{
if( move_uploaded_file($file_tmp, 'images/'.$file_name));
{
echo 'File uploaded';
}
}
else
{
foreach($errors as $error)
{
echo $error , '<br/>';
}
}
// print_r($errors);
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<p>
<input type="file" name="image" />
<input type="submit" value="Upload">
</p>
</form>
There is a mistake in your code:
$data = file_get_contents( $file_ext );
This should be:
$data = file_get_contents( $file_tmp );
This should solve your problem.

Multiple File Upload PHP

I use the Following code to upload a single file .With This code I can Upload a single file to the database .I want to make multiple files uploaded by selecting multiple files in a single input type file.What Changes should i make in the code to make it upload multiple files?
<?PHP
INCLUDE ("DB_Config.php");
$id=$_POST['id'];
$fileTypes = array('txt','doc','docx','ppt','pptx','pdf');
$fileParts = pathinfo($_FILES['uploaded_file']['name']);
if(in_array($fileParts['extension'],$fileTypes))
{
$filename = $_FILES["uploaded_file"]["name"];
$location = "E:\\test_TrainingMaterial/";
$file_size = $_FILES["uploaded_file"]["size"];
$path = $location . basename( $_FILES['uploaded_file']['name']);
if(file_exists($path))
{
echo "File Already Exists.<br/>";
echo "Please Rename and Try Again";
}
else
{
if($file_size < 209715200)
{
$move = move_uploaded_file( $_FILES["uploaded_file"]["tmp_name"], $location . $_FILES['uploaded_file']['name']);
$result = $mysqli->multi_query("call sp_upload_file('".$id."','" . $filename . "','".$path."')");
if ($result)
{
do {
if ($temp_resource = $mysqli->use_result())
{
while ($row = $temp_resource->fetch_array(MYSQLI_ASSOC)) {
array_push($rows, $row);
}
$temp_resource->free_result();
}
} while ($mysqli->next_result());
}
if($move)
{
echo "Successfully Uploaded";
}
else
{
echo "File not Moved";
}
}
else
{
echo "File Size Exceeded";
}
}
}
else
{
echo " Invalid File Type";
}
?>
The Html That is used is
<form id = "upload_form" method="post" enctype="multipart/form-data" >
<input type="file" name="uploaded_file" id="uploaded_file" style="color:black" /><br/>
</form>
Basically you need to add to input name [] brackets and attribute "multiple"
<form id = "upload_form" method="post" enctype="multipart/form-data" >
<input type="file" name="uploaded_file[]" multiple="true" id="uploaded_file" style="color:black" /><br/>
</form>
Now all uploaded file will be available via
$_FILES['uploaded_file']['name'][0]
$_FILES['uploaded_file']['name'][1]
and so on
More info at
http://www.php.net/manual/en/features.file-upload.multiple.php

Categories