需要表单流程自动重新命名上传的照片

时间:2017-05-27 03:58:30

标签: php forms file-upload buffer file-rename

ISSUE:

我有一个名为'的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文件最终会以&#39;名称&#39;保存。 (即数字),与任何&#39;毫秒&#39;相同。目前我的表格已经提交了#。

*注意:我的提交&#39;按钮也更新&#39;提交的毫秒时钟。

然后将生成的.html文件保存到其中一个有效目录中; &#39;类别/新闻&#39;,&#39;类别/体育&#39;或者&#39;类别/娱乐&#39;。

所以,(示例)如果.html文件保存到&#39;新闻&#39;目录......毫秒时钟位于&#39; 1500000000000&#39;提交表单时...... .html文件会有一个网址&my;我的网站上有一个网址:mywebsite.com/categories/news/1500000000000.html'

以相同的形式...我有一个文件上传字段供访问者从他们自己的服务器/设备上传图像文件。

注意:允许的图像文件类型仅限于JPEG,PNG和GIF。

这些上传的图片会保存在名为&#39; categories-images&#39;。

的文件夹中

目前......这些图片文件以&#39;名称&#39;保存到该文件夹​​。他们上传时就有了。

示例:如果用户上传名为&#39; blueimage.jpg&#39;的JPEG图像,该文件将保存在网址mywebsite.com/categories/category-images/blueimage.jpg& #39;

相反......我希望图片文件能够共享相同的名称&#39;作为.html文件。

所以,使用上面的例子......图像的名称应为&#39; 1500000000000.jpg&#39;并有一个网址

&#39; mywebsite.com/categories/category-images/1500000000000.jpg'

问题:

有没有办法实现这个,或者甚至可以合并用于命名.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";
        }
    } 
?>

1 个答案:

答案 0 :(得分: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";
    }
}