I have the following;
$FilePath = "c:\user\test\koala.jpg";
$put = $dropbox->putFile($FilePath);
or
$put = $dropbox->putFile("c:\user\test\koala.jpg");
This works great, and uploads the file to dropbox.
However, obviously I can not get the full path ie c:\user\test\koala.jpg through a form input box, due to security restrictions.
Is there a way around this that would work. Where I can just get $FilePath through some form of input without having to submit it as a temp file to my server.
I have put the full code below.
<?php
// #link https://github.com/BenTheDesigner/Dropbox/blob/master/Dropbox/API.php#L122-139
// Require the bootstrap
require_once('bootstrap.php');
// Extend your sript execution time where required
set_time_limit(0);
$put = $dropbox->putFile($FilePath);
// Dump the output
// var_dump($chunked);
Test something like this:
HTML
...
<form type="POST" action="myPhpUploadPage.php" enctype="multipart/form-data">
<input type="file" name="file"/><br/>
<input type="submit" value="send"/>
</form>
...
PHP (myPhpUploadPage.php)
// Require the bootstrap
require_once('bootstrap.php');
// Extend your sript execution time where required
set_time_limit(0);
if(isset($_FILES['file'])){
$FilePay = $_FILES['file']['tmp_name'];
$put = $dropbox->putFile($FilePath);
} else {
echo 'not file selected';
}
How about uploading your files to your server, and after running the script to copy them to your Dropbox account. Then run a script to erase the files in the temporary directory or specific folder.
Related
The following code does not work on PHP version 5.4 and higher on my server due to deprecation. Changing PHP version to 5.3 or lower with MultiPHP Manager, however, works fine.
What should I do to the following code to make it work for updated versions?
<?php
//blog.theonlytutorials.com
//author: agurchand
if($_POST){
//get the url
$url = $_POST['url'];
//add time to the current filename
$name = basename($url);
list($txt, $ext) = explode(".", $name);
$name = $txt.time();
$name = $name.".".$ext;
//here is the actual code to get the file from the url and save it to the uploads folder
//get the file from the url using file_get_contents and put it into the folder using file_put_contents
$upload = file_put_contents("uploads/$name",file_get_contents($url));
//check success
if($upload) echo "Success: <a href='uploads/".$name."' target='_blank'>Check Uploaded</a>"; else "please check your folder permission";
}
?>
<html>
<head><title>Theonlytutorials - Simple File Upload from URL Script!</title></head>
<body>
<h3>Theonlytutorials.com - Very Simple File Upload from URL Script!</h3>
<form action="" method="post">
Your URL: <input type="text" name="url" />
</form>
</body>
</html>
If you put data with file_put_contents() on one server (or xampp) and then retrieve the info with file_get_contents() on another server, the function may not work. I ran the code to put the data into the file and then read it back with file_get_contents(), and it worked. In other words, if it doesn't seem to be working, make sure you are reading from a file that you created on the same server.
Other than that, see other ideas about having correct settings in php.ini
I am completely a novice in all this ...
I have created a Social Networking project in which there is a module which allows user to upload photos..
I have hosted this project in my college server
I access that server using bitvise client with my server credentials.
My problem is i don't know how to setup upload mechanism for remote server ... In my localhost i simply use
move_uploaded_file($_FILES['file']['tmp_name'],$target_file);
function but i don't know how to do this for remote server ...
I tried FTP by looking at some tutorials but that didn't worked for me.
In my project structure there is a directory
users/user_id (diff for all users)/photos
here i want to place the uploaded files....
A proper description with example and proper functioning might be very helpful for me.... Thank you
EDIT:
Below is my code.
Photos.php
<form class="input-group-btn" method="post" action="editPhotos.php"enctype="multipart/form-data" id="myForm">
<input type="file" name="file" id="imgInp">
<button type="submit" class="btn btn-primary" name="form-submit">Done</button>
</form>
editPhotos.php
if( isset($_POST['form-submit']) ){
$target_file = "users/".$email."/pictures/Photos/" . basename($_FILES["file"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
move_uploaded_file($_FILES['file']['tmp_name'],$target_file);
$img =str_replace(" ", "",basename($_FILES["file"]["name"]));
rename($target_file, "users/".$email."/pictures/Photos/".$img);
header('Refresh: 1; url=Photos.php?user='.$email);
}
Small tutorial how to upload file.
For sure, you need correct encryption and file's type in your form (ommited other fields, to clear example):
form.html
< form action="upload.php" method="post" enctype="multipart/form-data">< /form>
< input name="test" type=file>
upload.php
In $_FILES you have all data of uploaded file. In given example, we have field named test.
Advice, to always first check error $_FILES['test']['error'] - the values you can find in here.
If this is correct, then prepare upload path. Some advices:
remember that if you use original filename ($_FILES['test']['name']), then is User upload second file, with same name, you will need overwrite file or ignore upload. Other way, is to save data to database and generate temporary name form him.
destination path(target_file) - regardless if upload folder is in the same catalog, you should always use global path, as good practice. You can use DIR for that.
don't use in path data, like email - is you have project, and want give opportunity to change email in configuration, what you will do with files? Better save user to Database and use his ID as key.
If you have path, then you simply need only use of move_uploaded_file, but remember to check result, as it not always will return true. You can have error, when you don't have permissions to destination folder (you'll need debug this).
I see that you, first upload file, then rename (then you should check, if rename was success). Don't extends this process, if it not necessary. Upload file for final path and name.
Example of code (I this rattle off)
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$fileName = basename($_FILES["file"]["name"]);
$fileName = str_replace(" ", "", $fileName);
$target_file = sprintf(__DIR__ . "/users/%s/pictures/Photos/%s", $email, $fileName);
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
// File was uploaded
header('Refresh: 1; url=Photos.php?user=' . $email);
} else {
// File was not uploaded;
throw new \Exception('File was not uploaded. Check warnings.');
}
}
Used other method to check, if this is POST
use method sprintf, for better code quality
checked effect of move_uploaded_file
use global path for destination file
Below code is risky in live environment, please use cautiously
Use a relative path to the uploads folder. For example, if your uploads folder is placed outside your current folder. Then
$PATH = '/absolute/example/path/to/uploads';//from config file
$target_file = "$PATH/file_name";
move_uploaded_file($_FILES['file']['tmp_name'],$target_file);
The above code will work both in local and remote server.
Other checks are below:
Check for errors while uploading file in server
To enable error handling use below code in your upload logic:
ini_set('display_errors', 1);
error_reporting(E_ALL);
Another important note is to make uploads folder writable otherwise file upload wont work.
I have a form:
<form method="POST" action="">
<textarea id="input_text" name="input_text"></textarea>
<input type="submit" name="decrypt" value="sm">
</form>
now I submit it, php is try to write $_POST['input_text'] to a file then do another action, after complete action, php 'll delete the file created.
<?php
$Path = dirname(__FILE__).'/temp/';
$File_NAME = time().'.txt';
$input_text = stripslashes($_POST['input_text']);
$fp=fopen($Path.$File_NAME,'w');
fwrite($fp,$input_text);
fclose($fp);
//do some curl action with the file, then delete the file
if(file_exists($Path.$File_NAME))
unlink($Path.$File_NAME);
but if the text too strong, user submit the form, then they abort the page, so the file doesn't delete.
I want to direct change the $_POST['input_text'] to type='file', but user also can use it such as a textarea. so php don't need to delete the file because it is a tmp file.
As per your Edit to the question, the possible solution is to first check the $_POST['input_text'] for length before even opening the file. If the text is too long, show an error message.
I don't think that file is even required in that case.
OLD ANSWER:
Fetching the $_POST['input_text], you can :
$txt = $_POST['input_text'];
$file = fopen("file.txt", "w+"); //w+ indicates read + write
fwrite($file,$txt); //to ride the 'input_txt'
Then perform the actions you want and finally delete the file if required using:
fclose($file);
delete("file.txt");
But make sure to grant the PHP page the permissions to Read/Write first.
if(isset($_POST['submit'])){
if(!empty($_FILES['files']['name'])){
$Zip = new ZipArchive();
$Zip->open('uploads.zip', ZIPARCHIVE::CREATE);
$UploadFolder = "uploads/";
foreach($_FILES['files'] as $file){
$Zip->addFile($UploadFolder.$file);
}
$Zip->close();
}
else {
echo "no files selected";
}
}
What is wrong here ? I have just watched a tutorial of creating archives and adding files in it but it is not working ...I am using php 5.4 . It is not even giving me any error. Can any one please guide me what i am doing wrong here.
Below is the form
<form action="" method="POST" enctype="multipart/form-data">
<label>Select files to upload</label>
<input type="file" name="files">
<input type="submit" name="submit" value="Add to archieve">
</form>
These lines don't make any sense
$UploadFolder = "uploads/";
foreach($_FILES['files'] as $file){
$Zip->addFile($UploadFolder.$file);
}
At that point in the code you posted, no uploaded files have been moved to a uploads/ directory, and looping though the $_FILES["files"] element - which is an associative array of various values, only one of which is the actual file name - and adding each value to the ZIP as a file, is nonsensical. - You should read the PHP docs relating to file uploading. It's clear you don't really know how PHP handles file uploads yet, which you should learn before attempting things like this.
One solution would be to move the uploaded file to the uploads/ directory using move_uploaded_file, but seeing as you are only really using the file there to add it to the archive, that step is pretty redundant; you can just add it directly from the temp location. First you need to verify it, though, which you can do with the is_uploaded_file function.
// Make sure the file is valid. (Security!)
if (is_uploaded_file($_FILES["files"]["tmp_name"])) {
// Add the file to the archive directly from it's
// temporary location. Pass the real name of the file
// as the second param, or the temporary name will be
// the one used inside the archive.
$Zip->addFile($_FILES["files"]["tmp_name"], $_FILES["files"]["name"]);
$Zip->close();
// Remove the temporary file. Always a good move when
// uploaded files are not moved to a new location.
// Make sure this happens AFTER the archive is closed.
unlink($_FILES["files"]["tmp_name"]);
}
I've been all over the internet reading up on APC, and it seems like a nifty way to detect file Uploading.
I am, however, having a problem.
I know how to call files and everything using Ajax, and that is what I am planning to do, but for Testing sake, I'm doing something like this.
Ok, so I have 3 files.
form.php
upload.php
status.php
form.php contains:
<input type="hidden" name="APC_UPLOAD_PROGRESS" value="1234" />
<input type="file" id="fileIn" name="file" />
(I am aware that I will need to use a unique ID in APC_UPLOAD_PROGRESS. Again, this is just for testings sake.)
Ok, Now Upload.php has the regular PHP upload script:
$origin = $_FILES['file']['name'];
if(move_uploaded_file(...etc...etc)...
And Status.php uses APC:
$upload = apc_fetch('upload_1234');
if ($upload) {
if ($upload['done'])
$percent = 100;
else if ($upload['total'] == 0)
$percent = 0;
else
$percent = $upload['current'] / $upload['total'] * 100;
echo $percent;
}
Now What I am doing is uploading a file using a regular HTTP method, and using another window to monitor Status.php.
The problem is; Status.php returns nothing!
However, If i write
print_r(apc_fetch('upload_1234'));
into upload.php, it returns the correct array, with all the details etc..
What am I doing wrong?
Thanks.
When this happens, something to check is that your hidden input element with the APC_UPLOAD_PROGRESS key is placed immediately before the file input in your form.
I know the form in the example above does do this, but it's easily missed in a more complicated form layout.