How to remove image from local directories using php - php

I am trying to delete images from local folder of my computer using php and form method. My html form is as follows:
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Delete Image" name="dlt">
</form>
and my php code is as follows:
if(isset($_POST['dlt'])) {
if(file_exists("F:\xampp\htdocs\practice\images/".$_FILES['file']['name'])) {
$src_file_name = "F:\xampp\htdocs\practice\images/".$_POST['file'];
unlink($src_file_name);
echo '<script>alert("File deleted");</script>';
}
else {
echo"<script>alert('File Doesn't exist);</script>";
}
}
where is the mistake ?

First check if function is called in the same directory where is kept "images" directory because if not - it can not find proper folder. It may be that You are calling PHP function from different location than it is defined and it takes location of the folder in which it is called so it may be path problem.
Also I would recommend to put 'die();' right after unlink() because the website will 'die' and unlink() will echo the error it encounters while trying to unlink the file.

You must ensure that the spliced path is correct, and it is recommended that you use the full path, such as "c:\xxx\images\filename". If the problem cannot be resolved, you are advised to provide error information.

Related

Two submit buttons in one HTML form, for uploading or deleting a file with PHP

I tried to create one form containing two submit buttons, one to upload a file and the second to delete a selected file from a specific location. The upload process is successful, but the delete process fails.
This is the code for the two submit buttons:
<html>
<head>
<title>Upload and Delete file</title>
</head>
<body>
<form action="upanddel.php" method="post" enctype="multipart/form-data">
Select File to Upload:<input type="file" name="uploadfile" value="upload">
<input type="submit" name="upload" value="Upload File">
<input type="submit" name="delete" value="Delete File"><br><br>
</form>
</body>
</html>
And this is the PHP code for uploading or deleting a file:
<?php
if(isset($_POST['upload'])) {
onFunc();
}
if(isset($_POST['delete'])) {
offFunc();
}
function onFunc(){
$filenameupload=__DIR__.'\\upload\\'.$_FILES['uploadfile']['name'];
if(!file_exists($filenameupload)){
move_uploaded_file($_FILES['uploadfile']['tmp_name'],$filenameupload);
print_r( $_POST);
$_POST=array();
}
}
function offFunc(){
$filenamedelete=__DIR__."\\upload\\".$_FILES['uploadfile']['name'];
unlink($filenamedelete);
echo "file is deleted";
}
?>
The error is:
Warning: unlink(C:\wamp64\www\IntroducingPHP\upload\Events-News.xlsx): Resource temporarily unavailable in C:\wamp64\www\IntroducingPHP\Deletefile.php on line 5
What is the solution?
The path you mentioned has '\\'. From there and from the path mentioned in the warning it seems that you are using Windows operating system.
The file path provided to the method unlink() seems valid because the warning says the resource is temporary not available.
This means some process is having access to your file that's why it is not allowed to be deleted.
There is two possibilities :
Are you opening the file after it is uploaded? In that case if the file is open it won't be allowed to delete it. So close it and try to delete again.
If you've not opened the file, then there is the possibility that the variable you have used to refer to the file is still holding it.
Inside onFunc() you are using $filenameupload to refer the file.
If the file does not exist you upload the file and in the end you are clearing $_POST.
But the variable $filenameupload is not processed afterwards. So maybe it is holding the file. You add the line
unset($filenameupload);
after the if() condition inside onFunc() and check it.
This will remove the reference to the file if it is yet not removed and afterwards you may be able to delete the file.

PHP unlink doesn't delete

This tiny problem has ruined my day. I can't delete files by PHP unlink function. I am creating PHP form to update and edit pdf files. Below is my piece of html form and PHP unlink script.
HTML
<form method="post" action="#" enctype="multipart/form-data">
<input type="file" value="<?php echo $row['img']?>" name="image">
<input type="submit" name="update">
</form>
PHP
<?php
if(isset($_POST['update']) && ($_FILES['image']['name'])){
$image=$_POST['image'];
unlink('../pdf/services/'.$image);
}
?>
Try $_FILES. Something like
$image = $_FILES['image']; and then $imgname = $image['name'];
After that you can use unlink(); as you wanted (unlink("../pdf/services".$imgname);)
Hope I helped, It worked after my tests!
Whenever you try to remove a file using unlink, you will need to make sure that everything is ok:
Is the path I want to remove a file from available? Do I have access all the folders I need along the path? If not, then giving the necessary privileges will be needed.
Is my location the one I think it is? You will need to run getcwd() which is current working directory. If your page is not in the desired location, then compared to it the path you want to reach will mean a different thing
Is adding the path I want to the location I got at the second point correct and the one I expect?
If everything above is all right and you still have the problem, then check the error log, it might give you helpful information. If the problem still persists, then you will need to create a dummy file on the path you want to remove the other file from. Is the file created? Is it created at the correct location?

Retrieve file path through browse input button

I need to get the full file path including file name information to handle later with PHP.
I am running into many difficulties.
We can use a html form with the html
<form method="post" enctype="multipart/form-data">
<input type="file" />
However, to pass this information we need to the server, we actually have to upload the file, I do not want to do this as its wasteful, The reason being is that this project is just for my local machine, The file already exists on my machine.
A manual way of achieving this is to provide a input text field, and inserting the file(s) full location path here via another file navigation window(copy/paste), But this quickly becomes boring and requires to much work when using the tool often.
Any other suggestions to achieve this?
This isn't possible. This has been discussed before and the explanations they give there are good, so I won't repeat:
How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?
paste following line under file tag,your problem will be solved
webkitdirectory directory multiple
<input type="file" id="ctrl" webkitdirectory directory multiple/>

php move_uploaded_file unable to move

I've got the following code to upload a file and add it to the folder called images located in the root of the server.
$file = $_FILES['prodImg']['tmp_name'];
$newLoc="/images/" . $_FILES['prodImg']['name'];
if(move_uploaded_file($file, $newLoc)){
//do some other code here
}
else{
echo 'error';
}
the form has this button to add the image
<input type="file" name="prodImg" id="prodImg" accept="image/png" />
the images folder has all permissions set to read, write and execute
every time i try to upload an image it go to the else statement.
not sure what i'm doing wrong here.how do i make it work properly?
Do you have attribute in your form?
enctype="multipart/form-data"
I.e.:
<form action="..." method="post" enctype="multipart/form-data">
Then you can try to check permission for that folder.
And also try change filepath:
$newLoc="./images/" . $_FILES['prodImg']['name'];
or
$newLoc="images/" . $_FILES['prodImg']['name'];
The path of your $newLoc variable may be wrong. Try ./images/.
The one specified may define an image folder on the same level as the home, root... folders if you use Linux.
remove the slash ahead of images
$newLoc= "images/".$_FILES['prodImg']['name'];

How do I make an upload file form to my blog?

I want to upload the files to this address: http://chusmix.com/Imagenes/grupos and I'm trying with this simple this code but it doesn't work:
<form enctype="multipart/form-data" method="post" action="http://chusmix.com/Imagenes/grupos">
Please specify a file:<br>
<input type="file" name="datafile" size="40">
</p>
<div>
<input type="submit" value="Send">
</div>
</form>
Oddly enough, the first result of a Google search yielded this rather helpful tutorial. Why not read it?
Read the PHP manual chapter "Handling file uploads":
http://php.net/manual/en/features.file-upload.php
The way you think uploads work is not the way they work. The form posts to the script you want to handle the request, not the location you want the uploads to be. When you upload a file to Apache, it places that file in the temporary directory of the computer (in Linux, that's /tmp by default).
Your script has to move the file from the temp directory to wherever you want it to be. The manual has plenty of code showing you how.
Make sure the form is loaded via
http://chusmix.com/Imagenes
The browsers wont you allow to upload to a unkown website (Same origin policy).
Edit your form
<form enctype="multipart/form-data" method="post" action="/grupos">

Categories