iam doing my project in codeigniter in that i used to create new directory to upload my images.now i need to delete my new directory for that i have tired this coding,
$dir = '/var/www/uploads/chat/'.$input['chat_id'];
system('/bin/rm -rf ' . escapeshellarg($dir));
but its not working for me.i have got this coding from this link http://stackoverflow.com/questions/1296681/php-simplest-way-to-delete-a-folder-including-its-contents what i need to do?
when you are creating the directory use 777 as permission like
mkdir('new_dir', 0777, true);
and then try the below code
if(rmdir('new_dir'))
{
echo "deleted";die;
}
else
{
echo "not deleted";die;
}
i checked it and found,if your folder has not right permission then it will not be deleted.
please try and let me know the status.
Related
My aim is to download multiple files into the folder on my localhost. I am uploading them using the HTML form.
Here is the code (really sorry that I can't give a link to the executable version of the code because it relies on too many other files and database if anyone knows the way then please let me know)
foreach ($_FILES as $value) {
$dir = '/';
$filename = $dir.basename($value['name']);
if (move_uploaded_file($value['tmp_name'],$filename)) {
echo "File was uploaded";
echo '<br>';
}
else {
echo "Upload failed";
echo '<br>';
}
}
So this little piece of code give me an error:
And here are the lines of code:
The problem is that the adress is correct, I tried enterring it into my file directory and it worked fine, I have seen some adviced on other people's related questions that // or \ should be used instead, but my version works just fine! Also I have checked what's inside the $_FILES and here it is if that's required for someone trying to help:
Thank you very much if anyone could help!!
You are trying to move the file to an invalid (or non-existent) path.
For the test you will write
$dir = 'c:/existing_dir/';
$filename = $dir.basename($value['name']);
If you want to move the file to a folder that is relative to the running file try
$dir = '../../directory/';// '../' -> one directory back
$filename = $dir.basename($value['name']);
By starting your file path with $dir = '/'; you are saying store the file on the root folder, I assume of C:
Apache if correctly configures should not allow you access to C:\
So either do
$dir = '../';
$filename = $dir.basename($value['name']);
to make it a relative path or leave the $dir = '/'; out completely
I have an image sharing website and recently I've noticed I can't delete some of my images via script.
My file is writable and exist (so it's not a permission issue), but why can't I unlink it?
echo $file = base_path('./files/images/2013/11/TubeCom_3313c73ab7924b1f36ee49ad0979a16ad490f9a2.jpg');
echo is_writable($file) ? ' #is_writable ' : ' !!is_writable ';
echo is_file($file) ? ' #is_file ' : ' !!is_file';
$res = unlink($file);
var_dump($res);
Here is the result:
./home/siteecom/domains/site.com/public_html/files/images/2013/11/TubeCom_3313c73ab7924b1f36ee49ad0979a16ad490f9a2.jpg
#is_writable #is_file bool(false)
I've also tried relative path ... didn't work
I recently run into an issue like this before.
Firstly you'll need to turn on error reporting as unlink() will tell you exactly what's wrong:
ini_set('display_errors', 1);
error_reporting(E_ALL);
You'll want to make sure the directory that contains the file you want to delete is writable (please supply the chmod permissions for us to help furthur).
You should look into using realpath() to get the absolute path to the file. (I don't think this is the issue as it doesn't throw a file not found error).
Your problem is almost certainly related to incorrect permissions for the directory you're trying to delete in and also the script thats trying to delete said files.
If you could supply those permissions of both with something like:
echo "Directory = ".substr(sprintf('%o', fileperms(DIRECTORY)), -4) . "<br />";
echo "PHP File = ".substr(sprintf('%o', fileperms(SCRIPT)), -4) . "<br />";
We can try and help you further.
You probably missed that in order to delete a file you'll need write permissions to the file and it's ancestor folder. Make sure that the directory ./files/images/2013/11 is writable by PHP.
I'm new in DirectAdmin. I'm facing a problem that when user try to create a folder, they will get error, "Unable to upload data." (refer to my code).
I think my code should be no problem as it can run smoothly in Localhost. The problem rise when I run on live server (DirectAdmin).
$id = $this->session->userdata('id');
$directory = "./image/userFolder/" . $id;
if(!is_dir($directory)) {
mkdir($directory, 0777, true);
}
$directory = $directory . "/" . $nameImage;
if(!imagejpeg($big_image, $directory)) {
$data['error'] = "Unable to upload data.";
return $data;
}
Hope to get answer or maybe something that I can look after. Thank you.
I managed to solve the problem. Basically my folder's permission is 0755. Then I change to 0777 that enable user to make folder (mkdir) in the server. You can learn more about permission here.
I am trying to upload my php project into public server.
I made the image upload file when I create product or edit product.
It works in localhost, but when I move to public server, it is not working.
I think move_uploaded_file part does not working.
How can I change the link? or do I have to change anything?
When I see Filzilla, I can see remote site that it is '/www/eshopProject/inventory_images'.
And index file is '/www/eshopProject/storeAdmin'.
Do I have to change link like this?
I don't know how can I change the link.
Could you help me? uploading the image into public server is not working..
Is it any security issue? or something?
Please help me. Thanks.
--index.php--
$pid = mysql_insert_id();
//Place image in the folder
$newname = "$pid.jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'], "../inventory_images/product_$newname");
First of all check the permissions of the directory as mentioned in come of the comments.
If you have shell access "chmod 777 target_dir" or "chmod 707 target_dir" should be sufficient.
Second try to debug it using if's and the file_exists function(http://php.net/manual/en/function.file-exists.php).
Something like this.
$uploadedFile = $_FILES['fileField']['tmp_name'];
$destination = "../inventory_images/product_$newname";
if(file_exists($uploadedFile))
{
echo "file uploaded to temp dir";
}
else
{
echo "file upload failed";
exit();
}
if(move_uploaded_file($uploadedFile, $destination))
{
echo "upload complete";
}
else
{
echo "move_uploaded_file failed";
exit();
}
You can also check your current working directory by using the FILE or DIR constants(http://php.net/manual/en/language.constants.predefined.php).
Try this.
echo __FILE__;
echo dirname(__FILE__);
echo __DIR__;
Use the copy() method. For me it worked.
copy($tmp_file, Destination) or
copy($tmp_image, IMAGE_DIRECTORY . SAM . $product_image);
Make sure you have write file permissions set to the folder you are trying to upload too.
I recommend setting the folders to "755" permissions and retry. This would make the permissions a little tighter.
This question is a bit old but i recently faced a similar issue where even with permission 777 on the upload folder it wouldn't work.
The issue was that the SELinux (https://wiki.centos.org/HowTos/SELinux) was on enforcing mode so i had to change it to permissive mode and then the upload works perfectly.
I hope this can help someone facing this issue.
I am stuck on a simple script. I have tried Googling it and I have tried all of the stuff I found on Google but none of it has worked for me. I am trying to you the mkdir function but I keep getting an error that says: Warning: mkdir() [function.mkdir]: No such file or directory in /home/content/l/i/n/web/html/php/mkdir.php on line 6
Here is my code:
<?php
//Create the directory for the user
$directory = time() . rand(0, 49716) ;
if (mkdir("/users/$directory", 0777)) {
echo '<p>Directory was created successfully.</p>' ;
} else {
echo '<p>Directory was NOT created.</p>' ;
}
?>
I am placing the script called mkdir.php in the same folder as the users folder. The users folder has the permissions set to 0777. I am not sure why I keep getting this error because the users folder does exists. Any help is greatly appreciated.
if (mkdir("users/$directory", 0777)) {
:-)
Nevermind I figured it out. It was just that the first slash in "/users/$directory" should be "users/$directory".