Expalanation for errors in the below code - php

if(!is_dir($dir_path))
{
$mk_dir=mkdir($dir_path, 0777);
$ch_mod=chmod($dir_path, 0777);
}
In the above code I am getting errors like below:
Warning: mkdir() [function.mkdir0]: No such file or directory in E:\salaahakardb\New Folder\xampp\htdocs\extramarks2\jnrcontent\fillblanks\form_vars.php on line 66
Warning: chmod() [function.chmod0]: No such file or directory in E:\salaahakardb\New Folder\xampp\htdocs\extramarks2\jnrcontent\fillblanks\form_vars.php on line 67
Please explain

The parent directory of the directory you're trying to create probably doesn't exist.
One way could be to create it recursively:
mkdir($dir_path, 0777, true);
Check http://php.net/manual/en/function.mkdir.php for further info.
Also you can get rid of that chmod() since you are already setting permissions while mkdir()'ing.

is_dir function returns false, if passed parameter is file - it's your situation.
try file_exists function

Related

In php code, use shell_exec correctly in my case

I have question about how to save log in correctly directory. My php code:
public static function log(test $testination) {
echo($testination->getDetails()."\n");
$log_filename = shell_exec("cd; ~/log/");
$log_filename2 = "log";
if (!file_exists($log_filename2))
{
mkdir($log_filename, 0777, true);
}
$log_file_data =$log_filename.'/logs_' . date('d-M-Y') . '.log';
file_put_contents($log_file_data, var_dump($testination) . "\n", FILE_APPEND);
}
in this code, I create a folder in the same place as my PHP file, then create folder log if this not exist.
I want to save in begining (just cd) then create log folder if this not exist I try this code
$log_filename = shell_exec("cd; ~/log/");
i get warning/error
PHP Warning: file_put_contents(/logs_18-Aug-2020.log): failed to open stream: Permission denied in /test/test/Helper.php on line 318
Any idea how to do it? Please im stuck.
Any help will be appreciated.
That's not how you should use file_put_contents: it does not know anything about that attempt to change the directory, as this will not affect the execution of PHP. If you want to get the directory of the current PHP file, you can use basename(__DIR__)
Additionall, cd; ~/log/ won't work in your shell as well.

Warning: move_uploaded_file(): The second argument to copy() function

I'm working on a file upload method. But i suddenly start to get the following error, The only thing I changed was the file name. I reverted it back, but the error still persists.
Does anyone know how to solve this?
The error message is:
Warning: move_uploaded_file(): The second argument to copy()
function cannot be a directory in
/hermes/bosnaweb14a/b1717/ipg.plantationkeyartcorn/kittyrescuetnr/docs/upload.php
on line 13
Warning: move_uploaded_file(): Unable to move '/tmp/phpJki8OC' to '/' in
/hermes/bosnaweb14a/b1717/ipg.plantationkeyartcorn/kittyrescuetnr/docs/upload.php
on line 13
Fail
my php looks like this
<?php
$file_upload="true";
$file_up_size=$_FILES['file_up'][size];
$file_destination=$REQUEST['file_type'];
$file_new_name=$REQUEST['file_name'];
$file_name=$_FILES[file_up][name];
**$add="$file_destination/$file_new_name"; // the path with the file name where the file will be stored**
if($file_upload=="true"){
if(move_uploaded_file ($_FILES[file_up][tmp_name], $add)){
echo print_r($file_new_name);
}else{echo "Fail";}
}else{
echo $msg;
}
?>
Well, since you are using superglobal array $_REQUEST incorrectly (notice the underscore in the name), your $add variable evaluates to just a slash. Which is a root directory.
Hence the errors - it is a directory after all and your script probably don't have write access to it anyway (which is a good thing).

Getting error after inserting 3 pages php data insert script

I have 3 php pages where details is supposed to be inserted to db in final php.. I am geetting these warnings below however data is inserted. I know these warning can be turned of by error reporting but doesnt look good to me go for that..
Warning: copy(): The first argument to copy() function cannot be a directory in /home/opterfhb/public_html/quest4home.com/search/add_edit_property_finish.php on line 301
Warning: unlink(tmp_imgs/tmp_1011/..): Is a directory in /home/opterfhb/public_html/quest4home.com/search/add_edit_property_finish.php on line 302
Warning: copy(): The first argument to copy() function cannot be a directory in /home/opterfhb/public_html/quest4home.com/search/add_edit_property_finish.php on line 301
Warning: unlink(tmp_imgs/tmp_1011/.): Is a directory in /home/opterfhb/public_html/quest4home.com/search/add_edit_property_finish.php on line 302
I think I have defined it wrong way.. seeking help..
I am getting error on this section:
// Moveing temp images to property directory
if ($handle = opendir('tmp_imgs/tmp_'.$property_id)) {
while (false !== ($file = readdir($handle)))
{
//$file_ext = strtolower(substr($file, strrpos($file, '.') + 1));
copy('tmp_imgs/tmp_'.$property_id.'/'.$file, 'property_images/img_'.$property_id.'/'.$file);
unlink('tmp_imgs/tmp_'.$property_id.'/'.$file);
}
closedir($handle);
}
The error is clear: you cannot use that function to copy directories. However, you may not be aware that you're even trying to do this in the first place.
There are magical "files" in each directory which are themselves directories (. and ..), so if you're iterating over a directory and copying everything in it, you need to explicitly skip those two.

PHP rename(<from>, <to>) gives warning: "Is a directory"

I have the following function:
function move($currentPath, $newPath)
{
if (!$this->_createFolder($newPath))
return false;
if (!rename($currentPath, $newPath))
return false;
return true;
}
where _createFolder() checks if the directory exists, and if it doesn't, creates it. I'm consistently getting the following warning:
"rename(/home/user/folder/folder/app/webroot/img/listings/incomplete/15/0/picture1.png,/home/user/folder/folder/app/webroot/img/listings/130/picture1.png): Is a directory "
The file is successfully copied to the second directory, but is not deleted from the first directory. rename() returns false and this warning is given. I thought it might be something with permissions, but after trying a bunch of things I couldn't seem to figure it out.
Any help would be appreciated.
your code is creating a folder using the $newpath
if (!$this->_createFolder($newPath))
return false;
then $newpath becomes a directory.

Warning: move_uploaded_file() failed to open stream

I am trying to upload file on ftp.
here is my code
$jname= "Accounts of Biotechnology Research";
if (!is_dir('/Trade/upload/ '.$jname)) {
mkdir('/Trade/upload/ '.$jname); // line 63
}
move_uploaded_file($_FILES["submission_file"]["tmp_name"], "/Trade/upload/$jname/" . $dup_name ); // line 67
Trade is a folder inside public_html folder.
When i am uploading a file it gives me a warning like,
Warning: mkdir() [function.mkdir]: No such file or directory in /home/my_username/public_html/Trade/upload.php on line 63
Warning: move_uploaded_file(/Trade/upload/Accounts of Biotechnology Research/76164762-sm.pdf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/my_username/public_html/Trade/upload.php on line 67
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phphZXp0O' to '/Trade/upload/Accounts of Biotechnology Research/76164762-sm.pdf' in /home/my_username/public_html/Trade/upload.php on line 67
First:
You have a space here mkdir('/Trade/upload/ '.$jname);. Suppose you should have mkdir('/Trade/upload/'.$jname); (same for is_dir)
Second:
Ensure that you can write into Trade/upload directory.
Third (and I suppose that is the real problem):
It looks like you are trying to upload into a directory with full path:
/home/my_username/public_html/Trade/upload/, but your code will try to create a directory with full path:
/Trade/upload/.
You need to change
if (!is_dir('/Trade/upload/ '.$jname)) {
mkdir('/Trade/upload/ '.$jname); // line 63
}
to
if (!is_dir('upload/'.$jname)) {
mkdir('upload/'.$jname); // line 63 (or maybe there should be Trade/upload, but suppose current working dir will be /home/my_username/public_html/Trade, so only upload/)
}
Another option is to force mkdir to create directories recursively:
mkdir('/Trade/upload/'.$jname, 0755, true);
But in that case, files will be uploaded into /Trade/upload/... instead of /home/my_username/public_html/Trade/upload/...
There are two things you should be aware of based on the error messages you received. I'm guessing /Trade isn't the root path on your machine since it's clear in the error that your actual path is /home/my_username/public_html/Trade/, so the first adjustment should be
$root_path = "/home/my_username/public_html/Trade/upload/";
The second adjustment I'd suggest is that you avoid pathnames with space in them:
$jname= "Accounts of Biotechnology Research"; //could be changed to
$jname= "Accounts_of_Biotechnology_Research"; //$jname = str_replace(" ","-",$jname) OR
$jname= "Accounts-of-Biotechnology-Research"; //$jname = str_replace(" ","-",$jname)
Finally take note of the space character on the following lines, they affect you final result:
if (!is_dir('/Trade/upload/ '.$jname)) { //AND
mkdir('/Trade/upload/ '.$jname);
}
Note the [space] between upload/ '.$jname in both strings.

Categories