Why is this php mkdir / uniqid not working? - php

I have this piece of code that should create a random directory and move uploads there:
$uploadPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . mkdir( 'assets/post/email_uploads/{uniqid(attachment_)}', 0777 ) . DIRECTORY_SEPARATOR . $_FILES[ 'file' ][ 'name' ];
The path assets/post/email_uploads/ already exists so the random folder should go inside email_uploads. The issue I'm facing is what to place between the DIRECTORY_SEPARATORs and have everything work.
When I try mkdir( 'assets/post/email_uploads/{uniqid(attachment_)}', 0777 ) OR
mkdir( 'assets/post/email_uploads/'.uniqid(attachment_), 0777 ) - The folder is not created and the upload is placed at the root.
When I try
$attchmentPath = 'assets/post/email_uploads/';
$uploadPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $attchmentPath.mkdir( uniqid(attachment_), 0777 ) . DIRECTORY_SEPARATOR . $_FILES[ 'file' ][ 'name' ];
OR
$attchmentPath = 'assets/post/email_uploads/';
$randomDir = mkdir( uniqid(attachment_), 0777 );
$newPath = $attchmentPath.$randomDir;
$uploadPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $newPath . DIRECTORY_SEPARATOR . $_FILES[ 'file' ][ 'name' ];
The folder is created at the root instead of the desired path and the file is not uploaded at all.

Perhaps like this? The content of uniqid should be quoted (unless it is a constant ) and the function call to uniqid needs to be escaped from the single quoted string
$dir=mkdir( __DIR__ . '/assets/post/email_uploads/'.uniqid('attachment_'), 0777 );
$name=$_FILES['file']['name'];
$uploadPath = $dir . DIRECTORY_SEPARATOR . $name;
You could try a recursive function to ensure that the directory path exists
function createpath( $path=NULL, $perm=0644 ) {
if( !file_exists( $path ) ) {
createpath( dirname( $path ) );
mkdir( $path, $perm, TRUE );
clearstatcache();
}
return $path;
}
$targetpath=__DIR__ . '/assets/post/email_uploads/'.uniqid( 'attachment_' );
$path=createpath( $targetpath );
echo $path;

I solved this by simply adding a parameter - TRUE in mkdir I had left out. So the functional code is - mkdir($path, 0777, TRUE) where $path is the path to the directory to be created.

Related

PHP file not copying correctly => file empty

I'm currently trying to copy a file from location A to B in PHP. The file get's copied but it has 0 Bytes. I'm so confused why this file is empty after this process. This is my code:
if ( ! file_exists( $file_dir . $file_category ) ) {
if ( ! mkdir( $file_dir . $file_category, 0777, true ) && ! is_dir( $file_dir . $file_category ) ) {
throw new \RuntimeException( sprintf( 'Directory "%s" was not created', $file_dir . $file_category ) );
}
$data = '<html><body bgcolor="#FFFFFF"></body></html>';
$file = fopen( $file_dir . $file_category . '/index.html', 'wb' );
fwrite( $file, $data );
fclose( $file );
$data = 'deny from all';
$file = fopen( $file_dir . $file_category . '/.htaccess', 'wb' );
fwrite( $file, $data );
fclose( $file );
}
copy( $output_dir . $filename, $file_dir . $file_category . '/' . $filename . '.pdf' );
Any help would be awesome.
is it just me or do you have switched the source and the destination file in your copy line:
copy( $output_dir . $filename, $file_dir . $file_category . '/' . $filename . '.pdf' );
PHP docs says that the parameters is like this:
copy ( string $source , string $dest [, resource $context ] ) : bool
but you first parameter uses "$output_dir" (might just be your variable name)
If this is not the case it would be helpful to know where you get "$filename" from since you are not validating it anywhere in your code, only "$file_category". Are you sure that your source file actually exists and has content?

PHP: Copy entire contents of a directory

Sorry for new post! I'm not yet trusted to comment on others posts.
I'm having trouble copying folders and this is where I started:
Copy entire contents of a directory
Function
function recurse_copy($src,$dst) {
$dir = opendir($src);
#mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
My input
$src = "http://$_SERVER[HTTP_HOST]/_template/function/";
$dst = "http://$_SERVER[HTTP_HOST]/city/department/function/";
recurse_copy($src, $dst);
I've also tried this
$src = "$_SERVER[DOCUMENT_ROOT]/_template/function/"; // And so on...
The function is executed but nothing is being copied.
Any ideas on what might be wrong?
SOLVED
Working solution
Along with
$src = "$_SERVER[DOCUMENT_ROOT]/_template/function/";
$dst = "$_SERVER[DOCUMENT_ROOT]/city/department/function/";
recurse_copy($src, $dst);
It's not tested but I think the issue might be that the target directory is not necessarily being created before attempting to copy files to it. The piece of code that creates the target directory would require a folder path rather than a full filepath - hence using dirname( $dst )
if( !defined('DS') ) define( 'DS', DIRECTORY_SEPARATOR );
function recurse_copy( $src, $dst ) {
$dir = opendir( $src );
#mkdir( dirname( $dst ) );
while( false !== ( $file = readdir( $dir ) ) ) {
if( $file != '.' && $file != '..' ) {
if( is_dir( $src . DS . $file ) ) {
recurse_copy( $src . DS . $file, $dst . DS . $file );
} else {
copy( $src . DS . $file, $dst . DS . $file );
}
}
}
closedir( $dir );
}
Use local paths
$src= "_template/function/";
$dst= "city/department/function/";
recurse_copy($src, $dst);
copy works locally on your server. You're trying to copy using HTTP scheme, it's not working that way.

File exists always returning false

My image directory is \webroot\files\thumbs
I am trying to add file_exists condition. For that I tried bellow code
$file = WWW_ROOT .'files' . DS . 'thumbs' . DS .'_'.$password->collection_id.'jpg';
$file_exists = file_exists($file);
It's always returning zero.
If I echo $file it's giving me output like this
c:\xampp\htdocs\myproject\files\thumbs\_61.jpg
You're missing the . before the extension. Update your $file definition as follows:
$file = WWW_ROOT .'files' . DS . 'thumbs' . DS .'_'.$password->collection_id.'.jpg';
// This was missing ---^
$file_exists = file_exists($file);

Copying files from multiple source to destination directories using PHP recursive copy function

The purpose of this question can be served by writing independent function for each source & destination directory in an include file but I'm looking for a better approach.
The following function copy files from one source directory to one destination directory.
How can I use this function to copy file from another source directory to destination directory?
Is array(); applicable here or explode(); shall be the right choice or none of these is applicable in this case?
if (isset($_POST['submit'])) {
$old_umask = umask(0);
if (!is_dir($dst)) mkdir($dst, 0777);
umask($old_umask);
function recurse_copy($src,$dst) {
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
//echo "$src";
}
$dir = $_POST['name'];
$src = "/home/user/public_html/directory/subdirectory/source/";
$dst = "/home/user/public_html/directory/subdirectory/destination/$dir/";
recurse_copy($src,$dst);
}

Autoload files when use require function

There's a magic function to autoload classes (__autoload), I want to know if there a way to load a file without a class.
Something like this:
require ('example_file'); // Trigger __autoloadfiles function
function __autoloadfiles($filename)
{
$files = array(
ROOT . DS . 'library' . DS . $filename. '.php',
ROOT . DS . 'application' . DS . $filename . '.php',
ROOT . DS . 'application/otherfolder' . DS . $filename. '.php'
);
$file_exists = FALSE;
foreach($files as $file) {
if( file_exists( $file ) ) {
require_once $file;
$file_exists = TRUE;
break;
}
}
if(!$file_exists)
die("File not found.");
}
You can define own function for requiring:
function require_file($file) {
// your code
}
and then call it
require_file('file');
I guess that there is no way to overload require function.

Categories