2016-09-01 71 views
0

我發現上傳腳本非常適合需要,因爲它將圖像重命名爲here使用echo語句顯示上傳圖像的完整URL

工程很好,但需要修改,以便在上傳後顯示完整的URL圖像。

有些工作但卡住了,需要輸入。

我的MOD上面所提到的腳本:

$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 />"; 

以上輸出是這樣的:

enter image description here

我的問題是:需要從路徑中刪除。( ../images/01092016211821.gif)

已經探索並嘗試了str_replace函數,但c annot開始工作。

還想回顯輸入區域內的完整圖像URL路徑,以便複製。

在此先感謝您的建議/解決方案。

下面是完整的腳本...

使用example.php:

<?php 
    if(isset($_POST["Send"])){  // If form is submited 
     require_once("uploadClass.php");  
     $file=$_FILES["fileField"];     // Get file from form 

     $destination="../images/"; 
     if (!file_exists($destination)) {  // If 'destination' folder dosn't exist, create 
      mkdir($destination); 
     } 

     $process=new Upload($destination);  // Set 'destination' as new default destination folder for upload 

     $uploadResult=$process->executeUpload($file); // Attach file to 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 is really 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 too heavy. Current max file size is : ".$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 any errorMessage have been detected 
      { 
       if (isset($uploaded_file['size'])) 
       { 
        //Move the uploaded_file to the 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; 
      } 
     } 
    } 
?> 

根據輸入我的解決方案,從@Forbs響應:

<?php 
    if(isset($_POST["Send"])){  // If form is submited 
     require_once("uploadClass.php");  
     $file=$_FILES["fileField"];     // Get file from form 

     $destination="../images/"; 
     if (!file_exists($destination)) {  // If 'destination' folder dosn't exist, create 
      mkdir($destination); 
     } 

     $process=new Upload($destination);  // Set 'destination' as new default destination folder for upload 

     $uploadResult=$process->executeUpload($file); // Attach file to 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> 

解決我的問題足以完成項目。

+0

你爲此做了什麼?你能告訴我們你的代碼嗎? – tanaydin

+0

@tanaydin與我是新的不知道是否應該只提供鏈接到上傳腳本或粘貼全部英寸是鏈接不好的方式來顯示腳本@ http://www.phpclasses.org/package/9906-PHP -Validate-and-process-an-uploaded-file-.html – Woody

+0

我的壞@tanaydin不知道用戶必須在phpclasses站點註冊/登錄。增加了整個腳本。 – Woody

回答

0

1)如果顯示正確,刪除前兩個句點?

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

2)爲什麼不將輸入切換到

值= '{目錄名(目錄名($ URL)。$ uploadresult)}' 來解決這個?

+0

1)是的,刪除兩個時期。喜歡將它們去掉,所以不會出現在URL中。只是不積極在哪裏放置你推薦的代碼。 2)當添加推薦值時,我得到:{dirname(dirname(http://myurl.net/testbed/AdminLTE/upload/?)。)} – Woody

+0

謝謝@Forbs!你的意見讓我走了。不得不做一些像$ uploadresult到$ uploadResult的更改。接受你的答案。 – Woody