php - Display full URL to uploaded image with echo statement -


i found upload script perfect needs renames images here.

works great need modify displays full url image after uploaded.

have working extent stuck , need input.

my mod above referenced script:

$url  = isset($_server['https']) ? 'https://' : 'http://';         $url .= $_server['server_name'];         $url .= $_server['request_uri'];          echo dirname(dirname($url)).$uploadresult;         echo "<br />";         echo "<br />";         echo "<a href='$uploadresult' target='_blank'><img src='$uploadresult' alt='picture'></a><br/>";         echo "<br />";         echo "<form><input name='imgcode' size='60' type='text' value='$uploadresult' /></form>";         echo "<br />"; 

above outputs this:

enter image description here

the issue have is: need remove .. path (../images/01092016211821.gif)

have explored , attempted str_replace function cannot working.

would echo full image url path inside input area make copying easy.

thanks in advance advice/solution.

here full script...

example.php:

<?php        if(isset($_post["send"])){      // if form submited         require_once("uploadclass.php");             $file=$_files["filefield"];                 // file form          $destination="../images/";         if (!file_exists($destination)) {       // if 'destination' folder dosn't exist, create             mkdir($destination);         }          $process=new upload($destination);      // set 'destination' new default destination folder upload          $uploadresult=$process->executeupload($file);   // attach file upload process          $url  = isset($_server['https']) ? 'https://' : 'http://';         $url .= $_server['server_name'];         $url .= $_server['request_uri'];          echo dirname(dirname($url)).$uploadresult;         echo "<br />";         echo "<br />";         echo "<a href='$uploadresult' target='_blank'><img src='$uploadresult' alt='picture'></a><br/>";         echo "<br />";         echo "<form><input name='imgcode' size='60' type='text' value='{dirname(dirname($url).$uploadresult)}' /></form>";         echo "<br />";         } ?>  <form action="?" method="post" enctype="multipart/form-data">     <table id="dyntable" class="table table-bordered">         <tr>             <td>                 file             </td>             <td>                 <input type="file" name="filefield" id="filefield" placeholder="">             </td>         </tr>         <tr>             <td colspan="2">                 <center>                     <button type="submit" name="send">send</button>                     <button type="reset">reset</button>                 </center>             </td>         </tr>     </table> </form> 

uploadclass.php:

<?php     /// classe d'upload de uploaded_file     class upload{         protected $destination="img/uploads/";  //default destination folder         protected $max_file_size=7500000;       //max file size         protected $authorized_extensions=array('png','jpg','gif','bmp','jpeg','pdf','word','txt');      //authorised file extensions          public function __construct($dest=null,$types=null){             $this->setparameters($dest,$types);         }          // set general parameters         public function setparameters($dest=null,$types=null){             if($dest!=null && $dest!=""){                 $this->destination=$dest;             }             if($types!=null && $types!=""){                 $this->authorized_extensions=$type;             }         }          // return uploaded file path or errormessages list         public function executeupload($uploaded_file,$destination=null){             if($destination==null || $destination==""){                 $destination=$this->destination;             }             if (!file_exists($destination)) {       // if 'destination' folder dosn't exist, create                 mkdir($destination);             }              $i=0;             $errormessage="";             $errormessage1="";             $errormessage2="";             if (!empty($uploaded_file)){    //if existing file submited                 //on vérifie si la taille du uploaded_file envoyé est acceptable                 $file_size = $uploaded_file['size'];                 if ( $file_size > $this->max_file_size )                 {                     $errormessage = "file heavy. current max file size : ".$this->max_file_size;                     return "";                 }                  //on définit les variables :                 $extensions_valides = $this->authorized_extensions; //liste des extensions valides                 if ($uploaded_file['errormessage'] > 0)                 {                     $i++;                     $errormessage = "file transfert failed";                 }                  $extension_upload = strtolower(substr(strrchr($uploaded_file['name'], '.')  ,1)); //get uploaded_file extension                 if (!in_array($extension_upload,$extensions_valides) )                 {                     $i++;                     $errormessage2 = "extension forbidden";                 }             }             //echo $errormessage2;              if ($i == 0) // if errormessage have been detected             {                 if (isset($uploaded_file['size']))                 {                     //move uploaded_file desired place                     $ico="chaine";                     $horodatage=date("dmyhis");                     $nomico = str_replace(' ','',$ico).".".$extension_upload;                     $ico = $destination.str_replace(' ','',$horodatage).".".$extension_upload;                     move_uploaded_file($uploaded_file['tmp_name'],$ico);                      return $ico;                 }else{                     return null;                 }             }else{                 echo "<h3>upload failed</h3>";                 echo "<table><tr><td> <b><u>cause(s) :</u></b></td></tr><tr><td>";                 echo $errormessage.'<br>';                 echo $errormessage1.'<br>';                 echo $errormessage2.'<br>';                 echo "</td></tr></table>";                 //return "0";                 return null;             }         }     } ?> 

my solution based on input @forbs response:

<?php        if(isset($_post["send"])){      // if form submited         require_once("uploadclass.php");             $file=$_files["filefield"];                 // file form          $destination="../images/";         if (!file_exists($destination)) {       // if 'destination' folder dosn't exist, create             mkdir($destination);         }          $process=new upload($destination);      // set 'destination' new default destination folder upload          $uploadresult=$process->executeupload($file);   // attach file upload process          $url  = isset($_server['https']) ? 'https://' : 'http://';         $url .= $_server['server_name'];         $url .= $_server['request_uri'];          if (substr($uploadresult,0,2) == "..")         $uploadresult = substr($uploadresult,2);          echo dirname(dirname($url)).$uploadresult;         echo "<br />";         echo "<br />";          } ?>  <form action="?" method="post" enctype="multipart/form-data">     <table id="dyntable" class="table table-bordered">         <tr>             <td>                 file             </td>             <td>                 <input type="file" name="filefield" id="filefield" placeholder="">             </td>         </tr>         <tr>             <td colspan="2">                 <center>                     <button type="submit" name="send">send</button>                     <button type="reset">reset</button>                 </center>             </td>         </tr>     </table> </form>  <textarea id="mytext" rows="3" cols="80"><?php echo dirname(dirname($url)).$uploadresult; ?></textarea> 

solves issue enough finish project.

1) remove leading 2 periods if appear right?

 if (substr($uploadresult,0,2) == "..")     $uploadresult = substr($uploadresult,2); 

2) why not change input to

value ='{dirname(dirname($url).$uploadresult)}' solve that?


Comments