Can not able to create directory in host server - php

I am trying to create a folder in server. I wrote the following code
function CreateErrorLog($IntegrationId, $errorStr){
if (!is_dir(PUBLIC_PATH.'/errors/'.$IntegrationId)) {
echo 'not present<br/><br/>';
try{
mkdir(PUBLIC_PATH.'/errors/'.$IntegrationId, 0777);
}catch(Exception $e){
echo '<br/><br/> exception----'.$e.'<br/><br/>';
print_r($e);
}
}else{
echo 'present<br/><br/>';
}
}
I am getting output "not present" but I can`t see the directory created in the name of $IntegrationId under "errors" folder.
this is working in localhost. After moving to remote server I am getting the problem. What is the problem?

Issue can be errors folder might not be having write permission in server
If no permission, it will not through any exception.
Put the mkdir in if() condition. So you will understand from the return value.
And put ini_set('display_errors', 1); on top of your code.

Firstly check your path permission is writable.If this fine,you must mention absolute path instead of PUBLIC_PATH.you can get absolute path create a php file using function 'echo getcwd()'.This will show you absolute path.This choose on instead of PUBLIC_PATH.

Related

permission denied when i try to unlink an image in the folder using php [duplicate]

I make a site and it has this feature to upload a file and that file is uploaded to a server
Im just a newbie to php I download xampp and I run this site that i made in my local machine.
My site is like this you upload a file then that file will be uploaded to a server, but when i tried unlink() because when i try to remove the filename to a database I also want to remove that pic on the server, but instead I got an error and it says "Permission denied".
question:
How can I got permission to use unlink();?
I only run this on my localmachine using xampp
Permission denied error happens because you're trying to delete a file without having enough/right permissions for doing that.
To do this you must be using superuser account or be the same user that have uploaded the file.
You can go to the directory from your command line and check the permissions that are set to the file.
The easiest solution is to loggin as administrator/root and delete the file.
Here is another work around:
// define if we under Windows
$tmp = dirname(__FILE__);
if (strpos($tmp, '/', 0)!==false) {
define('WINDOWS_SERVER', false);
} else {
define('WINDOWS_SERVER', true);
}
$deleteError = 0;
if (!WINDOWS_SERVER) {
if (!unlink($fileName)) {
$deleteError = 1;
}
} else {
$lines = array();
exec("DEL /F/Q \"$fileName\"", $lines, $deleteError);
}
if ($deleteError) {
echo 'file delete error';
}
And some more: PHP Manual, unlink(), Post 106952
I would recommend, always first to check PHP Manual (in case your question concerns PHP), just go to the page with function that you have problems with and just click search CTRL+F in your browser and enter, for example, Windows, and as a result, in your case, you would find at least 7 related posts to that or very close to that what you were looking for.
Read this URL
How to use Unlink() function
I found this information in the comments of the function unlink()
Under Windows System and Apache, denied access to file is an usual error to unlink file. To delete file you must to change file's owern. An example:
<?php
chown($TempDirectory."/".$FileName,666); //Insert an Invalid UserId to set to Nobody Owern; 666 is my standard for "Nobody"
unlink($TempDirectory."/".$FileName);
?>
So try something like this:
$Path = './doc/stuffs/sample.docx';
chown($Path, 666);
if ( unlink($Path) )
echo "success";
else
echo "fail";
EDIT 1
Try to use this in the path:
$Path = '.'.DIRECTORY_SEPARATOR.'doc'.DIRECTORY_SEPARATOR.'stuffs'.DIRECTORY_SEPARATOR.'sample.docx';

How to change permission upload file image?

I am new to PHP, I am doing the image upload function, the problem I have is that although successfully uploaded I don't have access to that image, is there any solution for this is not ?
my code:
public static function uploadImage($image){
try {
$rand = rand(1, 1000);
$target_save = "./Public/upload/".$rand.Helpers::slug($image['name']);
move_uploaded_file($image['tmp_name'], $target_save);
chmod($target_save, 777);
return Helpers::getUrlPage().trim($target_save, "./");
} catch (Exception $e){
return Helpers::getPathPublic('admin')."images/no_image.webp";
}
}
notification I get when opening pictures on my computer
It looks like you don t have permission to view this file check the permissions and try again
I tried running my application on xampp completely without this problem, but if I use MAMP PRO I will have this problem.
Have you checked the permissions of your file afterwards?
It seems like you are using an incorrect chmod mode
It is stated in the chmod documentation, that you should use octal values as secong argument.
An example:
chmod($target_save, 0777);
Also make sure the reading user has the correct permissions to read the file.

PHP - Creating .txt document in Ubuntu

I am having trouble using fopen() to create a text document for later use as a cookie file.
I have read the documentation for this function, but to no avail.
Notes:
Ubuntu
read / writable ("w+")
I have tried several storage locations including:
/home/jack/Desktop/cookie
/var/www/cookie
/home/jack/Documents/cookie
PHP
echo "debug";
echo "\r\n";
$cookie = fopen("/home/jack/Documents/cookie", "w+");
fclose($cookie);
if(!file_exists($cookie) || !is_writable($cookie))
{
if(!file_exists($cookie))
{
echo 'Cookie file does not exist.';
}
if(!is_writable($cookie))
{
echo 'Cookie file is not writable.';
}
exit;
}
Result
file is not created
Output to browser: debug Cookie file does not exist.Cookie file is not writable.
Other Fun Facts
I have tried using fopen(realpath("/home/jack/Documents/cookie"), "w+")
echo "\r\n" gives a space. Why not a newline?
I believe the problem must be something to do with my permissions to create the file, but I have no problem "right-click" creating the text document on the Desktop.
THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS
echo "debug";
echo "\n";
$jack = "jack";
$cookie = "/home/jack/Documents/cookie";
touch($cookie);
chmod($cookie, 0760);
if(!file_exists($cookie) || !is_writable($cookie))
{
if(!file_exists($cookie))
{
echo 'Cookie file does not exist.';
}
if(!is_writable($cookie))
{
echo 'Cookie file is not writable.';
}
exit;
}
fclose($cookie);
THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS
Instead of fopen()..
touch() to create
chmod() for permissions
I also added user name jack to www-data group.
chmod($path, 0760) group read / write
Reference
chmod() octal values here.
Look at the documentation for file_exists again. It does not take a file handle as an argument, it takes a string filename. The same is true for is_writable. Even if it did, you are opening the file handle and then immediately closing it, so I'm not sure why you're trying to use the file pointer at all after it's been closed.
You may be correct in that you have improper permissions set, but I would start here, first.
Also, if you're only trying to create the file, you may look into using the touch method, instead:
if( touch( $filename ) ) {
// It worked!
} else {
// It didn't work...
}
The web server is not executing as your user. touch /home/jack/Documents/cookie && chmod 777 /home/jack/Documents/cookie to allow the web server user to access the file.
Note this is BAD in production environments.
It looks like a permission issue. What user is PHP running as? It's likely running as www-data or something similar. You should make sure that the folders you are trying to write to are writable by either the user or group that PHP is running as. If you created those folders while logged in a jack, they probably belong to jack:jack and are not accessible by www-data:www-data.
You can also add jack to the www-data group, to make things a bit easier for development.

How to get permission to use unlink()?

I make a site and it has this feature to upload a file and that file is uploaded to a server
Im just a newbie to php I download xampp and I run this site that i made in my local machine.
My site is like this you upload a file then that file will be uploaded to a server, but when i tried unlink() because when i try to remove the filename to a database I also want to remove that pic on the server, but instead I got an error and it says "Permission denied".
question:
How can I got permission to use unlink();?
I only run this on my localmachine using xampp
Permission denied error happens because you're trying to delete a file without having enough/right permissions for doing that.
To do this you must be using superuser account or be the same user that have uploaded the file.
You can go to the directory from your command line and check the permissions that are set to the file.
The easiest solution is to loggin as administrator/root and delete the file.
Here is another work around:
// define if we under Windows
$tmp = dirname(__FILE__);
if (strpos($tmp, '/', 0)!==false) {
define('WINDOWS_SERVER', false);
} else {
define('WINDOWS_SERVER', true);
}
$deleteError = 0;
if (!WINDOWS_SERVER) {
if (!unlink($fileName)) {
$deleteError = 1;
}
} else {
$lines = array();
exec("DEL /F/Q \"$fileName\"", $lines, $deleteError);
}
if ($deleteError) {
echo 'file delete error';
}
And some more: PHP Manual, unlink(), Post 106952
I would recommend, always first to check PHP Manual (in case your question concerns PHP), just go to the page with function that you have problems with and just click search CTRL+F in your browser and enter, for example, Windows, and as a result, in your case, you would find at least 7 related posts to that or very close to that what you were looking for.
Read this URL
How to use Unlink() function
I found this information in the comments of the function unlink()
Under Windows System and Apache, denied access to file is an usual error to unlink file. To delete file you must to change file's owern. An example:
<?php
chown($TempDirectory."/".$FileName,666); //Insert an Invalid UserId to set to Nobody Owern; 666 is my standard for "Nobody"
unlink($TempDirectory."/".$FileName);
?>
So try something like this:
$Path = './doc/stuffs/sample.docx';
chown($Path, 666);
if ( unlink($Path) )
echo "success";
else
echo "fail";
EDIT 1
Try to use this in the path:
$Path = '.'.DIRECTORY_SEPARATOR.'doc'.DIRECTORY_SEPARATOR.'stuffs'.DIRECTORY_SEPARATOR.'sample.docx';

file-system path creation error (file upload)

my code keeps on saying that the folder does not exist though it should be checked by the mkdir function..it creates the folder but does not go through the uploading process.. and displays the error on the couldnt find the folder.. is the algorithm correct? please help.. Your advice will help! :)
here is the code..
if(!(file_exists($target_path)))
{
if(!mkdir($target_path, 0777, TRUE))
{
die ("could not create the folder on mkdir");
}
//in this line the error occurs..printing what is below..//
die ("could not find folder on file exists");
}
else
{
umask($target_path);
...
}
file_exists() routine requires the complete path to file like
/var/www/uploads/file1.c
so the
file_exists($target_path);
call is ok . but second call to make directory, ie,
mkdir()
is requiring an directory , not an path to an file ie, it require /var/www/upload part only .
so you can remove the basename from path name and apply it to mkdir function()
try..
if(file_exists($target_path) && is_dir($target_path)){ //rest of the code...
}
instead of...
if(!(file_exists($target_path))){
}
Hope this will do something for you...
...............................
one thing more...
i think the problem is with if(!(file_exists($target_path))){} Statement,
THIS SHOULD BE...
if(!file_exists($target_path)){}
during the file upload process, your file saving path in move_uploaded_file()
function may be creating problem. I am saying may be because your given code is not clear enough to me. Second parameter of move_uploaded_file() is the destination where first parameter is the file name. please check the value of $target_path, it may solve your problem. thank you.

Categories