php - how to delete a file on the server and add a new file based on clients choice -


i have figured out how upload file server if file not exist. if file exist client able overwrite/delete existing file , upload new file. yes, both files must have same name.

so attempting when file existed use switch statement calls function either delete existing file , upload new file or force php code die therefore canceling upload

    if (isset($_get['run'])) $linkchoice=$_get['run'];      else $linkchoice='';       switch($linkchoice){        case 'one' :            upload_file();           break;       case 'two' :            kill();           break; 

then link execute function

   <a class=button href='?run=one'>replace existing file</a><br/>    <a class=button href='?run=two'>cancel upload</a> 

here how tried put together.

    <?php     error_reporting(e_all);     ini_set('display_errors',1);       $allowedexts = array("xml");     $temp = explode(".", $_files["file"]["name"]);     $extension = end($temp);     if (($_files["file"]["type"] == "text/xml")     && in_array($extension, $allowedexts)){       if ($_files["file"]["error"] > 0){         echo "return code: " . $_files["file"]["error"] . "<br>";       }        else{         if (file_exists("simxml/" . $_files["file"]["name"])){          echo $_files["file"]["name"] . " file exists.<br/>";         $file = $_files["file"]["name"];         $type = $_files["file"]["type"];         $size = ($_files["file"]["size"] / 1024);         /* if (isset($_get['run'])) $linkchoice=$_get['run'];            else $linkchoice='';               switch($linkchoice){               case 'one' :                upload_file($file,$type,$size,$temp);                case 'two' :                kill();               }             echo "<a class=button href='?run=one'>replace existing file</a><br/>";            echo "<a class=button href='?run=two'>cancel upload</a>"; */             }           else{            echo "file: " . $_files["file"]["name"] . "<br>";            echo "type: " . $_files["file"]["type"] . "<br>";            echo "size: " . ($_files["file"]["size"] / 1024) . " kb<br>";             move_uploaded_file($_files["file"]["tmp_name"],            "/var/www/jd/simxml/" . $_files["file"]["name"]);            echo "stored in: " . "simxml/" . $_files["file"]["name"];            }         }      }      else{        echo "invalid file";        }     ?>     <?php    error_reporting(e_all);    ini_set('display_errors',1);     function delete_file($file){      unlink("/var/www/jd/simxml/" . $file);      }    function upload_file($file,$type,$size,$temp){      echo $file;      delete_file($file);       echo "file: " . $file . "<br>";      echo "type: " . $type . "<br>";      echo "size: " . $size . " kb<br>";       move_uploaded_file($temp,      "/var/www/jd/simxml/" . $file);      echo "stored in: " . "simxml/" . $file;      }    function kill(){       }     ?> 

obviously not work, , struggling tying or implement several concepts/ideas together.

i appreciate advice on figuring out how implement this.

thank in advance, jd

not answer, not suitable comment:

    if (file_exists("simxml/" . $_files["file"]["name"])){ 

here use relative path, if script ever changes current working directory, you'll end no end of trouble because elsewhere:

move_uploaded_file($_files["file"]["tmp_name"], "/var/www/jd/simxml/" . ... 

and

unlink("/var/www/jd/simxml/" . $file); 

you use absolute paths. not idea.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

java - More than one row with the given identifier was found: 1, for class: com.model.Diagnosis -