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?
Related
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.
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/>
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'];
We are trying to create a site for wallpapers so that would be somehow large files(2mb-5mb) so we'll be needing to store the images on the disk space instead in the database and only the paths to the database. So if you can give some ideas on how to do that (the method we know for now is creating a PHP script with the upload function and by manually selecting the images from the PC to be uploaded) unless you guys would have other suggestions. Tutorials will be much appreciated. Thanks a lot!
This is for the admins to add images not for the users.
Note: we haven't developed any script so this is to get some ideas from you guys on what we can use with this, if none guess we will just go with the php script.
Your form,
<form action="PHP_FILE_PATH.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="submit" value="Upload" />
</form>
The PHP Part
<?php
if($_FILES['image']['name'])
{
$save_path="FOLDER_PATH_TO_SAVE_UPLOADED_IMAGE"; // Folder where you wanna move the file.
$myname = strtolower($_FILES['image']['tmp_name']); //You are renaming the file here
move_uploaded_file($_FILES['image']['tmp_name'], $save_path.$myname); // Move the uploaded file to the desired folder
}
$inser_into_db="INSERT INTO `database`.`table` (`folder_name`, `file_name`) VALUES('$save_path', '$myname'))";
?>
For each file uploaded, generate a UUID and use that for the filename on disk. That avoids collisions, sanitizing filenames and path traversal vulnerabilities.
You'll have a table like this: (id, description, filename) with values like (1, "Green field", "0D729DCD-5116-4480-81CE-90A0380B557A.png").
Next, you want to avoid the problem of having too many files in one folder — you'll hit a filesystem limitation for many FSes.
To work around this problem, create directories based on the first few letters of the filename. For 0D729DCD-5116-4480-81CE-90A0380B557A.png, you would store it in /0/D/7/0D729DCD-5116-4480-81CE-90A0380B557A.png.
now first thing is I've seen everywhere over the net what to do in the case that what I'm trying to do doesn't work, I've tried all of the solutions and they don't work, I'm obviously missing something.
I'm uploading multiple files from a form field. This works perfectly and runs some code that resizes etc deletes tmp files blah blah.
The problem is if I don't want to upload any files the upload and image processing script still runs throwing a bunch of errors.
I've tried the following... plus a bunch more with some weird variations :P
if($_FILES['gallery']['name']!=""){ // if files then...
include_once("gallery_edit_script.php");
}
and
if (count($_FILES["gallery"]["name"] > 0)){ // if files count is more than 0 then...
include_once("gallery_edit_script.php");
}
Would the fact that the gallery_edit_script.php is an include have something to do with it?
I checked the file error with...
$_FILES["gallery"]["error"]
It showed no files were selected to upload which was exactly what I wanted.
Any ideas people?
Thanks for anyone who has a look at this.
Cheers
Added HTML but like I said upload is working fine, it's when I want to post the form and not include files to upload that I want it to skip the gallery script. This is on an edit page, so the user has submitted form, data added to db and files uploaded, then comes back and wants to edit data but not upload files.
HTML (simplified as there are heaps of fields etc)
<form action="inventory_edit_lease.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">
Gallery Photos <input class="input-file" type="file" name="gallery[]" id="gallery" multiple="multiple" />
<input class="button-edititem" type="submit" name="submit" id="button" value="" onclick="javascript:return validateMyForm();"/>
</form>
Sorry I didn't add HTML first time round, form works so didn't think I really needed it ;)
Few check lists...
Make sure you have named your <input type="file" /> as gallery:
<input type="file" />
Make sure the <form> tag has a method="post" and action="" to the correct URL.
Also, make sure your <form> tag has enctype="multipart/form-data" else you won't be able to upload files via that form!
We need to see the HTML Code of your file before we can suggest something. Make sure you have followed the above checklists and even then if it isn't working, post the code and let us know!
Without HTML form I'm just guessing:
for multiple file uploads with same name you must have the filed as
on server side you will receive them as $_FILES["gallery"]
$_FILES["gallery"] will be an array of elements, eg:
foreach($_FILES["gallery"] as $file){
var_export($file);
}
For those interested this is what worked :)
I got this from another thread if($_FILES['files']['name']!="") run if files, don't run if no files headache
if(!empty($_FILES['gallery']['tmp_name']))
{
include_once("gallery_edit_script.php");
}
else
{
header("Location: inventory_list_sales.php");
}
Funny thing is I tried this with another site I'm working on with almost identical code as I copied all the files and only edited small parts and it doesn't work lol
Thanks for everyone's help :)