PHP - Unlink() Refusing To Delete File
I am trying to delete a file via unlink(). It's a .html file, stored in a folder on its own.
// current directory $dir = dirname(__FILE__); $dir = str_replace('classes','',$dir); $file = str_replace('./',$dir,$fr['location']); unlink($fr['location']); if(!unlink($file)){ echo 'err'; } if(file_exists($file)){ echo 'ok1'; } The $fr['location'] stores the real location, as ./uploads/1303577497/barebones.html. I've tried every combination I can think of to delete it. I've even deleted it from FTP and re-uploaded it, in case it was a permissions issue, but it refuses to go. When I file_get_contents it, it appears as existing. Can anyone help? Similar TutorialsBasically i have a table show the contents of database (id, name, image type, image size). This is it, http://dvplus.webuda.com/testing/delete-image.php The image on the last column i want it have a href on that image that will delete the file directory that relevant on the table row. I tried to user <a href="<? unlink="" ?>img scr</a>....., any one got any idears??? I want to delete a file, I'm using Windows XP, however, it gives me Permission Denied: Code: [Select] $file =$row['img'] ; $filedel = "mages/".$file; unlink ($filedel); Is the code wrong? I tried setting permission to the folder but in vain... I have a download-list, and want to have a delete-button/link at each file. I have tried different codes, but i'm new to php and need some help. if ($handle = opendir('files/engelsk/')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $en .= '» <a href="/files/engelsk/'.$file.'">'.$file.'</a> - <i><a href="#">Delete file</a></i><br />'; } } closedir($handle); } It's the "<a href="#">Delete file</a>" I want to be the delete-button. Please help! I wonder whether someone may be able to help please. I've put together the following page http://www.mapmyfinds.co.uk/development/deletetest.php which allows users to view a gallery of their uploaded images. I'm now starting to work on the `deletion` functionality and I've attached a suitable icon and the Javascript code so the user can select the image to delete. From the research I've done, I know that to delete the files from my server I need to use the PHP `unlink` command. The problem I'm having is I'm not sure how to tie up the 'deletion' selection made by the user and the physical deletion of the files. I've added my `unlink` code below which uses the relative path and then for the thumbnails, Ive added the echo source filename, so the image in the Thumbnails folder matches the one of the same name in the gallery being deleted, but as I said, it's at this point that I'm not sure what to do next. Code: [Select] <?php unlink($path); unlink($path . 'Thumbnails/' . "<?php echo $source; ?>"); ?> I just wondered whether someone could perhaps have a look at this please and possibly provide some guidance on how I can link up the Javascript `click` event and the deletion of the files. Many thanks and kind regards After a user on my site deletes their account I want their profile picture to be deleted from my images folder. I was testing the unlink function on my index.php file and it is failing. Here is the error: Warning: unlink(/assets/img/avatars/users/jasongordon.png): No such file or directory in /home/www-data/big.com/index.php on line 86 My code was just: unlink('/assets/img/avatars/users/jasongordon.png'); I'm not sure why this fails because my file does exist. any help with this please? it works fine with deleting database results but the unlink wont unlink file from dir? Code: [Select] else if($action=="del") { $id = $_GET["id"]; $cls = $_GET["cls"]; $dir = "gallery/$cls"; echo "<p align=\"center\">"; $owner = mysql_fetch_array(mysql_query("SELECT uid FROM gallery WHERE id='".$id."'")); if(mod(getuid_id($id))||getuid_id($id)==$owner[0]) { $res = mysql_query("DELETE FROM gallery WHERE id='".$id."'"); $res2 = mysql_query("DELETE FROM comments WHERE pid='".$id."'"); $res3 = mysql_query("DELETE FROM rate WHERE pid='".$id."'"); if($res||res2||res3 || unlink($dir)) { echo "Photo Deleted From Gallery<br/>"; }else{ echo "Database Error!<br/>"; } }else{ echo "You can't delete this Photo"; } Dear forum I'm new to this forum and since I'm doing more and more php development, you'll probably going to see me more. For now I got this problem and I hope you guys can help me out =(. I'm building an administartion panel for a website. On this panel the administrator can upload pictures for a gallery. This part works, so normally permissions shouldn't be a problem. The file names are stored in my mysql database for example "pic2.jpg", "pic2.jpg", ... Now I got this form to delete pictures from a specific gallery. It deletes from the database but it just wont delete the files. I'm using a local xampp server. I got the following code: Code: $merk = $_POST["MerkLeegMaken"]; $sql = "SELECT * FROM catalogus WHERE merkId=" . $merk; $resultSet = mysql_query($sql); while ( $row = mysql_fetch_array($query) ) { $url = "../uploads/" . $row["foto"]; // this url is f.e. "../uploads/picture1.jpg", and I'm currently in "/administrationPanel/form.php fclose($url); unlink($url); } $sql = "DELETE FROM catalogus WHERE merkId=" . $merk; mysql_query($sql); $message = "<p class='success'>Het huidig merk is succesvol leeggemaakt.</p>"; (I got in my root dir the directory /uploads and the directory /administartionPanel, so ../uploads should be correct right?" I also tried to delete with "c:/xampp/xampp/htdocs/uploads/pic1.jpg" but that didn't work either. But shouldn't "../uploads/pic1.jpg" work anyway, since I use that same url as the source of an Image below the form. All my different url attempts didn't work. I kept finding the files in my windows explorer =( Hope someone can help. www.phpkode.com i want to delete a file from FOXML directory. I had tried this code but no use... <?php $path="/srv/fedora/tomcat/webapps/formConfirm_1/WEB-INF/classes/FOXML"; $dh = opendir($path); while ($temp = readdir($dh)) { if ($temp!='.' && $temp!='..' && $temp!='.htaccess') chmod($temp,777); } $filename = "/srv/fedora/tomcat/webapps/formConfirm_1/WEB-INF/classes/FOXML/myfile.xml"; unlink($filename); ?> any suggestions?? Hi, I have built a captcha system that saves the captcha to a temp folder with the file name captcha_CURRENT_TIME_STAMP where CURRENT_TIME_STAMP is the unix time. I want to delete images older than 5mins old by reading the time stamp on the file name. My question is this: How can i loop through the image folder and read the time stamps on the file name and delete all images older than 5mins? Thanks Hey guys! Everytime i upload something to the server theres a files created called Resource id #10 (number increments after eachj upload) From what i can ubserve, those are the TMP file ... but for somereason it's staying there and piles up and takes out a lot of space. Is there a way to delete them when the upload is finished? hi friends!! in php > 5 i use "session_regenerate_id(TRUE)" in order to generate a new session file and delete old one. In php 4.x the TRUE parameter doesn't exist: old files will be accumulated until the garbage collector will cleans all. How can I delete old session file in php 4.x having regenerated the new one? Hello Mates, I am in need of help. I have a Flat File News Script. I am not sure how to build the Delete script.. i am not knew to PHP but kinda new to Flat File Database.. the reason why im using FFD is because my service doesn't allow mysql on the cheaper priced web pages.. i have 4 fields ID || user_name || title || message i want it to check the id because it will be in the url like http://webpage.com/delete_news.php?id=3 and the id 3 and the other fields in id 3 is deleted. Thank you Valkeri2010 Hello everyone, This is my first post and I need a little help. My knowledge of php is limited, so this question may look simple. English isn't my mother tongue, which you have probably noticed already. So, my problem: I 've made a a phpscript that adds text to a txt-file, that works perfect, but I also need something to delete some parts of the file. part of my html-code: Code: [Select] <form method="POST" action="verwijderscript.php"> <table class="noborder"> <tr><td class="noborder"> <input type="text" id="verwijdertitel" name="verwijdertitel" size="40"> <input type="submit" name="s1" value="Verwijder Bericht" /> </td></tr> </table> </form> part of my txt-file: Code: [Select] <div id="Testtitel"> <span class="subtitel">Testtitel</span> <span class="rechts">Gepost op 15/03/2011</span> <br> <img class="nieuws" src="image.gif"><br> texttexttexttexttext<br> texttexttext<br> text <div class="streep"></div> </div id="Testtitel"> <div id="Testtitel2"> <span class="subtitel">Testtitel2</span> <span class="rechts">Gepost op 15/03/2011</span> <br> <img class="nieuws" src="image2.gif"><br> texttexttexttexttext<br> texttexttext<br> text <div class="streep"></div> </div id="Testtitel2"> part of my php-code: Code: [Select] <?php $titel = $_POST['verwijdertitel']; $oud = file_get_contents("data.txt"); $data = fopen("data.txt", "w+"); $nieuw = str_replace("id=\""$titel, "id=\"hidden", $oud); fwrite($data, "$nieuw"); fclose($data); header("refresh:0;url=http://www.test.com"); ?> css: Code: [Select] #hidden{display:none;} Problem: parse error line 6 of php-file And I don't think this works if the error is solved. I don't have a MySQL database, that's why I use a txt files. What I try to do is to hide the div, it would be better to delete it, but than I need to work with regex, I guess Hello, I'm requesting your help here cause I'm really confused. I'm looking for deleting entries when an image doesn't load on my links test. I got all my links in a .txt file like this : links.txt = Code: [Select] <div class="gallery"> <a href="http://www.website.com/page"> <img src="http://tc16.easythumbhost.com/image.jpg" onerror="ImgError(this)"/> </a> </div> #enddiv the "onerror" code in img src sends an "Image Missing" pic. What I'd want is to change the "onerror" function to open a php script which deletes all the lines of the div. This is the script I use to show the links on my page : Code: [Select] $links = "links.txt"; $fd = fopen ($links, "r"); $content = fread ($fd,filesize ($links)); fclose ($fd); $delimiter = "#enddiv"; $splitcontent = explode($delimiter, $content); $output = array_slice($splitcontent, 0,100); foreach ( $output as $divs ) { echo "$divs"; } I've searched hours and tried lots of codes before posting Help would be very appreciated. Thanks. Hi! What i can't figure out, is how to delete between 2 given strings in another file. Explanation: I have a file, it contains following: Code: [Select] ### Start of content 0 ### Some Radom Lines with text 1 ### End of content 0 ### ### Start of content 1 ### Some Radom Lines with text 2 ### End of content 1 ### ### Start of content 2 ### Some Radom Lines with text 3 ### End of content 2 ### And so on, now i need code, which can find line i give and delete between. Something like this: If, i give string: Code: [Select] ### Start of content 0 ### Then it will delete this line to the Code: [Select] ### End of content 0 ### Hope you can help! All the best: Mart L. I've created a demo page for my CMS, and I want to delete itself and make a fresh content install. I got the delete part figured out, but what about uploading fresh content files? (only a few text files). Here's the delete part: Code: [Select] <?php $expiretime=720; // minutes (in how many minutes it deletes the files) $tmpFolder="tmp/"; // where to delete the files - be careful with this. $fileTypes="*.*"; foreach (glob($tmpFolder . $fileTypes) as $Filename) { $FileCreationTime = filectime($Filename); $FileAge = time() - $FileCreationTime; if ($FileAge > ($expiretime * 60)){ //deleting files: unlink($Filename); } } ?> I want to add text to a file, getting it printed out in index.php and later want to edit/delete per row. I give codes page wise. first the form to take data: add.php: Code: [Select] <?php ?> <form method="post" action="add_data.php"> <fieldset> <legend>Student List</legend> Name : <input type="text" name="uname" /><br> Content : <input type="text" name="age" /> <br> <input type="submit" name="submit" value="send" /> </fieldset> </form> Next adding text to a file: add_data.php: Code: [Select] <?php $id = 1; if (file_exists("data.txt")) { $fp = fopen("data.txt", 'r'); $str = fread($fp, filesize("data.txt")); $str_arr = explode("|", $str); foreach ($str_arr as $rec) { if ($rec) { $id++; } } } $mode = (file_exists("data.txt"))? "a" : "w"; $fp = fopen("data.txt", $mode); $line = $id . "--" . $_POST['uname'] . "--" . $_POST['age'] . "|"; $res = fwrite($fp, $line); fclose($fp); header("location:index.php"); ?>In the index.php, I'd like to fetch data and edit them per row: Code: [Select] <?php $fp = fopen("data.txt", 'r'); $str = fread($fp, filesize("data.txt")); $str_arr = explode("|", $str); echo "<table border='1'>"; echo "<tr><td>ID</td><td>Name</td><td>Age</td><td>Option</td></tr>"; foreach ($str_arr as $rec) { if ($rec) { $rec_arr = explode("--", $rec); echo "<tr>"; foreach ($rec_arr as $col) { echo "<td>$col</td>"; } print "<td><a href=\"edit.php?id=$id\">Edit</a>/<a href=\"delete.php?id=$id\">Delete</a></td></tr>"; } } echo "</table>"; echo "<a href=\"add.php\">Add Data</a>"; ?> Next I want to Edit per row: edit.php: Code: [Select] <?php $fp = fopen("data.txt", 'r'); $str = fread($fp, filesize("data.txt")); $str_arr = explode("|", $str); foreach ($str_arr as $rec) { if ($rec) { $rec_arr = explode("--", $rec); $id = $rec_arr[0]; $name = $rec_arr[1]; $age = $rec_arr[2]; if ($id == $_REQUEST["id"]) { echo "<form action='edit_data.php' method='post'>"; echo "Name :<input type='text' name='uname' value='$name' /><br>"; echo "Age: <input type='text' name='age' value=$age /><br>"; echo "<input type='hidden' name='uid' value=$id />"; echo "<input type='submit' value='Edit'>"; echo "</form>"; } } } ?> From edit.php to edit_data.php where I practically try to edit per row: Code: [Select] <?php $fp = fopen("data.txt", 'r'); $str = fread($fp, filesize("data.txt")); $str_arr = explode("|", $str); foreach ($str_arr as $rec) { if ($rec) { $rec_arr = explode("--", $rec); $id = $rec_arr[0]; $name = $rec_arr[1]; $age = $rec_arr[2]; if ($id == $_POST['uid']) { $new_str = $id . "--" . $_POST['uname'] . "--" . $_POST['age'] . "|"; } else { $new_str = $id . "--" . $name . "--" . $age . "|"; } } } fclose($fp); $fp = fopen("data.txt", 'w'); fwrite($fp, $new_str); fclose($fp); ?> The mechanism is simple. Taking datas from a form, explode them to an array and get them into col/row pattern to edit and delete them. But my problem is when I want to edit in index.php page, specially in this part: Code: [Select] print "<td><a href=\"edit.php?id=$id\">Edit</a> It says, undefined index. But I try to catch this $id in the edit.php page, in this manner, as you see in my code: Code: [Select] foreach ($str_arr as $rec) { if ($rec) { $rec_arr = explode("--", $rec); $id = $rec_arr[0]; $name = $rec_arr[1]; $age = $rec_arr[2]; if ($id == $_REQUEST["id"]) { echo "<form action='edit_data.php' method='post'>"; echo "Name :<input type='text' name='uname' value='$name' /><br>"; echo "Age: <input type='text' name='age' value=$age /><br>"; echo "<input type='hidden' name='uid' value=$id />"; echo "<input type='submit' value='Edit'>"; echo "</form>"; } } } Have I done any mistake here? If anyone points out, I'll be obliged. Extremely new here and to PHP and I have exhausted my newbie resources and my brain on this one. Concept: Cron Job to INSERT 5 lines per hour (Not the whole list at once) Script to read a pipe delimited text file (about 5k Lines) Open File Get last (bottom) 5 lines of file, ignore the rest Insert those 5 lines into my database Open File, Remove last (bottom) 5 lines I am able to do parts 1-3 but not part 4 (removing last 5 lines) Please help me! Code: [Select] $data = array_slice(file('data.txt'), -5); foreach($data as $value) { $fields=explode("|",$value); $num=count($fields); for ($n=0;$n<$num;$n++) { $null=""; print ($n==0) ? "Importing Video...<BR>" : $null; } // Insert into tmpvideos table here include("includes/config.php"); $last_id_result = @mysql_query( @"SELECT ext_id FROM tmpvideos ORDER BY ext_id+0 DESC, id DESC" ); if ( 0 < @mysql_num_rows( $last_id_result ) ) { $last_id = @mysql_result( $last_id_result, 0, "ext_id" ) + 1; } else { $last_id = "1"; } copy($fields[3], "thumbs/ex_".$last_id.".jpg" ); $fields[3] = "thumbs/ex_".$last_id.".jpg"; set_time_limit(0); mysql_query("INSERT into tmpvideos (id, ext_id, title, description, duration, category, date_added, thumb, video, views, rating, votes, status) VALUES ('','".$last_id."','$fields[0]','$fields[1]','$fields[5]','$fields[6]',NOW(),'$fields[3]','$fields[2]','','','','1')"); } I understand the 'concept' but am unable to grasp the code! I have tried many snippets and really need help. Hello all. In lack of better terms, I am a programming noobie when it comes to designing code. I can look at a code and figure it out, but basically I am trying to figure this out. I host a website from home, running on a server 2008 machine, with apache/php, basically to share files from home, work, friends house, wherever I need. I do have a FTP for this, but I also want to be able to upload files via http. I actually want to be able to set a time or date that the file will auto-delete. Such as, I select a file from my local machine, set the deletion time to 1 hour/60 minutes/3600 seconds, whatever, upload it to the site, and it spit back a url that I can access that file for that set amount of time. It would be great to implement something like bit.ly to shorten the link, or even something to randomize the link. Any ideas on that from anyone? Willing to donate (paypal) a little bit if someone can actually help me out, or point me in the right direction. A week on google has proved useless. Thanks so much. Hey all need some help with the subject title. Have a user text file with all information(Name, address, passwords, etc..) Need to be able to select the user based on Checkbox selection then hit delete button and all users checked will be removed from the txt file and thus removed from HTML page. Im using PHP to show contents of the txt file into my HTML page. So far i have: <?php $file = fopen("user.txt","r"); echo '<table border="0" width="100%"> <tr> <th>Select</th> <th>Username</th> <th>Password</th> <th>FirstName</th> <th>LastName</th> <th>Email</th> <th>Address</th> </tr> '; while(!feof($file)) { $user = explode(":", fgets($file)); echo '<tr> <td><input type="checkbox" name="chkDelete" id = "chkDelete" value="select" /></td> <td>' . $user[0] . '</td> <td>' . $user[1] . '</td> <td>' . $user[2] . '</td> <td>' . $user[3] . '</td> <td>' . $user[4] . '</td> <td>' . $user[5] . '</td> </tr>'; echo '</table>'; fclose($file); ?> Am unsure as to how i would go about doing this, help would be greatly appreciated! Thanks in advanced, Daniel. |