Getting error after inserting 3 pages php data insert script - php

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.

Related

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).

Copying folder to other folder with PHP

I'm trying to move folder to other folder, with all it's files. Both folders are in root directory. Tried a lot of ways, and always get no result.
Here is my latest atempt:
$source = "template/"
$dest = "projects/"
function copyr($source, $dest){
if (is_link($source)) {
return symlink(readlink($source), $dest);
}
if (is_file($source)) {
return copy($source, $dest);
}
if (!is_dir($dest)) {
mkdir($dest);
}
$dir = dir($source);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}
copyr("$source/$entry", "$dest/$entry");
}
$dir->close();
return true;
}
Need professional glance to tell me, where I'm getting it wrong?
EDIT:
Sorry for wrong tags.
Problem is - nothing is happening. Nothing is being copied. No error messages. Simply nothing happens.
File structure:
I suggest to try and do the following
How do you run the scrips? Do you open page in browser or run script in command line? If you open page in browser this might be an issue with permissions, paths (relative and not absolute) and errors not shown but logged.
Use absolute folder paths instead of relative paths. For example /var/www/project/template.
Apply realpath() function to all paths and check (output) the result. If path is wrong (folder does not exist, separators are wrong etc) you will get empty result from the function.
Make sure to use DIRECTORY_SEPARATOR instead of / if you run your script on Windows. I can not check if / works on Windows now but potentially this might be an issue. For example
copyr($source.DIRECTORY_SEPARATOR.$entry", $dest.DIRECTORY_SEPARATOR.$entry);
Check warnings and errors. If you do not have permission you should get warning like this
PHP Warning: mkdir(): Permission denied
You may need to enable warnings and errors if they are disabled. Try for example to make an obvious mistake with name and check if you get any error message.
Try to use tested solution from one of the answers. For example xcopy function.
Try to add debug messages or run your script in debugger step by step. Check what is happening, what is executed etc. You can add debug output near any operator like (just an idea):
echo 'Creating directory '.$name.' ... ';
mkdir($name);
echo (is_dir($name) ? 'created' : 'failed').PHP_EOL;

Silverstripe 3.1 - mkdir() issue bug or local issue?

I seem to get this error on my local server when the site is loaded for the first time e.g. in the morning. Once I do a refresh it's gone...
I'm using silverstripe 3.1.
Is there a way to prevent this locally or is this a bug?
Warning: mkdir(): File exists in /framework/core/manifest/ManifestCache.php on line 19
Looks like line 19 is trying to create a TEMP folder but it already exists...
function __construct($name) {
$this->folder = TEMP_FOLDER.'/'.$name;
if (!is_dir($this->folder)) mkdir($this->folder);
}
Should that function check if the folder exists first e.g.
if (!is_dir($this->folder) || !file_exists($this->folder)) mkdir($this->folder);
Seems that there exists a file with the same name as the directory. That's why is_dir() returns false but mkdir() fails because the file exists.
You can change this to:
if (!file_exists($this->folder)) mkdir($this->folder);
This should work so far.
However it is necessary to mention that such file existence tests are vulnerable against race conditions by design. That's why you need to additionally check the return value of mkdir():
if (!file_exists($this->folder)) {
if(#mkdir($this->folder) === FALSE) {
throw new Exception('failed to create ' . $this->folder);
}
}
This may not being required if you (or the framework) has registered a global error handler which turns warning into exceptions, because mkdir() will throw a warning on errors.

exception in creating directory

Am a beginner in php. My problem is, i use the following code to create a directory and copy some files into it(I used a code get from so itself). The code works fine directory is created and files are copied. But I am getting a warning like this.
function copyr($source, $dest)
{
// Simple copy for a file
if (is_file($source)) {
chmod($dest, 0777);
return copy($source, $dest);
}
// Make destination directory
if (!is_dir($dest)) {
mkdir($dest);
}
chmod($dest, 0777);
// Loop through the folder
$dir = dir($source);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}
// Deep copy directories
if ($dest !== "$source/$entry") {
copyr("$source/$entry", "$dest/$entry");
}
}
// Clean up
$dir->close();
return true;
}
copyr("agentSourcefolder", "testTemp5");
.
Warning: chmod() [function.chmod]: No such file or directory in /home/websiteName/public_html/php_file_upload2.php on line 9
I have to get the response from server after this, what should i do? I used
header("Location: http://www.websiteName.com/");
But it shows the following error
Warning: Cannot modify header information - headers already sent by (output started at /home/websiteName/public_html/php_file_upload2.php:9) in /home/websiteName/public_html/php_file_upload2.php on line 41
And if the directory is already created this code works fine
This one:
Warning: Cannot modify header information - headers already sent by (output started at /home/websiteName/public_html/php_file_upload2.php:9) in /home/websiteName/public_html/php_file_upload2.php on line 41
is because you are passing the header('location:') call after anything has been written to the screen. All of this processing should be done before anything is written to the page, like echo, breaking out of php, etc. Also make sure there's NO white space before the opening <?php line.
The other error is likely because a file you're trying to chmod doesn't exist or exist yet. Or that you may need to provide an absolute path to mkdir. like $_SERVER['DOCUMENT_ROOT']."/path-to-new-dir/"

Expalanation for errors in the below code

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

Categories