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/"
Related
Below is my attempt to delete a folder and all its content. A folder may contain zip files and folders with files.
public function deleteFolder($dir){
if(file_exists($dir)){
$it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
$files = new \RecursiveIteratorIterator($it,
\RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
if ($file->isDir()){
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
rmdir($dir);
}
}
but it returns the following error:
rmdir(C:\Juliver\UIUX\pd-loader\loader/temp/utso-pulgada-pd-loader-5066a7e0298a):
Directory not empty in C:\Juliver\UIUX\pd-loader\loader\Patcher.php on line 95
line 95 points to rmdir($dir); line
If I check the folder utso-pulgada-pd-loader-5066a7e0298a, I see that it's already empty but it throws me the above error.
$dirname = 'C:/Users/Admin/Desktop/test';
array_map('unlink', glob("$dirname/*.*"));
rmdir($dirname);
try this, this remove all the file present in the folder, and that folder too
Directory may contain other directories so you have to use a recursive function.
function removeDir($path) {
$files = glob("$path/*");
foreach ($files as $file) {
if (is_dir($file)) {
removeDir($file);
} else {
unlink($file);
}
}
rmdir($path);
}
Now is enough to call removeDir("/my/nice/path");
If you see the directory already empty, try to check for hidden files and be sure that you have the right permissions.
I suspect you have already checked its not a file permissions issue. As your code works for me but not you, it makes me wonder if it is a to do with PHP file stat or Real Path caching.
Unlinking a file should clear stat cache for individual file automatically. However PHP bugs have previously been known to cause this issue with rmdir.
Try doing a clearstatcache after the rmdir statement in your foreach block.
Previously I've used glob (mentioned in other answers) so I've no idea how RecursiveDirectoryIterator works re file handles; as a long shot try destroying these objects ( unset($files); unset($it) ) before your final rmdir.
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;
I encounter an error when copying files inside a directory from another computer in a shared network using PHP. Basically here is the code i am using, the recursive function created by #authorAidan Lister to copy whole directories. It is working on my local machine if I use a local file directory, but when I change it to a directory from another computer it gives me an error concerning permissions.
I am sure that I took care of the permissions concerning folder access because I have gone as far as to allow "Everyone" to be able to have full access to the folder for testing purposes , but it still doesn't work. I am lost and have exhasuted all options I can think of.
PHP is the name of my local machine.
VMSTBOX is the name of the computer with the files i am trying to access.
Also, when I use " chmod("\\VMSTBOX\Users", 0777); "in my PHP code.It also gives me a "permission denied " error. I think this is related to my issue.
I have posted a chunk of my source code below along with the error message below. Thanks for any help
function xcopy($source, $dest, $permissions = 0777)
{
// Check for symlinks
if (is_link($source)) {
return symlink(readlink($source), $dest);
}
// Simple copy for a file
if (is_file($source)) {
return copy($source, $dest);
}
// Make destination directory
if (!is_dir($dest)) {
mkdir($dest, $permissions);
}
// Loop through the folder
$dir = dir($source);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}
// Deep copy directories
xcopy("$source/$entry", "$dest/$entry", $permissions);
}
// Clean up
$dir->close();
return true;
$filelocation="\\\VMSTBOX\\Files";
$newfilelocation="\\\\PHP\\VarTemptoCopy";
$status = xcopy($filelocation, $newfilelocation);
CODE ENDS HERE
Here is the error message i keep getting.
"Warning: dir(\VMSTBOX\Files,\VMSTBOX\Files): Access is denied. (code: 5) in C:\xampp\htdocs...."
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.
I am trying to use this simple bit of code to iterate through the "export" folder and delete files older than 24 hours:
if ($handle = opendir("/home/username/public_html/en/graphs/export")) {
while (false !== ($file = readdir($handle))) {
$filelastmodified = filemtime($file);
if((time() - $filelastmodified) > 24*3600)
{
unlink($file);
}
}
closedir($handle);
}
Some notes:
1) I do realize there are similar questions, but the solutions suggested there don't seem to work for me.
2) The absolute path to the directory is correct (tested)
3) The directory has 777 permissions. The files in it don't, but I tested with some files with 777 permissions and the same errors happened. So it doesn't seem to be a permission issue.
4) The file that contains this code is in a different directory (it's a cron job, I like to keep them together in a separate directory)
This is the error that appears (for each file in the directory):
Warning: filemtime() [function.filemtime]: stat failed for countries_rjRp9.png in /home/username/public_html/path-to-crons/crons/exports.php on line 12
Warning: unlink(countries_rjRp9.png) [function.unlink]: No such file or directory in /home/username/public_html/path-to-crons/crons/exports.php on line 16
In this example, countries_rjRp9.png is one of the files that should be unlinked from the export directory.
What's going on here?
You should specify the full path to unlink the file. In your loop, $file will be countries_rjRp9.png and you're trying to unlink it from the working directory, which is, the directory in which all of your cronjobs reside.
You state that the absolute path to your files is correct, but you forgot to use the absolute path once you're in your loop. You're only using an absolute path in your opendir() call, nowhere else.
Try doing something like this:
if ($handle = opendir("/home/username/public_html/en/graphs/export")) {
while (false !== ($file = readdir($handle))) {
// Take the filename and add its full path
$file = "/home/username/public_html/en/graphs/export/" . $file;
$filelastmodified = filemtime($file);
if((time() - $filelastmodified) > 24*3600)
{
unlink($file);
}
}
closedir($handle);
}