PHP file upload not working with dynamic path - php

i am trying to write a php upload script to upload files to different folders. My code works for direct path (something like 'path/to/directory') but not for dynamic path taken from runtime.
$directory_self = dirname($_SERVER['PHP_SELF']);
$folder = $_POST['folder_name']; //final folder
$toupload = $_SERVER['DOCUMENT_ROOT'] . $directory_self .'/files'. $folder;
$uploadsDirectory = str_replace (" ", "", $toupload);
When i echo $uploadsDirectory it shows the exact path. Could any one help me what could be wrong in this?

You should check and see if the folder is created and if the script has permission to write files there.
What is the exact output of the upload script? (i.e. what errors does it throw?)

Try using dirname(__FILE__);
<?php
$directory_self = dirname(__FILE__);
$folder = "faruk"; //final folder
$toupload = $_SERVER['DOCUMENT_ROOT'] . $directory_self .'/files/'. $folder;
$uploadsDirectory = trim($toupload);
echo $uploadsDirectory."\n";
?>
Output on my laptop;
/home/test/Desktop/files/test

Try adding some debug stuff to see if the path you're generating actually exists and is writeable:
$directory_self = dirname($_SERVER['PHP_SELF']);
$folder = $_POST['folder_name']; //final folder
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . $directory_self . '/files';
$uploadsDirectory = str_replace (" ", "", $uploaddir);
if (!is_dir($uploadDirectory)) {
die("$uploadDirectory is not a directory");
}
if (!is_writeable($uploadDirectory)) {
die("$uploaddir is not writeable");
}
$toupload = $uploadDirectory . $folder;
if (!is_writeable($toupload)) {
die("$toupload is not writeable");
}

Related

PHP define the destination folder for an upload

I use the following PHP script to upload an image to my server. Actually it moves the file in the same folder where my script is (root). I would like to move it into the folder root/imageUploads. Thank you for your hints!
$source = $_FILES["file-upload"]["tmp_name"];
$destination = $_FILES["file-upload"]["name"];
...
if ($error == "") {
if (!move_uploaded_file($source, $destination)) {
$error = "Error moving $source to $destination";
}
}
You will need to check if the destination folder exists.
$destination = $_SERVER['DOCUMENT_ROOT'] . '/imageUploads/'
if (! file_exists($destination)) { // if not exists
mkdir($destination, 0777, true); // create folder with read/write permission.
}
And then try to move the file
$filename = $_FILES["file-upload"]["name"];
move_uploaded_file($source, $destination . $filename);
So now your destination looks like this:
some-file.ext
and it's dir is same as file that executes it.
You need to append some dir path to current destination. E.g.:
$path = __DIR__ . '/../images/'; // Relative to current dir
$path = '/some/path/in/server/images'; // Absolute path. Start with / to mark as beginning from root dir
And then move_uploaded_file($source, $path . $destination)
Full path to the destination folder should be provided to avoid and path issue for moving uploaded files, I have added three variations for destination paths below
$uploadDirectory = "uploads";
// Gives the full directory path of current php file
$currentPath = dirname(__FILE__);
$source = $_FILES["file-upload"]["tmp_name"];
// If uploads directory exist in current folder
// DIRECTORY_SEPARATOR gices the directory seperation "/" for linux and "\" for windows
$destination = $currentPath.DIRECTORY_SEPARATOR.$uploadDirectory.DIRECTORY_SEPARATOR.$_FILES["file-upload"]["name"];
if (!move_uploaded_file($source, $destination)) {
echo $error = "Error moving $source to $destination";
}
// If to current folder where php script exist
$destination = $currentPath.DIRECTORY_SEPARATOR.$_FILES["file-upload"]["name"];
if (!move_uploaded_file($source, $destination)) {
echo $error = "Error moving $source to $destination";
}
// If uploads directory exist outside current folder
$destination = $currentPath.DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR.$uploadDirectory.DIRECTORY_SEPARATOR.$_FILES["file-upload"]["name"];
if (!move_uploaded_file($source, $destination)) {
echo $error = "Error moving $source to $destination";
}

How to check Image name in folder and rename it?

I have a csv file with image name and Prefix for image and I want to add prefix in that image name and rename it and move it to another directory.
<?php
$fullpath = '/var/www/html/john/Source/01-00228.jpg';
$additional = '3D_Perception_';
while (file_exists($fullpath)) {
$info = pathinfo($fullpath);
$fullpath = $info['dirname'] . '/' . $additional
. $info['filename']
. '.' . $info['extension'];
echo $fullpath ;
}
?>
Here Image file is store in Source Directory I want to rename it with some prefix and move it other directory like Destination
Please help me out to find solution for this.
<?php
$source_directory = '/var/www/html/john/Source/';
$dest_directory = '/var/www/html/john/Source/'; // Assuming you'll change it
$filename = '01-00228.jpg';
$prefix = '3D_Perception_';
$file = $source_directory . $filename;
$dest_file = $dest_directory . $prefix . $filename;
if (file_exists($file)) {
if (copy($file, $dest_file)) {
unlink($file); // Deleting source file.
var_dump(pathinfo($dest_file)); // Dump dest file infos, replace it with wathever you want to do.
} else {
// if copy doesn't work, do something.
}
}
?>
Try this, and take care about my comments. ;)

PHP - move_uploaded_file not working to copy file in same folder

Having some trouble with rewriting a photo file. I need the file name to get rewritten as a random string. The file uploads fine - I can't seem to get it copy the file and rewrite the file name to the random string. The file is going to stay in the directory.
The function is working fine and I can rewrite file name in the database, but it will not rewrite the actual file in the folder. The folder permissions are rwxr-xr-x (755).
Any thoughts?
function AfterUpdate(){
$file = $this->file_attachment;
$path_parts = pathinfo($file);
$newFilename = $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];
$file_src = $_SERVER['DOCUMENT_ROOT'] . $file;
$newfile_src = $_SERVER['DOCUMENT_ROOT'] . $newFilename;
if (move_uploaded_file($file_src, $newfile_src)){
$this->file_attachment = $newFilename;
}
}
$newFilename contains a path location I guess by looking at the $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];.
$newFilename should just be the new file name with extension.
move_uploaded_file will only move files from one folder to another or the same, that already exists. But will not create a folder for you.
Simple fix. Replace move_uploaded_file with rename. The file will not be moved, just renamed.
$file = $this->file_attachment;
$path_parts = pathinfo($file);
$newFilename = $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];
$file_src = $_SERVER['DOCUMENT_ROOT'] . "/" . $file;
$newfile_src = $_SERVER['DOCUMENT_ROOT'] . "/" . $newFilename;
if (rename($file_src, $newfile_src)){
$this->file_attachment = $newFilename;
}

Move file into a specific folder

I have a question regarding file handle, i have:
Files:
"Mark, 123456, HTCOM.pdf"
"John, 409721, JESOA.pdf
Folders:
"Mark, 123456"
"Mark, 345212"
"Mark, 645352"
"John, 409721"
"John, 235212"
"John, 124554"
I need a routine to move files to correct folders.
In case above, i need to compare 1st and second value from file and folder. If are the same i move the file.
Complement to post:
I have this code, work right but i need to modify to check name and code to move files...
I'm confused to implement function...
$pathToFiles = 'files folder';
$pathToDirs = 'subfolders';
foreach (glob($pathToFiles . DIRECTORY_SEPARATOR . '*.pdf') as $oldname)
{
if (is_dir($dir = $pathToDirs . DIRECTORY_SEPARATOR . pathinfo($oldname, PATHINFO_FILENAME)))
{
$newname = $dir . DIRECTORY_SEPARATOR . pathinfo($oldname, PATHINFO_BASENAME);
rename($oldname, $newname);
}
}
As a rough-draft and something that will only work with your specific case (or any other case following the same naming pattern), this should work:
<?php
// define a more convenient variable for the separator
define('DS', DIRECTORY_SEPARATOR);
$pathToFiles = 'files folder';
$pathToDirs = 'subfolders';
// get a list of all .pdf files we're looking for
$files = glob($pathToFiles . DS . '*.pdf');
foreach ($files as $origPath) {
// get the name of the file from the current path and remove any trailing slashes
$file = trim(substr($origPath, strrpos($origPath, DS)), DS);
// get the folder-name from the filename, following the pattern "(Name, Number), word.pdf"
$folder = substr($file, 0, strrpos($file, ','));
// if a folder exists matching this file, move this file to that folder!
if (is_dir($pathToDirs . DS . $folder)) {
$newPath = $pathToDirs . DS . $folder . DS . $file;
rename($origPath, $newPath);
}
}

permissions on a new folder in php

My code looks like this:
do
{
$rand = rand(1,10000000);
$name_of_file_clear = $rand ;
$name_of_file = $rand . '.JPG' ;
$name_of_file_t = $rand . '_t.JPG' ; // for thubnsdffafasf
$this_directory_path = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
$images_directory_path = $_SERVER['DOCUMENT_ROOT'] . $this_directory_path . 'img/' . $name_of_file_clear . '/';
$whole_path = $images_directory_path.$name_of_file;
} while (file_exists($whole_path));
mkdir($images_directory_path, 777);
chmod($images_directory_path, 777);
move_uploaded_file($temp_file_name, $whole_path);
The problem is, when I try uploading file, the file is not uploaded. I think the problem is something with the permissions. Help me please, I am new to php.
You're specifying them in decimal. Try specifying them in octal instead.
mkdir($images_directory_path, 0777);

Categories