Move uploaded file inside a server - php

I'm trying to move an uploaded file inside my server on my Cpanel hosting. I am using the PHP function move_uploaded_file, but how do I know the exact path of my Server?
I tried using this syntax:
move_uploaded_file($filelocation, http://website.com/thisfolder/filename.txt);
i do know that my file location variable is outputting correctly as I could see that it goes into var/ directory of the server.
I want the path in my filemanager that has the public_html folder.
/ServerName1/ServerName2/public_html/Folder1/thisfolder
How do I place it into the correct directory?

<?php
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
?>
A sample code of how to upload to a directory. You can find more examples here : http://us1.php.net/move_uploaded_file

Related

upload image on a parallel directory's folder

I have two directories:
/publichtml
/csp.example.com
My code is in csp.example.com/classes/ep-class.php and I want to upload my file on /publichtml/upload/photo folder. How can I achieve that also /publichtml is accessible using example.com so I am taking target directory as a absolute url?
Here is my code:
if(isset($_FILES["imagefile"]["type"][0]))
{
$filename=$_FILES["imagefile"]["name"][0];
// Storing source path of the file in a variable
$sourcePath = $_FILES['imagefile']['tmp_name'][0];
// Target path where file is to be stored
$targetPath = SITEURL."upload/photo/".$filename;
if(move_uploaded_file($sourcePath,$targetPath))
{
return "Yes";
}
else
{
echo $_FILES["imagefile"]["errors"][0];
die("File upload error !");
}
}
Any help would be highly appreciated.

Upload Multiple Files HTML5 / PHP

I'm trying to build a basic upload form to add multiple files to a folder, which is processed by PHP.
The HTML code I have is:
<form id="form" action="add-files-php.php" method="POST" enctype="multipart/form-data">
<div class="addSection">Files To Add:<br><input type="file" name="files[]" multiple /></div>
<div class="addSection"><input type="submit" name="submit" value="Add Files" /></div>
</form>
And the PHP to process is:
$file_path = "../a/files/article-files/$year/$month/";
foreach ($_FILES['files']['files'] as $file) {
move_uploaded_file($_FILES["file"]["name"],"$file_path");
}
I can run the PHP without any errors, but the files don't get added to the path folder.
Where am I going wrong with this?
I have a similar code actually in one of my projects. Try it.
foreach ($_FILES['files']['name'] as $f => $name) {
move_uploaded_file($_FILES["files"]["tmp_name"][$f], $file_path);
}
Look at the following page:
http://php.net/manual/en/function.move-uploaded-file.php
EDIT:
Nowhere in the code you provided, does it show that you actually give your file a filename, you simply refer to a path, rather than a path+filename+extension
move_uploaded_file($_FILES["files"]["tmp_name"][$f], $file_path . $name);
modifying my original code sample to be like the second one, should work.
Iterate the $_FILES['files']['error'] array and check if the files are actually uploaded to the server:
$dest_dir = "../a/files/article-files/$year/$month";
foreach ($_FILES["files"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
// The temporary filename of the file stored on the server
$tmp_name = $_FILES["files"]["tmp_name"][$key];
$name = basename($_FILES["files"]["name"][$key]);
// Handle possible failure of the move_uploaded_file() function, too!
if (! move_uploaded_file($tmp_name, "$dest_dir/$name")) {
trigger_error("Failed to move $tmp_name to $dest_dir/$name",
E_USER_WARNING);
}
} else {
// Handle upload error
trigger_error("Upload failed, key: $key, error: $error",
E_USER_WARNING);
}
}
The biggest issue with your code is that you are trying to move $_FILES['files']['name'] instead of $_FILES['files']['tmp_name']. The latter is a file name of temporary file uploaded into the temporary directory used for storing files when doing file upload.
P.S.
Using relative paths is error-prone. Consider using absolute paths with the help of a constant containing path to the project root, e.g.:
config.php
<?php
define('MY_PROJECT_ROOT', __DIR__);
upload.php
<?php
require_once '../some/path/to/project/root/config.php';
$dest_dir = MY_PROJECT_ROOT . "/a/files/article-files/$year/$month";

how to make upload file and folder/subfolder to dropbox using php

i use dropphp for use upload file to dropbox and works fine , but now i want upload file and folder/subfolder to dropbox , when folder/subfolder and files make same name in dropbox path location...
i found this code from here
// Set the timezone so filenames are correct
date_default_timezone_set('Europe/London');
// Backup all files in public_html apart from the gz
$siteroot = "/path/to/backup";
$dropbox_email='dropbox#email'; //Dropbox username
$dropbox_pass='pass'; // Dropbox password
include("DropboxUploader.php");
$uploader = new DropboxUploader($dropbox_email, $dropbox_pass);
function FolderToDropbox($dir, $dropbox_link){
$dropbox_folder = 'FolderInDropboxRoot/';
$files = scandir($dir);
foreach($files as $item){
if($item != '.' && $item != '..'){
if(is_dir($dir.'/'.$item)) FolderToDropbox($dir.'/'.$item,$dropbox_link);
else if(is_file($dir.'/'.$item)) {
$clean_dir = str_replace("/path/to/backup", "", $dir);
$dropbox_link->upload($dir.'/'.$item,$dropbox_folder.$clean_dir.'/');
}
}
}
}
FolderToDropbox($siteroot,$uploader);
but not work, maybe same change new api in
dropboxuploader.php
How can the code be modified for dropphp or using dropbox SDK ?
thank you

php upload file into the folder

I am trying to upload multiple images into the folder using php . The code can print out the file names which means I get the files but now it does not upload them and I get no error : below is my code
<?php
$target = "image_uploads/";
if(isset($_FILES['FILE_NAME'])){
foreach($_FILES['FILE_NAME']['tmp_name']as $key => $error ){
print_r($key);
$file_upload = $key.$_FILES['FILE_NAME']['name'][$key];
#print image names
echo $file_upload.'</br>';
move_uploaded_file($file_upload,$target);
}
}
?>
In target you have to give the file name too. Please use the code below,
$target = "image_uploads/";
if(isset($_FILES['FILE_NAME'])){
foreach($_FILES['FILE_NAME']['tmp_name'] as $key => $error ){
print_r($key);
$file_upload = $key.$_FILES['FILE_NAME']['name'][$key];
print image names
echo $file_upload.'</br>';
move_uploaded_file($file_upload,$target.$_FILES['FILE_NAME']['name']);
}
}
I think the problem is in the foreach loop.
foreach ($_FILES['FILE_NAME']['tmp_name'] as $key => $val) {
// this loops through the tmp_name of $_FILES['FILE_NAME']
// which is a string
}
I think you meant something like:
foreach ($_FILES as $index => $fileArray) {
$tmpName = $fileArray['tmp_name'];
echo "File at key $index is temporarily uploaded at $tmpName";
}
The code above will loop through all uploaded files and print it's current filename.
It might happen that your target folder is not writable.
I also think, that the cause of which you're not getting errors is, that you have the following:
print_r($key);
Yous should have:
print_r($error);
There can be multiple reasons for this :
The target folder must exist before trying to move the file from temp location to the target and must also be writable
the move_uploaded_file takes the second argument as the file name followed by the directory name, so it can be something like : target folder/user.file.name.ext
If you are uploading multiple files, then the $_FILES must be accessed as shown in the link : http://php.net/manual/en/features.file-upload.multiple.php
for the php error messages that you may encounter, here is a list : http://php.net/manual/en/features.file-upload.errors.php

How can I move a file to another folder using php?

I have an upload form where users can upload images that are currently being uploaded to a folder I made called 'temp' and their locations are saved in an array called $_SESSION['uploaded_photos']. Once the user pushes the 'Next Page' button, I want it to move the files to a new folder that is dynamically created right before that.
if(isset($_POST['next_page'])) {
if (!is_dir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id'])) {
mkdir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id']);
}
foreach($_SESSION['uploaded_photos'] as $key => $value) {
$target_path = '../images/uploads/listers/'.$_SESSION['loggedin_lister_id'].'/';
$target_path = $target_path . basename($value);
if(move_uploaded_file($value, $target_path)) {
echo "The file ". basename($value). " has been uploaded<br />";
} else{
echo "There was an error uploading the file, please try again!";
}
} //end foreach
} //end if isset next_page
An example for a $value that is being used is:
../images/uploads/temp/IMG_0002.jpg
And an example of a $target_path that is being used is:
../images/uploads/listers/186/IMG_0002.jpg
I can see the file sitting in the temp folder, both of those paths look good to me and I checked to make sure that the mkdir function actually created the folder which it did well.
How can I move a file to another folder using php?
As I read your scenario, it looks like you've handled the upload and moved the files to your 'temp' folder, and now you want to move the file when they perfom a new action (clicking on the Next button).
As far as PHP is concerned - the files in your 'temp' are not uploaded files anymore, so you can no longer use move_uploaded_file.
All you need to do is use rename:
if(isset($_POST['next_page'])) {
if (!is_dir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id'])) {
mkdir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id']);
}
foreach($_SESSION['uploaded_photos'] as $key => $value) {
$target_path = '../images/uploads/listers/'.$_SESSION['loggedin_lister_id'].'/';
$target_path = $target_path . basename($value);
if(rename($value, $target_path)) {
echo "The file ". basename($value). " has been uploaded<br />";
} else{
echo "There was an error uploading the file, please try again!";
}
} //end foreach
} //end if isset next_page

Categories