[thelist] How do I delete uploaded files in PHP?

Nan Harbison nan at nanharbison.com
Mon Nov 12 15:20:09 CST 2007


Anthony,

I tried this ftp method as a stupid wild stab at something that might work,
I found it on a website explaining how to delete files. Resumes are uploaded
using PHP file upload functions, mainly using this function, written by
Marcos Thiago <fabismt at yahoo.com.br>:

  function moveFileToDestination(){

    if(is_uploaded_file($this->tmp_name) && $this->dst_dir){
      if($this->checkUploadConditions()){
        $dst_file_name = ($this->randon_name) ? $this->generateFileName() :
$this->fixFileName($this->name);
        $full_destination_path = $this->dst_dir."/".$dst_file_name;
        if(file_exists($full_destination_path) && !$this->replace) {
          $this->error_type = 7;
        } else {
          if(@move_uploaded_file($this->tmp_name,$full_destination_path)){
            @chmod ($this->dst_dir."/".$dst_file_name,$this->file_perm);
            $this->error_type = 0; 
          } else {
            $this->error_type = 5; 
          }
        }
      }
    } else {
      $this->error_type = 6; 
    }

    if($this->error_type != 0){
      $this->fail_files_track[$this->succeed_track_index]["file_name"]
= $this->name;
      $this->fail_files_track[$this->succeed_track_index]["new_file_name"]
= $dst_file_name;
 
$this->fail_files_track[$this->succeed_track_index]["destination_directory"]
= $this->dst_dir;
      $this->fail_files_track[$this->succeed_track_index]["field_name"]
= $this->fld_name;
      $this->fail_files_track[$this->succeed_track_index]["file_size"]
= $this->size;
      $this->fail_files_track[$this->succeed_track_index]["file_type"]
= $this->type;
      $this->fail_files_track[$this->succeed_track_index]["error_type"]
= $this->error_type;
      $this->fail_files_track[$this->succeed_track_index++]["msg"]
= $this->msg[$this->error_type];
      return FALSE;
    } else {
      $this->succeed_files_track[$this->fail_track_index]["file_name"]
= $this->name;
      $this->succeed_files_track[$this->fail_track_index]["new_file_name"]
= $dst_file_name;
 
$this->succeed_files_track[$this->fail_track_index]["destination_directory"]
= $this->dst_dir;
      $this->succeed_files_track[$this->fail_track_index]["field_name"]
= $this->fld_name;
      $this->succeed_files_track[$this->fail_track_index]["file_size"]
= $this->size;
      $this->succeed_files_track[$this->fail_track_index]["file_type"]
= $this->type;
      $this->succeed_files_track[$this->fail_track_index]["error_type"]
= $this->error_type;
      $this->succeed_files_track[$this->fail_track_index++]["msg"]
= $this->msg[$this->error_type];
      return TRUE;
    }
  }
++++++++++++++++++++++++++
The unlink function didn't work for me. I will reread the php.net page you
sent me. 
Thanks
Nan


-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Anthony Baratta
Sent: Monday, November 12, 2007 4:09 PM
To: thelist at lists.evolt.org
Subject: Re: [thelist] How do I delete uploaded files in PHP?

How are your resumes being posted to the site?? Are you using the built-in
PHP File Upload functions?

http://us.php.net/manual/en/features.file-upload.php

I'm curious as to why you are using FTP at all. Are you receiving them via
the web service and the transporting them via FTP to a specific location?
Are the files local to the web server?

If you login manually with your FTP credentials can you see the files you
expect? Can you delete them manually?

If the files are local to the web sever, make sure that the web server has
the proper permissions on the files. You should be using this PHP function
on files that are local:

http://us.php.net/manual/en/function.unlink.php

-----Original message-----
From: "Nan Harbison" nan at nanharbison.com
Date: Mon, 12 Nov 2007 12:55:03 -0800
To: thelist at lists.evolt.org
Subject: [thelist] How do I delete uploaded files in PHP?

> Hi all,
>  
> I am working on a website where clients can upload a resume, and can 
> upload a new resume in its place. This is working just fine. However, 
> I am having the file names be randomly generated, so the old file is 
> not overwritten. I need to be able to delete the old file, but I am 
> having trouble a tutorial on how to do this, all the links I get in 
> google are for little applications to manage file uploads and downloads.
>  
> -I tried using 'unlink' but it didn't work
>  
>  
> -I tried this but it isn't working. I didn't get any error messages, 
> it just doesn't delete the old resume.
>  
> $file = 'resumes/'.$_POST['oldresume']; $ftp_server = 'my ftp 
> address'; $ftpuser = 'myusername; $ftppass = my password;
>  
> // set up basic connection
> $conn_id = ftp_connect($ftp_server);
>  
> // login with username and password
> $login_result = ftp_login($conn_id, $ftpuser, $ftppass);
>  
> // try to delete $file
> if (ftp_delete($conn_id, $file)) { // the ftp function
>          echo '$file deleted successful';  } else {
>          echo 'could not delete $file';  }
>  
>     // close the connection
>     ftp_close($conn_id);
>  
> Can someone please explain to me how I should do this, or point me to 
> a resource that show me?
>  
>  
> Thanks!
>  
> Nan

-- 

* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester and archives
of thelist go to: http://lists.evolt.org Workers of the Web, evolt ! 




More information about the thelist mailing list