how to import to dropbox using php? - php

I was wondering how to upload folders not just files to dropbox
I'm using a code to upload files what needs to be changed in it to create or upload folders for example?
below you will see the upload.php
<?php
require_once 'terceros/dropbox/vendor/autoload.php';
use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxApp;
$dropboxKey =" your key";
$dropboxSecret =" your secret";
$dropboxToken=" your token";
$app = new DropboxApp($dropboxKey,$dropboxSecret,$dropboxToken);
$dropbox = new Dropbox($app);
if(!empty($_FILES)){
$nombre = uniqid();
$tempfile = $_FILES['file']['tmp_name'];
$ext = explode(".",$_FILES['file']['name']);
$ext = end($ext);
$nombredropbox = "/" .$nombre . "." .$ext;
try{
$file = $dropbox->simpleUpload( $tempfile,$nombredropbox, ['autorename' => true]);
echo "Arquivo enviado com sucesso";
}catch(\exception $e){
print_r($e);
}
}
?>
now index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data" >
<input type="file" name="file" id="file"/>
<br>
<input type="submit" name="btnenviar" id="btnenviar" />
</form>
</body>
</html>
If you want to know, I'm using this composer
composer require kunalvarma05/dropbox-php-sdk

Related

i can not upload image php everything is ok but it failes uploading

everything is ok i can seperately insert query into my database but it does not move the uploaded file
here is my code. see please if you can find any issue below.
it checked my image folder directory it is correct the directory takes me to the exact folder.
upload_file is also on in php.ini
but still it is not working.
<?php
$conn = mysqli_connect("localhost", "root", "HERE_IS_MYCORRECT_PASSWORD", "HERE_IS_MYCORRECT_DTABASE_NAME");
if($conn) {
echo "<p style='color:green;'>connected</p>";
}
else {
echo " connection failed";
}
if(isset($_POST['uploadfilesub'])){
$id = $_POST['id'];
$filename = $_FILES['uploadfile']['name'];
$filetmpname = $_FILES['uploadfile']['tmp_name'];
$folder = './Resources/style/images/' . $filename;
echo "<p>".$filename."</p>";
echo "<p>".$filetmpname."</p>";
$sql = "INSERT INTO product_images VALUES ($id,'$filename') ";
if(move_uploaded_file($filetmpname , $folder)){
echo "<p color='green'>moved</p>";
$qry = mysqli_query($conn, $sql);
if($qry){
echo "<p color='green'>Inserted into mysql</p>";
} else {
echo "<p color='red'>Failed to Insert</p>";
}
}
else {
echo "<p style='color:red;'>Failed to move</p>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="id">
<input type="file" name="uploadfile" />
<input type="submit" name="uploadfilesub" value="upload" />
</form>
</body>
</html>
As far as I can see your error (in regards to the particular, mentioned, issue) is very simple:
$folder = './Resources/style/images/';
change to:
$folder = './Resources/style/images/' . $filename;
Simply move_uploaded_file expects full target path and not only target folder.

How to upload an image with html and php?

I'm trying to upload an image using html and php but when I try it, it gives me always an error.
I've already seen in the php.ini file if the upload option is active and it is, also de max-size is at 1000M
HTML code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form action="php/uploadLocal.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 code
<?php
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if(isset($_POST["submit"])) {
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.";
}
}
?>
just restarted pc and it worked, simply frustrating

PHP and MySQLi - Upload Multiple Image

I am planning to make an image galary using PHP with MySQL as a database.
I already make a multiple upload images using PHP but I got a problem. When I uploaded 8 images in database only show 1 row data. But in folder there are 8 images that I have upload.
Here is my code :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="a.php" method="post" enctype="multipart/form-data">
<input type="file" name="file_img[]" multiple>
<input type="submit" name="btn_upload" value="Upload">
</form>
<?php
include_once 'koneksi.php';
if(isset($_POST['btn_upload']))
{
for($i = 0; $i < count($_FILES['file_img']['name']); $i++)
{
$filetmp = $_FILES["file_img"]["tmp_name"][$i];
$filename = $_FILES["file_img"]["name"][$i];
$filetype = $_FILES["file_img"]["type"][$i];
$filepath = "uploads/".$filename;
move_uploaded_file($filetmp,$filepath);
$sql = "INSERT INTO files (file,path,type) VALUES ('$filename','$filepath','$filetype')";
if($connect->query($sql) === TRUE) {
header("Location: a.php");
} else {
header("Location: a.php");
}
}
$connect->close();}
?>
</body>
</html>
Can someone help me ? Thanks for ur feedback :)
Move the header location redirect outside the for loop.
Also Refer Upload multiple images and store their path in database.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="a.php" method="post" enctype="multipart/form-data">
<input type="file" name="file_img[]" multiple>
<input type="submit" name="btn_upload" value="Upload">
</form>
<?php
include_once 'koneksi.php';
if(isset($_POST['btn_upload']))
{
for($i = 0; $i < count($_FILES['file_img']['name']); $i++)
{
$filetmp = $_FILES["file_img"]["tmp_name"][$i];
$filename = $_FILES["file_img"]["name"][$i];
$filetype = $_FILES["file_img"]["type"][$i];
$filepath = "uploads/".$filename;
move_uploaded_file($filetmp,$filepath);
$sql = "INSERT INTO files (`file`,`path`,`type`) VALUES ('$filename','$filepath','$filetype')";
$connect->query($sql);
}
if($connect->query($sql) === TRUE) {
header("Location: a.php");
exit; // always add exit after header Location:
} else {
header("Location: a.php");
exit; // always add exit after header Location:
}
$connect->close();}
?>
</body>
</html>
After some code beautification of your snippet answer became obvious - you only get one new row in database, because in your for loop you redirect user to a.php right after inserting this row.
In other words your for loop will always perform one and only one step.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="a.php" method="post" enctype="multipart/form-data">
<input type="file" name="file_img[]" multiple>
<input type="submit" name="btn_upload" value="Upload">
</form>
<?php
include_once 'koneksi.php';
if (isset($_POST['btn_upload'])) {
for ($i = 0; $i < count($_FILES['file_img']['name']); $i++) {
$filetmp = $_FILES["file_img"]["tmp_name"][$i];
$filename = $_FILES["file_img"]["name"][$i];
$filetype = $_FILES["file_img"]["type"][$i];
$filepath = "uploads/".$filename;
move_uploaded_file($filetmp, $filepath);
$sql = "INSERT INTO files (file, path, type) VALUES ('$filename','$filepath','$filetype')";
// #NOTICE: Read about SQL Injection and why above SQL is bad.
// #SOLUTION: Place 1
if ($connect->query($sql) === true) {
header("Location: a.php");
exit; // #NOTICE: Always add `exit` after "header Location:"
} else {
header("Location: a.php");
exit; // #NOTICE: Always add `exit` after "header Location:"
}
}
$connect->close();
// #SOLUTION: Place 2
}
?>
</body>
</html>
Can you see it now?
Checkout link below! File [1] is your file after performing some cleaning. It's also contains two comments marked as #SOLUTION. Place 1 is the code you should move to Place 2. After moving you should also change if condition to check if all files were uploaded correctly. File [2] on the other hand is edited by me with (probably) proper solution for your problem.
I hope I helped!
https://github.com/broiniac/stackoverflow/blob/master/44346949/
EDIT: #AdhershMNair:
I think that because of line if($connect->query($sql) === TRUE) { in your solution, data for last uploaded file will be inserted into database twice (once for last iteration in for loop and once for if statement).

I am not getting the output for this script

i have written a code but that is not working. below is a code which enters an image into the database on user input. But my problem is that when i am calling it to print on page, nothing is diaplaying, please help me here.
<?php
$conx=mysqli_connect('localhost','root','sultan','colour');
$sql = "SELECT theme from colors WHERE username='shail' ";
$sqli=mysqli_query($conx,$sql);
$row=mysqli_fetch_row($sqli);
$theme=$row[0];
if(isset($_POST['submit']))
{
$theme=$_POST['theme'];
$sql = "UPDATE colors SET theme='$theme' WHERE username='shail' ";
$sqli=mysqli_query($conx,$sql);
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
body{ background-image:url(<?php echo $theme ?>) ;}
</style>
</head>
<body>
<form method="post">
Input your picture<input type="file" name="theme" /><br />
<input type="submit" value="upload" name="submit" />
</form>
</body>
</html>
You need to terminate the statement.
current
<?php echo $theme ?>
new
<?php echo $theme; ?>
<?php
$conx=mysqli_connect('localhost','root','','colour');
$sql = "SELECT theme from colors WHERE username='shail' ";
$sqli=mysqli_query($conx,$sql);
$row=mysqli_fetch_row($sqli);
$theme=$row[0];
if(isset($_POST['submit']))
{
// new code
$path = '/';
$fileName = $_FILES['theme']["name"];
$fileTmpLoc = $_FILES['theme']["tmp_name"];
$temp = explode(".", $_FILES['theme']["name"]);
$extension = end($temp);
$temp = explode('.', $fileName );
$ext = array_pop($temp );
$name = implode('.', $temp );
$FileNM = $name.time().".".$extension;
$theme = $FileNM.$ext;
move_uploaded_file($_FILES['theme']["tmp_name"], $theme);
$sql = "UPDATE colors SET theme='$theme' WHERE username='shail' ";
$sqli=mysqli_query($conx,$sql);
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
body{ background-image:url(<?php echo $theme; ?>) ;}
</style>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Input your picture<input type="file" name="theme" /><br />
<input type="submit" value="upload" name="submit" />
</form>
</body>
</html>
<?php
$conx=mysqli_connect('localhost','root','','colour');
$sql = "SELECT theme from colors WHERE username='shail' ";
$sqli=mysqli_query($conx,$sql);
$row=mysqli_fetch_row($sqli);
$theme=$row[0];
if(isset($_POST['submit']))
{
// new code
$error1="";
$error2="";
if ($_FILES["theme"]["type"] !== "image/jpeg"){
echo $error1 = "File is not in JPG!";
}else{
$error1="";
}
if(($_FILES["theme"]["size"] > 1024)){
echo $error2 = "File size not allowed more than 1 MB!";
}else{
$error2="";
}
if($error1 =="" && $error2==""){
$fileName = $_FILES['theme']["name"];
$fileTmpLoc = $_FILES['theme']["tmp_name"];
$temp = explode(".", $_FILES['theme']["name"]);
$extension = end($temp);
$temp = explode('.', $fileName );
$ext = array_pop($temp );
$name = implode('.', $temp );
$FileNM = $name.time().".".$extension;
move_uploaded_file($_FILES['theme']["tmp_name"], "img/".$FileNM);
$sql = "UPDATE colors SET theme='$FileNM' WHERE username='shail' ";
$sqli=mysqli_query($conx,$sql);
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
body{ background-image:url(<?php echo "img/".$FileNM; ?>) ;}
</style>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Input your picture<input type="file" name="theme" /><br />
<input type="submit" value="upload" name="submit" />
</form>
</body>
</html>

Uploaded images not in image folder

I just made a file upload code and I was wondering why the file upload wasn't working? I checked the permission of my image folder in localhost and that seems to be ok. I don't know where the problem exactly is. Any ideas?
<?php
require "config.php";
require "functions.php";
require "database.php";
if(isset($_FILES['fupload'])){
$filename = addslashes($_FILES['fupload']['name']);
$source = $_FILES['fupload']['tmp_name'];
$target = $path_to_image_directory . $filename;
$description = addslashes($_POST['description']);
$src = $path_to_image_directory . $filename;
$tn_src = $path_to_thumbs_directory . $filename;
if (strlen($_POST['description'])<4)
$error['description'] = '<p class="alert">Please enter a description for your photo</p>';
if($filename == '' || !preg_match('/[.](jpg)|(gif)|(png)|(jpeg)$/', $filename))
$error['no_file'] = '<p class="alert">Please select an image, dummy! </p>';
if (!isset($error)){
move_uploaded_file($source, $target);
$q = "INSERT into photo(description, src, tn_src)VALUES('$description', '$src','$tn_src')";
$result = $mysqli->query($q) or die (mysqli_error($myqli));
if ($result) {
echo "Succes! Your file has been uploaded";
}
createThumbnail($filename);
}
}
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Upload</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>My photos</h1>
<ul><?php getPhotos(); ?></ul>
<h2>Upload a photo</h2>
<form enctype="multipart/form-data" action="index.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file" name="fupload" /><br/>
<textarea name="description" id="description" cols="50" rows="6"></textarea><br/>
<input type="submit" value="Upload photo" name="submit" />
</form>
<?php
if (isset($error["description"])) {
echo $error["description"];
}
if (isset($error["no_file"])) {
echo $error["no_file"];
}
?>
</body>
</html>
Please make sure that you appended directory separator in $path_to_thumbs_directory.
And you don't need to use addslashes to $filename.

Categories