Permission denied in PHP - php

My script generates a directory for photos that user uploads but permissions for that generated directory shows as r----x--t even though i specify 777 in php
if (!is_dir($dir.$new_id)) {
$new_dir = $dir.$new_id.'/';
mkdir($new_dir, 777, true);
$thumbnail_dir = $new_dir.'thumbnail/';
if (!is_dir($thumbnail_dir)) {
mkdir($thumbnail_dir, 777, true);
}
}
I should mention that i'm on hostinger free account that is running centos OS.

set your umask temporarily to zero, the umask acts as a set of permissions that applications cannot set on files. Source : http://php.net/manual/en/function.umask.php
$old = umask(0);
if (!is_dir($dir.$new_id)) {
$new_dir = $dir.$new_id.'/';
mkdir($new_dir, 0777, true);
$thumbnail_dir = $new_dir.'thumbnail/';
if (!is_dir($thumbnail_dir)) {
mkdir($thumbnail_dir, 0777, true);
}
}
umask($old);
if ($old != umask()) {
die('An error occurred while changing back the umask');
}

Related

mkdir() function: No such file or directory

I have the code:
$path_directory = $_SERVER['DOCUMENT_ROOT']."/facebook/user/".$userid."/coverphoto/";
if(!file_exists($path_directory) && !is_dir($path_directory)){
mkdir($path_directory, true);
and it is telling me Warning: mkdir(): No such file or directory
and I do not know what to do.
Help is very much appreciated
You should use mkdir() like this
mkdir($path_directory, 0777, true);
FYI: https://www.php.net/manual/en/function.mkdir.php
And you may use below code to make sure you have enough permission first.
$path_directory = $_SERVER['DOCUMENT_ROOT']."/facebook";
mkdir($path_directory);
Try this
First check if the user has his own directory
$user_path_directory = $_SERVER['DOCUMENT_ROOT']."/facebook/user/".$userid;
if(!file_exists($user_path_directory) && !is_dir($user_path_directory)) {
mkdir($path_directory, true);
}
If the user has his own directory, lets create his coverphoto directory.
$path_directory = $user_path_directory."/coverphoto/"
if(!file_exists($path_directory) && !is_dir($path_directory)) {
mkdir($path_directory, true);
}

mkdir() not creating folder on the website

my php code is not creating the final folders which are (profile,post and cover) when hosted online but creates when hosted locally.Here is the code please help
<?php
if(isset($_POST['file']) && ($_POST['file']=='Upload'))
{
$path = "fh_users/Male/".$user."/Profile/";
$path2 = "fh_users/Male/".$user."/Post/";
$path3 = "fh_users/Male/".$user."/Cover/";
mkdir($path, 0, true );
mkdir($path2, 0, true);
mkdir($path3, 0, true);
$img_name=$_FILES['file']['name'];
$img_tmp_name=$_FILES['file']['tmp_name'];
$prod_img_path=$img_name;
move_uploaded_file($img_tmp_name,"fh_users/Male/".$user."/Profile/".$prod_img_path);
mysql_query("insert into user_profile_pic(user_id,image) values('$userid','$img_name')");
header("location:Secret_Question1.php");
}
?>
the second parameter of the mkdir() function is the permissions.
using mkdir($path, 0, true ); you create a new folder with 000 permissions (ie no permissions for anybody)
you should use mkdir($path, 0750, true ); instead which will give full permissions to the owner, read/execute permissions for group users and nothing for unknown users
The execute permission on a directory allows the affected user to enter the directory, and access files and directories inside

FTP permission denied using php script

I have a problem with my ftp account. Here is the code I have written to connect to my FTP and create a folder:
<?php
$ftp_server = "xxxxxxxxxxxxxxx";
$ftp_username = 'xxxxxxxx';
$ftp_password = 'xxxxxxxxxxxxxxx';
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_password);
// try to create directory $path
$path = '/2/7/7';
if(ftp_mkdir($ftp_conn, $path)) {
echo 'done';
die();
}
echo 'error';
?>
I get the following error when I run the code:
Warning: ftp_mkdir(): Permission denied
When I use filezilla or another ftp manager application I can create a folder easily but the problem occurs while using a script to handle it. I told to my network administrator to make sure if the account has read and write permissions. They say that read and write permissions have been already set. I guess that it probably needs execute permission as well which might not be set.
Is there any way to check the permission using php script? I also tried to check the folder permission using filezilla but it shows something like this:
try this solution it works recursive-ftp-make-directory
// recursive make directory function for ftp
function make_directory($ftp_stream, $dir)
{
// if directory already exists or can be immediately created return true
if (ftp_is_dir($ftp_stream, $dir) || #ftp_mkdir($ftp_stream, $dir)) return true;
// otherwise recursively try to make the directory
if (!make_directory($ftp_stream, dirname($dir))) return false;
// final step to create the directory
return ftp_mkdir($ftp_stream, $dir);
}
function ftp_is_dir($ftp_stream, $dir)
{
// get current directory
$original_directory = ftp_pwd($ftp_stream);
// test if you can change directory to $dir
// suppress errors in case $dir is not a file or not a directory
if ( #ftp_chdir( $ftp_stream, $dir ) ) {
// If it is a directory, then change the directory back to the original directory
ftp_chdir( $ftp_stream, $original_directory );
return true;
} else {
return false;
}
}
don't forget to add $dir='2/3/4/5/6' for new recursively directory!

Deleting a file through cli in vagrant give permission denied

I'm trying to run a php script from the command line in vagrant, but I'm getting a permission denied error when trying to delete a file.
Using the command sudo chmod 777 messages/ does not work, because I can't change the directory permissions, vagrant's shared folders don't allow that.
I also tried to use chmod and umask to change the directory's and file's permissions to be able to delete it, but to no avail.
$oldUMask = umask(0000);
chmod($dir, 0777);
chmod($file, 0777);
unlink($file);
umask($oldUMask);
How can I fix this? Am I missing something obvious?
The directory structure is like so:
/vagrant
/app
/messages
And the owner and group of the directory is www-data, vagrant is run with those user and group settings aswell.
This is the complete script:
<?php
require 'cli-start.php';
use CodeRichard\Config\Config;
use CodeRichard\Text\MessageInfo;
use CodeRichard\Text\TextMessage;
$dir = 'messages/';
$iterator = new DirectoryIterator($dir);
$file = null;
/** #var SplFileInfo $entry */
foreach($iterator as $entry)
{
$ext = $entry->getExtension();
if($entry->isDir() || strtolower($ext) != 'json')
{
continue;
}
$file = $entry->getRealPath();
break;
}
if($file != null)
{
$message = json_decode(file_get_contents($file), true)['message'];
$service = new Services_Twilio(Config::get('twilio.account_sid'), Config::get('twilio.auth_token'));
$messageInfo = new MessageInfo(Config::get('twilio.from_number'), Config::get('twilio.to_number'), $message);
$textMessage = new TextMessage($service, $messageInfo);
$status = $textMessage->send();
if($status['sent'])
{
$oldUMask = umask(0000);
chmod($dir, 0777);
chmod($file, 0777);
unlink($file);
umask($oldUMask);
}
echo $status['message'];
}
The known problem. The simplest solution is to modify in the Vagrantfile the synced folders settings:
config.vm.synced_folder "./htdocs", "/var/www", owner: "vagrant", group: "www-data", mount_options: ["dmode=775,fmode=664"]

Change folder permission to 777 using PHP temporarily

I have a Video folder on my server which has 755 permission. The problem is: when someone goes to upload video file, it can't be upload into that folder because of permission error.
If I change the permission to 777, then Video can be uploaded. But I don't want to allow the folder permission to 777 for security reason.
Is there any way in PHP to temporary change the permission to 777 while uploading video?
PHP provides a function, chmod() for the task.
Attempts to change the mode of the specified file to that given in mode.
You can put it in an if statement, and if it returns false, you can skip the upload file part.
The usage will be like
if( chmod($path, 0777) ) {
// more code
chmod($path, 0755);
}
else
echo "Couldn't do it.";
As described in the chmod function manual, the $mode must be in octal format - with leading zero, i.e chmod($path, 0777)
There is a way (PHP provides chmod function) but since PHP is not the owner of the folder, you won't be able to change the permission. And I think you are solving the wrong problem. Add webserver and PHP in the same group and give 775 to the folder.
You have to initialize the config for the upload, like this:
$config['remove_spaces'] = FALSE;
$config['upload_path'] = $path;
$this->upload->initialize($config);
$this->load->library('upload', $config);
You can use chmod() function.
For more information, try here
Warning: You cannot undo the file permissions that are changed by the script below. Proceed with extreme caution.
Important: this code should only be used if you remember to delete it immediately after use. As above, its use may put your site into an insecure state.
//replace dirname(__FILE__) with desired folder.
file_fix_directory(dirname(__FILE__));
function file_fix_directory($dir, $nomask = array('.', '..')) {
if (is_dir($dir)) {
// Try to make each directory world writable.
if (#chmod($dir, 0777)) {
echo "Made writable: " . $dir . "";
}
}
if (is_dir($dir) && $handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $nomask) && $file[0] != '.') {
if (is_dir("$dir/$file")) {
// Recurse into subdirectories
file_fix_directory("$dir/$file", $nomask);
}
else {
$filename = "$dir/$file";
// Try to make each file world writable.
if (#chmod($filename, 0666)) {
echo "Made writable: " . $filename . "";
}
}
}
}
closedir($handle);
}
}
or you can use terminal for this
chmod -R 755 public_html/test

Categories