dongzhi4073 2016-09-01 21:45
浏览 84
已采纳

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

I found a upload script that is perfect for needs as it renames images here.

Works great but need to modify so it displays the full URL to image after uploaded.

Have working to some extent but stuck and need input.

My mod to the 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 like this:

enter image description here

The issue I have is: need to remove the .. from path (../images/01092016211821.gif)

Have explored and attempted the str_replace function but cannot get working.

Would also like to echo the full image URL path inside input area to make copying easy.

Thanks in advance for advice/solution.

Here is full script...

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;
            }
        }
    }
?>

My Solution based on input from @Forbs response:

<?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>

Solves my issue good enough to finish project.

  • 写回答

1条回答 默认 最新

  • doulan3436 2016-09-01 21:49
    关注

    1) Remove the leading two periods if they appear right?

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

    2) why not change the input to

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器