2017-05-27 42 views
0

問題:需要一個表單進程自動重新命名上傳照片

我有一個PHP緩存代碼的名字「我的形式生成的html的網頁的基礎上,在表單字段的一個數據我形成。

<!--Start Buffer-> 
<?php ob_start(); $URL=preg_replace('#[^_0-9a-zA-Z]#', '_', $_REQUEST['timeValue']); ?> 

即特定形式字段是自動填充與當前UNIX毫秒時鐘。

<!--FormPage--> 
    <!--Timevalue/Unix millisecond code/field-> 

    <div class="formElement">TIMESTAMP URL<br><input id="timeValue" name="timeValue"></div> 
    <script src="timeValueURL.js"></script> 

所以......我的.html文件最終保存爲「名」(即數字),這是因爲無論「毫秒」是在被「提交」我的形式時刻相同。

*注意:我的「提交」按鈕也會在提交時更新毫秒時鐘。

生成的.html文件然後保存到其中一個有效的目錄中; '類別/新聞','類別/運動'或'類別/娛樂'。 (例如)如果.html文件被保存到'news'目錄中......並且當表單被提交時毫秒時鐘爲'1500000000000'... .html文件將有一個url 「mywebsite.com/categories/news/1500000000000.html」


在相同的形式......我有我的觀衆從自己的服務器/設備上傳的圖像文件的文件上傳字段。

注意:允許的圖像文件類型僅限於JPEG,PNG和GIF。

這些上傳的圖像保存在名爲'categories-images'的文件夾中。

目前......這些圖像文件保存到他們上傳時的「名稱」下的那個文件夾中。

示例:如果用戶上傳名爲'blueimage.jpg'的JPEG圖像,則該文件將保存在URL'mywebsite.com/categories/category-images/blueimage.jpg'中。

相反...我想讓圖像文件與.html文件共享相同的「名稱」。

因此,使用上面的例子...圖像應該有「1500000000000.jpg」的名稱,並有

「mywebsite.com/categories/category-images/1500000000000.jpg」的URL地址。


問題:

有沒有辦法實現這個類似,甚至可能是在引入命名.html文件中使用的代碼?


注:

它發生,我認爲我應該包括圖像處理以及PHP的。

所以這裏是....

<?php 
    $target_dir = "articles/article-images/"; 
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; 
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
    if(isset($_POST["submit"])) 
    { 
     $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
     if($check !== false) 
     { 
      echo "" . $check[""] . ""; $uploadOk = 1; 
     } 
     else 
     { 
      echo " &#xd7; FILE IS NOT AN IMAGE"; $uploadOk = 0; 
     } 
    } 
    if(file_exists($target_file)) 
    { 
     echo " &#xd7; THIS IMAGE ALREADY EXIST ON SERVER"; $uploadOk = 0; 
    } 
    if ($_FILES["fileToUpload"]["size"] > 500000) 
    { 
     echo " &#xd7; FILE IS TOO LARGE"; $uploadOk = 0; 
    } 
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") 
    { 
     echo " &#xd7; ONLY JPG, JPEG, PNG & GIF FILES ARE PERMITTED"; $uploadOk = 0; 
    } 
    if ($uploadOk == 0) 
    { 
     echo " &#xd7; IMAGE WAS NOT UPLOADED"; 
    } 
    else 
    { 
     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
     { 
      echo '<img class="fixed-ratio-resize" src="../../articles/article-images/'. basename($_FILES["fileToUpload"]["name"]). '">'; 
     } 
     else 
     { 
      echo " &#xd7; IMAGE WAS NOT UPLOADED"; 
     } 
    } 
?> 

回答

0

你應該只需要採取的HTML文件(毫秒,在以往任何時候讀取的代碼爲)的名稱和更換$_FILES["fileToUpload"]["name"]。我將把該值作爲$millisecs作爲參考,其擴展名爲$imageFileType

<?php 
# Presumably this whole thing is based off a post, so just check at the top 
if(!isset($_POST["submit"])) { 
    # Let the user know, this this can not be accessed without a submission 
    die('Nothing submitted.'); 
} 
# Set the extension first since you aren't going to use the name 
$imageFileType = pathinfo($_FILES["fileToUpload"]["name"],PATHINFO_EXTENSION); 
# Set dir 
$target_dir = "articles/article-images/"; 
# Add your milliseconds carried over from your html script. Add extension, 
# this will be the new name 
$target_file = $target_dir.$millisecs.".{$imageFileType}"; 
# Default affermative 
$uploadOk = 1; 
# Get image size 
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
if($check !== false) { 
    # I am not suer what this is...it doesn't look like you should echo 
    # anything at all... 
    echo "".$check[""].""; 
    $uploadOk = 1; 
} 
else { 
    echo " &#xd7; FILE IS NOT AN IMAGE"; 
    $uploadOk = 0; 
} 

if(file_exists($target_file)) { 
    echo " &#xd7; THIS IMAGE ALREADY EXIST ON SERVER"; 
    $uploadOk = 0; 
} 

if ($_FILES["fileToUpload"]["size"] > 500000) { 
    echo " &#xd7; FILE IS TOO LARGE"; 
    $uploadOk = 0; 
} 
# Set allowed file types 
$allowed = array('jpg','jpeg','png','gif'); 
#Check if type is in list 
if(!in_array(strtolower($imageFileType),$allowed)) { 
    echo " &#xd7; ONLY JPG/JPEG, PNG & GIF FILES ARE PERMITTED"; 
    $uploadOk = 0; 
} 

if($uploadOk == 0) { 
    echo " &#xd7; IMAGE WAS NOT UPLOADED"; 
} 
else { 
    # Move the temp file to the new name here 
    if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
     # Write the basename of the new file here 
     echo '<img class="fixed-ratio-resize" src="../../articles/article-images/'. basename($target_file). '">'; 
    } 
    else { 
     echo " &#xd7; IMAGE WAS NOT UPLOADED"; 
    } 
}