unlink() file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kelvinwebdesigner
    New Member
    • Jan 2010
    • 5

    unlink() file

    Hi everybody!
    Im having trouble doing this task hope someone could help.

    I still working on my project/app where user can upload a file a server, that then provide him with the link that can be shared for a certain period of time.
    I want, using <option> tag, to make a user select the max time that he would like to share the image. For example when the user select 30 minutes, the php file get the server time, and then plus 30 minutes unlink/delete the file from database. (thats my idea). Im having problem with it.

    I leave you with this code so far! The problem its obvious on time function.

    _Display the option____

    Code:
    <?
    function delete_file(){
    	$tempo = array ("+30 minutos", "+1 hora");
    	echo '<select name="tempo">';
    	foreach ($tempo as $key =>$value){
    		echo "<option id='select_expire' value=\"$key\" >$value</option>\n";}
    	
    	echo'</select>';
    }//ende of the function
    	
    	//call the form taga:
    	echo '<label for="select_expire">Escolhe o expire time:</label>';
    	//call the function
    	delete_file();
    	
    ?>

    _And this is my code so far, but obviously with many mistakes______

    Code:
    <?
    if (isset($tempo['30 minutes'])){
    	$file = $user_file;
    	$expire = $time + 30;
    	$time = time();
    	unlink($file);}
    ?>
    Last edited by Dormilich; Jan 12 '10, 08:49 PM. Reason: Please use [code] tags when posting code
  • dgreenhouse
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    First, the delete logic should be uncoupled from the form logic.

    Probably running a cron job every 5 minutes or so...

    I'd just have the image file names and expiry times inserted into a database.

    Then, when the cron job runs, it checks the "sharing" table looking for expirations and deletes those files it finds that should be expired.

    A better; and more complex, design wouldn't delete the files at all, but only share them if they're still within the sharing period. In addition, the app could allow the 'sharing user' to elect to either delete the image upon expiration or just un-share it. That way, the ‘sharing user’ could elect to share it again at a later date. Using this logic, it would be easy to extend the application further to allow the 'sharing user' to give specific '"share-ee" users' access to the images. Obviously, you can extend this ad-infinitum - but that's the joy of 'over engineering!' ;-)

    I'm not sure of your complete design criteria here, but having it coupled to a form ensures it will not get run until someone accesses the form.

    Or, maybe I'm missing something...

    Comment

    • kelvinwebdesigner
      New Member
      • Jan 2010
      • 5

      #3
      Thanks for the help.
      But later i figure it out! maybe using setcookie would be better!

      So i came up with this code. But for some reason is not working, as well as option select.

      [CODE=php]<?
      setcookie("cook ie[one]","cookieon e", time() - 1800);
      setcookie("cook ie[two]","cookietw o");

      if (isset($_COOKIE["cookie"])) {

      foreach ($_COOKIE["cookie"] as $name =>$value)
      echo '<select name="cookie">' ;

      {

      echo "<option id='select_expi re' value=\"$name\" >$value</option>\n";}

      echo'</select>';
      }//ende of the function

      //call the form:
      echo '<label for="select_exp ire">Escolhe o expire time:</label>';
      //call the function
      ?>[/CODE]

      Any help?
      Last edited by Atli; Jan 21 '10, 09:18 AM. Reason: Added [code] tags.

      Comment

      • dgreenhouse
        Recognized Expert Contributor
        • May 2008
        • 250

        #4
        Could you explain what you're trying to accomplish with this code.

        As I mentioned in my last post, I think you're going about this the wrong way.
        ( I could be wrong and missing the obvious and simple.)

        The sharing logic SHOULD not be tied to a FORM's logic in anyway; other than the expiration time being entered by the SHARING user.

        Can you explain why you want to use a form?

        Also...

        How are the proposed images that are to be shared presented to users?

        What is this to be used for? Is this an actual application, a school project, or just a personal project?

        I realize that the program logic in your initial post is quite simple, it's just that I don't think you've provided enough information to help you.

        I guess you could use the logic you described and base the expiration time off of the time the (file was / files were) uploaded to the server, but where are you going to store the expiration time?

        Is it 30mins, an hour, etc.?
        If the time is going to be in a cookie and it is of variable in length - then - who's cookie?

        I hope I'm making sense...
        Last edited by dgreenhouse; Jan 21 '10, 08:15 PM. Reason: Expanding initial post response

        Comment

        • kelvinwebdesigner
          New Member
          • Jan 2010
          • 5

          #5
          Hi dgreenhouse, this post is a continuation of the first one where i did the introduction of this application! fortunately i solved the problems that i has with headers last time. So check it out!



          Maybe you're missing something! The thing is the user upload the photo, and choose for how long he wants to use the service! In case he chooses 30 minutes, the file will be deleted in that time, and the link will be no longer available.

          Maybe i could do it just interracting with the database thus,

          Code:
          <?php
          
          $link = @mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
          $database = mysql_select_db(DB_NAME);
          $sql = "SELECT * FROM temp WHERE DATE_ADD(time, INTERVAL 30 MINUTE) < NOW()";
          $result = mysql_query("$sql");
          while ($out = mysql_fetch_array($result)) { 
             $dirname = $out['directory'];
             $filename = $out['filename'];
          
             $file = "$dirname/$filename";
          
             if(file_exists($file))
             unlink($file);
          
             rmdir($dirname);
             
             $link = @mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
             $database = mysql_select_db(DB_NAME);
             $sql = "DELETE FROM temp WHERE directory = '$dirname'";
             mysql_query("$sql");
          }
          
          mysql_close($link);
          
          ?>

          Comment

          Working...