I'm trying to create a directory using Codeigniter. But it gives me this error:
Message: mkdir() [function.mkdir]: Permission denied.
Here's the code:
$imageDir = $_SERVER['DOCUMENT_ROOT'] . DS . 'public' . DS . 'images' . DS . 'products';
$userDir = $imageDir . DS . $userId;
if (!is_file($userDir) && !is_dir($userDir)) {
if (!mkdir($userDir)) {
return false;
}
return true;
}
I already changed the permissions to 777 but the problem still stands.
Try
$imageDir = $_SERVER['DOCUMENT_ROOT'] .'/public/images/products/';
$userDir = $imageDir . $userId;
and try to echo $userDir so that you can find the path where it is making the directory.
Related
So I want to be able to move the uploaded attachments to the specified directory depending on the month inside the year.
I'm getting the following two errors:
Warning: move_uploaded_file(): The second argument to copy() function cannot be a directory
Warning: move_uploaded_file(): Unable to move '/private/var/tmp/phptmfv0g' to 'uploads/2020/01'
Code:
$dir = "uploads/" . date('Y') . '/' . date('m');
if (!file_exists($dir)) {
mkdir($dir, 0755, true);
}
move_uploaded_file($_FILES['rtk']['tmp_name']['5']['file'], $dir);
So it creates the directory to build the folders, but I can't seem to move my contact form files into that directory at all - What could I be doing different?
This used to work by placing all the attachments in the same directory:
$file = "uploads/" . basename($this->get_last_name() . " - " . $this->get_first_name() . " - " . date("Y-m-d h:i:sa"));
move_uploaded_file($_FILES['rtk']['tmp_name']['5']['file'], $file);
You need to give the directory and filename for the destination:
$dir = "uploads/" . date('Y') . '/' . date('m');
$file = basename($_FILES['rtk']['name']['5']['file']);
if (!file_exists($dir)) {
mkdir($dir, 0755, true);
}
move_uploaded_file($_FILES['rtk']['tmp_name']['5']['file'], "$dir/$file");
Or if they exist, use the object methods as you did previously to build a filename:
$file = $this->get_last_name() . "-" . $this->get_first_name() . "-" . date("Y-m-d h:i:sa");
Exactly as the error says, the path you are trying to copy to is a directory not a file path
The problem is not about the month
It's that the path ends up as: uploads/2020/01
While it should be: uploads/2020/01/filename.ext
You can get original file name with $_FILES['file']['name'] --> $_FILES['rtk']['name']['5']['file']
$dir = "uploads/" . date('Y') . '/' . date('m');
$fileName = $_FILES['rtk']['name']['5']['file'];
$fullPath = $dir . '/' . $fileName;
if (!file_exists($dir)) {
mkdir($dir, 0755, true);
}
move_uploaded_file($_FILES['rtk']['tmp_name']['5']['file'], $fullPath);
--- To prevent users from replacing each other files you can add a unique prefix, for example just using uniqid()
$dir = "uploads/" . date('Y') . '/' . date('m');
$fileName = uniqid() . '_' . $_FILES['rtk']['name']['5']['file'];
$fullPath = $dir . '/' . $fileName;
if (!file_exists($dir)) {
mkdir($dir, 0755, true);
}
move_uploaded_file($_FILES['rtk']['tmp_name']['5']['file'], $fullPath);
I use laravel 5.6
I want to upload files on 2 folders at once. So if the image uploaded successfully in the product folder, I want the image uploaded in the thumbs folder as well
I try like this :
public function uploadImage($file)
{
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPath = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product';
if(!File::exists($destinationPath)) {
File::makeDirectory($destinationPath, 0755, true);
}
$file->move($destinationPath, $fileName);
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
If the code run, it just success to upload in the product folder. It did not upload in the thumbs folder
There exist error like this :
message Unable to find file (/tmp/phpUSxbEJ).
exception Intervention\Image\Exception\NotReadableException
I try run this code :
public function uploadImage($file)
{
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
So I remove code to upload in product folder. I try it and it works. It success upload on the thumbs folder
So I think in one process, it only upload in one folder
Is there any other way to upload on 2 folders?
You first upload the temporary file which will be removed once you save it on your disk, thats why u can't reuse it, instead of reusing it, you fetch the saved image and do resizing on it and save it with a different name:
public function uploadImage($file)
{
...
$file->move($destinationPath, $fileName);
//$file here doesn't exist anymore, hence it can't be read
...
$uploadedFile = Storage::get($destinationPath . DIRECTORY_SEPARATOR . $filename);
$image_resize = Image::make($uploadedFile);
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
I find a solution
I try like this :
$file = $file->move($destinationPath, $fileName);
It works
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);
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.
Trying to autoload classes from <root>/incl/classes folder.
The problem is, when I call some class for ex. ip like that
$ip= new ip();
$ip=$ip->get();
PHP gives error message Notice: Undefined variable: path . But in fact file already exists
I'm declaring all various paths at the top of page.
define("ds", DIRECTORY_SEPARATOR);
$path = array();
$path['root'] = $_SERVER['DOCUMENT_ROOT'];
$path['common'] = $path['root'] . ds . "common";
$path['design'] = $path['root'] . ds . "design";
$path['contents'] = $path['root'] . ds . "contents";
$path['content_images'] = $path['root'] . ds . "content" . ds . "images";
$path['design_images'] = $path['root'] . ds . "design" . ds . "images";
$path['blocks'] = $path['contents'] . ds . "blocks";
$path['includes'] = $path['root'] . ds . "incl";
$path['pages'] = $path['contents'] . ds . "pages";
$path['classes'] = $path['includes'] . ds . "classes";
$files = glob("common" . ds . "*.php");
array_unshift($files, $path['common'] . ds . "settings.php", $path['common'] . ds . "db.php");
foreach ($files as $filename)
require_once $filename;
//Auto loading classes
function __autoload($class_name) {
if (file_exists($path['classes'] . ds . $class_name . '.php')) {
require_once($path['classes']. ds . $class_name . '.php');
} else {
die($path['classes'] . ds . $class_name . '.php');
}
}
For testing purposes added die($path['classes'] . ds . $class_name . '.php'); line. It outputs \ip.php. I wonder, why it doesn't echo $path['classes'] even if I declared it before ?
It's a scoping issue. Your $path variable doesn't exist in your autoload function. It is a global variable, and you need to explicitly "invite" it:
function __autoload($class_name) {
global $path;
You should actually have gotten a notice about this.