Warning: mkdir(): No such file or directory - php

Trying to create a new dir using:
$date = date('MDY');
$fileName = $date . '-organizations.xlsx';
$directory = "/guard/API/office/Storage/Custom Reports/";
if (!file_exists($directory) && !is_dir($directory)) {
mkdir($directory, 0777, true);
}
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save($directory . $fileName);
return $fileName;
But the error is:
Warning: mkdir(): Permission denied in \somedir\...
Could it be an issue of permissions on my local dev machine? I will NOT be able to set up permissions on the server.
Thanks!

Because you tried to make directory from root path. Try two way to fix error.
1.Change directory path from currently to right:
$directory = __DIR__."/guard/API/office/Storage/Custom Reports/";
Try this as root user:
exec ('sudo mkdir -m 777 '.$directory);

Related

ZipArchive ExtractTo() Permission Denied

I'm trying to extract zip files from a folder into a subfolder. The error I'm recieving is:
Warning: ZipArchive::extractTo(XXXX): failed to open stream: Permission denied in /var/www/html/update_alerts2.php on line 97
extracted
Code :
public function extractFiles($inputDir,$outputDir) {
$files_to_extract = $this->getFiles($inputDir);
$zip = new ZipArchive();
foreach ($files_to_extract as $file) {
echo $file;
$res = $zip->open($inputDir . $file);
if ($res) {
$zip->extractTo($outputDir);
$zip->deleteName($inputDir . $file);
$zip->close();
} else {
echo 'failed, code:' . $res;
}
}
}
I've tried setting permissions to 777 but I still get the error. What are the possible ways I can fix this issue (I'm using centos7 and apache) ?

Permission denied in /opt/lampp/htdocs/ when writing a file in PHP

When a user creates a profile and uploads a profile picture, the PHP "move_uploaded_file" function is used to move a temporary copy of this image to the project directory.
While my current code works perfectly fine on my Windows 10 machine, it doesn't work on my Mac because of incorrect file permissions.
I have already tried the method provided in other answers and nothing changes after running the terminal command:
sudo chmod 644 /opt/lampp/htdocs/yourfolder
This is true for any variation including:
sudo chmod -R 777 /opt/lampp/htdocs/yourfolder
Note that I DO have admin permissions on the laptop
Other users have stated that these commands have fixed their problems, so I suspect that mine is different.
PHP Code Snippet:
$username = $_POST["username"];
$password = $_POST["password"];
$ext = null;
/* Debugging information
echo $_FILES["profile_pic"]["name"]."<br>";
echo $_FILES["profile_pic"]["tmp_name"]."<br>";
echo $_FILES["profile_pic"]["size"]."<br>";
echo $_FILES["profile_pic"]["error"]."<br>";
*/
if (strrpos($_FILES["profile_pic"]["name"], ".jpg") != null || strrpos($_FILES["profile_pic"]["name"], ".jpeg") != null) {
$ext = ".jpg";
};
if (strrpos($_FILES["profile_pic"]["name"], ".png") != null) {
$ext = ".png";
};
$path = __DIR__ . "\data\users\images\\" . uniqid() . $ext;
if (move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $path)) {
echo("File successfully uploaded");
} else {
echo("Upload failed");
};
Returned: Upload failed
Error thrown:
move_uploaded_file(/opt/lampp/htdocs/Test-Site\data\users\images\5c1dadd52bb71.jpg): failed to open stream: Permission denied in /opt/lampp/htdocs/Test-Site/signup_confirm.php on line 27
Expected: user profile picture is inserted into the .../htdocs/Test-Site/data/users/images folder
Actual: above error about incorrect file permissions is thrown
The reason for the error is that I was using incorrect directory separators ("\" instead of "/" for Mac). Changing these made the profile picture move to the intended location.

cakephp - create new directory inside webroot

I want to create a new directory(folder) inside the webroot so i can create/edit files inside that new folder.
My code is:
$path = 'files' . DS . 'pathtofolder';// $folder_name;
$folder = new Folder($path);
if (!is_null($folder->path)) { //this is to verify if created
echo "Exists";
}
else{
echo "Doesnt exist";
}
The result is always doesnt exist. And i cant find any created folder inside my cakephp folders.
I tried to change 'files' for 'webroot' but it doesnt work.
Is this the correct code to create a directory?
try this
$path = 'files' . DS . 'pathtofolder';
$folder = new Folder($path, true, 0755);
if ($folder->path) {
echo "Exists";
}
else{
echo "Doesnt exist";
}
There is a direct PHP call to create directories. So, you can use that. I dont know the cakephp version.
if (!mkdir($structure, 0777, true)) {
die('Failed to create folders...');
}
Also, check if your web-server user www-data has permissions to create directories. Otherwise, on command prompt run this command
addgroup www-data
That will add www-data to enable permissions. You might need to add sudo in some cases to above command if you get permission denied error.
$path = WWW_ROOT . 'webroot' . DS . 'FolderNameYouWantToSet' . DS;
if (!file_exists($path)) {
$oldMask = umask(0);
mkdir($path, 0777, true);
chmod($path, 0777);
umask($oldMask);
}
This will create a folder with 777 permission, you can set the folder permission as per your requirement.
WWW_ROOT is the global cake variable which stores the path up to the project.

How to create a folder in site->default->files with write read permissions with Drupal?

I'm working on Ubuntu and I need to create a folder in Drupal's default->files folder with write permission so that I would be able to add files to that folder later on.
Here is the code I have:
drupal_mkdir('public://' . $new_dir . '/');
$file = file_copy($file, 'public://' . $new_dir . '/' . $file_name);
drupal_mkdir('public://' . $new_dir , 0777);
if you need it to be recursive set third argument to true.
update:
$oldumask = umask(0);
drupal_mkdir("public://". $new_dir , 0777);
umask($oldumask);
fire this command and than try your code ..
chmod -R 0700 sites/default/files

Connect to remote afp server using PHP

I'm trying to make a php script to connect to an afp server and get a directory listing (with each file size). The server is local in our office, but I'm unable to just make a script on the afp server side. On my machine, I use something like this:
$filesInDir = array();
$filesInMySQL = array();
if (is_dir($uploadDir)) {
$dh = opendir($uploadDir);
if ($dh) {
$file = readdir($dh);
while ($file != false) {
$path = $uploadDir . "/" . $file;
$type = filetype($path);
if ($type == "file" && $file != ".DS_Store" && $file != "index.php") {
$filesInDir[] = $file;
}
$file = readdir($dh);
}
closedir($dh);
} else {
echo "Can't open dir " . $uploadDir;
}
} else {
echo $uploadDir . " is not a folder";
}
But I can't connect to the afp server. I've looked into fopen it doesn't allow afp, and I don't think it'd allow directory listing:
opendir("afp://ServerName/path/to/dir/");
Warning: opendir() [function.opendir]: Unable to find the wrapper "afp" - did you forget to enable it when you configured PHP? in...
Warning: opendir(afp://ServerName/path/to/dir/) [function.opendir]: failed to open dir: No such file or directory in...`
I'm not looking to see if a file exists, but to get the entire directory listing. Eventually I'll also have to remotely copy files into an output directory.
eg.
mkdir afp://ServerName/output/output001/
cp afp://ServerName/path/to/dir/neededfile.txt afp://ServerName/output/output001/
Maybe use http://sourceforge.net/projects/afpfs-ng/ to mount it...
I'm developing on an Mac Mini, so I realised I could just mount the afp share, and use readdir. I had to mount the drive using the following:
sudo -u _www mkdir /Volumes/idisk
sudo -u _www mount_afp -i afp://<IP>/sharename/ /Volumes/idisk/
Further details here

Categories