使用 echo 语句显示完整 URL 到上传的图像
Display full URL to uploaded image with echo statement
我找到了一个非常适合需要的上传脚本,因为它可以重命名图像 here。
效果很好,但需要 mod 验证以便在上传后显示完整的 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 />";
上面的输出是这样的:
我遇到的问题是:需要从路径 (../images/01092016211821.gif) 中删除 ..
探索并尝试了 str_replace 功能,但无法正常工作。
还想回显输入区域内的完整图像 URL 路径,以便于复制。
提前感谢 advice/solution。
这是完整的脚本...
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) 如果前两个句号正确,请将其删除?
if (substr($uploadresult,0,2) == "..")
$uploadresult = substr($uploadresult,2);
2) 为什么不把输入改成
value ='{dirname(dirname($url).$uploadresult)}' 来解决这个问题?
我找到了一个非常适合需要的上传脚本,因为它可以重命名图像 here。
效果很好,但需要 mod 验证以便在上传后显示完整的 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 />";
上面的输出是这样的:
我遇到的问题是:需要从路径 (../images/01092016211821.gif) 中删除 ..
探索并尝试了 str_replace 功能,但无法正常工作。
还想回显输入区域内的完整图像 URL 路径,以便于复制。
提前感谢 advice/solution。
这是完整的脚本...
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) 如果前两个句号正确,请将其删除?
if (substr($uploadresult,0,2) == "..")
$uploadresult = substr($uploadresult,2);
2) 为什么不把输入改成
value ='{dirname(dirname($url).$uploadresult)}' 来解决这个问题?