I was trying to create directories automatically with php, with the mkdir() function.
<?php
session_start();
$domain = $_SESSION['domain'];
$mydomain = "/" . $domain;
echo $_SESSION['domain'] . " " . $mydomain . "<br />";
$mk = mkdir($mydomain, 0777, true);
if ($mk){
echo "directory created";
}else{
echo "directory no created";
}
?>
but it gave me back this error
[02-May-2021 09:19:41 UTC] PHP Warning: mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7
[02-May-2021 09:22:28 UTC] PHP Warning: mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7
[02-May-2021 09:22:30 UTC] PHP Warning: mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7
[02-May-2021 09:22:31 UTC] PHP Warning: mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7
[02-May-2021 09:22:32 UTC] PHP Warning: mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7
[02-May-2021 09:22:32 UTC] PHP Warning: mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7
[02-May-2021 09:22:36 UTC] PHP Warning: mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7
how can i fix that?
im using hostinger
You can try like below
<?php
session_start();
$domain = $_SESSION['domain'];
$mydomain = "/" . $domain;
echo $_SESSION['domain'] . " " . $mydomain . "<br />";
$old = umask(0);
$mk = mkdir($mydomain, 0777, true);
umask($old);
if ($mk){
echo "directory created";
}else{
chmod($mydomain, 0777);
}
?>
if mkdir() Not worked, You need to change permissions using chmod()
https://www.php.net/umask
Use echo for example to look at the value of $mydomain and then use this command to give the right access
chmod 777 -R <directory name>
Related
I try to upload an image to a directory on localhost.
Here is the part in my php file, which causes the error:
if (move_uploaded_file($tmp_name, "/Users/sandro/Documents/bildgalerie/uploads")) {
echo "The file " . basename($filename) . " has been uploaded";
} else {
echo "error";
}
If I load the page, I get the following:
Warning: move_uploaded_file(): Unable to move '/private/var/folders/np/sln14hvn3tsbzc4kpjjtp17c0000gn/T/phppJbFJK' to '/Users/sandro/Documents/bildgalerie/uploads' in /Users/sandro/Documents/bildgalerie/control/upload.php on line 31
My php file has multiple errors. The first one has to do something with permissions. Normally there's nothing wrong with it it, but now I use a csv-file. I think that's the problem. But I don't understand the other warnings.
Which permission do I have to add?
$filename = "menuitems.csv";
$fp = fopen($filename, "r");
while ($data = fgetcsv($fp, filesize($filename), ",")) {
if (trim($data[0]) == '') break;
$href = strtolower(substr($data[1], 0, 7));
if ($href == "http://")
print "<li> " . $data[0] . "</li>\n";
else
print "<li> <a href=\"" . data[1] . "\" >" . $data[0] . "</a></li>\n";
}
fclose($fp);
echo "</ul>\n";
echo "</div>\n";
Warning: fopen(menuitems.csv): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/basis/php/index.php on line 10
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/basis/php/index.php on line 11
Warning: fclose() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/basis/php/index.php on line 21
the csv file that was moved into the document root does not have permissions set correctly. You can do this several ways, including changing permission in the file properties dialog (via right click menu) or use the php command:
chmod("/somedir/somefile", 0644);
but installing xampp in its own folder off the root directory instead of Program Files directory will fix this issue in most XAMPP versions.
I just wanna ask if its possible to make directory and store the uploaded file in it to a shared folder from another pc using mkdir and move_uploaded_file. I tried doing it locally it works fine but when i try this code
if($_REQUEST['submit']){
$app_name = $_POST['name'];
$desc = $_POST['description'];
$file_url = "192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"];
$photo_loc = "192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"];
$select = $_POST['select'];
$date = date('m/d/y h:i:s A');
mkdir("192.168.3.34/testupload/files/$app_name/");
mkdir("192.168.3.34/testupload/images/$app_name/");
/* some codes here that is working */
if($file != "" ){
if (file_exists("..192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"]) && file_exists("..192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"])) {
die("File Exists");
}
move_uploaded_file($_FILES["file"]["tmp_name"],
"192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"]);
if($photo_url != "") {
move_uploaded_file($_FILES["photo"]["tmp_name"],
"192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"]);
}
$name = escape($_REQUEST['name']);
$description = escape($_REQUEST['description']);
//$query = "INSERT into files (name, description,file_name,photo_url,date_uploaded) values ('{$name}', '{$description}','{$file}', '{$photo_url}', now() )";
//mysql_query($query) or die("Error in Query" . mysql_error());
//commit();
//redirect("manage.php");
$file_loc = "192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"];
$photo_loc = "192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"];
}
}else{
set_error("File not Found");
//redirect('manage.php');
}
i tried to change the "/" in "\" and still getting this error
Warning: mkdir() [function.mkdir]: No such file or directory in
C:\xampp\htdocs\atts_update\upload.php on line 12
Warning: mkdir() [function.mkdir]: No such file or directory in
C:\xampp\htdocs\atts_update\upload.php on line 13
Warning:
move_uploaded_file(192.168.3.34/testupload/files/1111111111111/sss.txt)
[function.move-uploaded-file]: failed to open stream: No such file or
directory in C:\xampp\htdocs\atts_update\upload.php on line 71
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to
move 'C:\xampp\tmp\php610B.tmp' to
'192.168.3.34/testupload/files/1111111111111/sss.txt' in
C:\xampp\htdocs\atts_update\upload.php on line 71
Warning:
move_uploaded_file(192.168.3.34/testupload/images/1111111111111/Picture-in-a-picture4aaa.jpg) [function.move-uploaded-file]: failed to open stream: No such file or
directory in C:\xampp\htdocs\atts_update\upload.php on line 76
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to
move 'C:\xampp\tmp\php611C.tmp' to
'192.168.3.34/testupload/images/1111111111111/Picture-in-a-picture4aaa.jpg' in C:\xampp\htdocs\atts_update\upload.php on line 76
This is working fine on init.php
include 'models/m_app.php';
include 'views/v_app.php';
include 'controllers/c_app.php';
but the spl_autoload_register() is not working
spl_autoload_register(function ($class) {
include 'models/' . $class . '.class.php';
include 'views/' . $class . '.class.php';
include 'controllers/' . $class . '.class.php';
});
and I am getting error like
Warning: include(models/Model.class.php): failed to open stream: No such file or directory in C:\wamp\www\App\core\init.php on line 3
Warning: include(): Failed opening 'models/Model.class.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\App\core\init.php on line 3
Warning: include(views/Model.class.php): failed to open stream: No such file or directory in C:\wamp\www\App\core\init.php on line 4
Warning: include(): Failed opening 'views/Model.class.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\App\core\init.php on line 4
Warning: include(controllers/Model.class.php): failed to open stream: No such file or directory in C:\wamp\www\App\core\init.php on line 5
Warning: include(): Failed opening 'controllers/Model.class.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\App\core\init.php on line 5
Can you please let me know why this is happening?
Well, you are using the spl_autoload_register function incorrectly.
An autoload function is getting called, when you are trying to create a class that does not exist.
The correct way would be to name your files just as the class inside them and add an ending like .class.php:
View.class.php
Model.class.php
Controller.class.php
The autoloader:
spl_autoload_register(function ($class) {
set_include_path('views/' . PATH_SEPARATOR . 'controllers/' . PATH_SEPARATOR . 'models/');
include $class . '.class.php';
});
And to test it:
$test1 = new Model();
$test2 = new Controller();
$test3 = new View();
Here is function that I'm using for recursively deleting folders and files
function rmdir_recursively($dir) {
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $file) {
if ($file == '.' || $file == '..') continue;
if (!rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file)) {
chmod($dir . DIRECTORY_SEPARATOR . $file, 0777);
if (!rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file)) return false;
};
}
return rmdir($dir);
}
The problem is, when I send some folder inside root, it deletes this folder. But when I send root folder itself like that
rmdir_recursively("./");
It returns bunch of errors like below
PHP Warning: unlink(.//wp/wp-admin/network) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-admin/user) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-content) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-content/plugins) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-content/plugins/akismet) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
What am I missing?
You say you're sending it rmdir_recursively("./");?
But look at the code, you are later calling rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file).
This means that you'll be trying to eliminate files in ".//".
Which is why your error message contains unlink(.//wp/wp-admin/user)
Use rmdir_recursively("."); instead.