使用echo语句显示上传图像的完整URL

时间:2016-09-01 21:45:41

标签: php upload echo

我找到了一个非常适合需要的上传脚本,因为它重命名了图片here

效果很好但需要修改,以便在上传后显示图像的完整URL。

在某种程度上工作但仍然需要输入。

我对以上引用脚本的修改:

$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函数,但无法正常工作。

还要回显输入区域内的完整图像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>

解决我的问题,足以完成项目。

1 个答案:

答案 0 :(得分:0)

1)如果它们出现正确,则删除前两个句点?

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

2)为什么不将输入更改为

value ='{dirname(dirname($ url)。$ uploadresult)}'来解决这个问题?