【问题标题】:How do I post one image at a time instead of the whole folder?如何一次发布一张图片而不是整个文件夹?
【发布时间】:2017-09-06 04:21:49
【问题描述】:

这是我的代码,现在所有图像都在点击发布按钮后上传和显示,我试图让它们在点击发布按钮后一次显示一个,我试过丢失if 语句后的 continue 语句,但会导致图像损坏。我尝试在输出后添加中断,但当然只显示初始图像,有什么建议吗?

<?php  
$folder = "images/";

if(isset($_POST['post'])){
    if (is_dir($folder)) 
    {
        if ($open = opendir($folder)) 
        {
            while (($file = readdir($open)) != false)
            {
                if ($file == '.' || $file == '..')continue;
                {
                    echo '<img src = "images/'.$file.'" width = "150" height = 150>';
                }
            }

            closedir($open);
        }
    }


    if (isset($_POST['upload'])) 
    {
        $file_name = $_FILES['file']['name'];
        $file_type = $_FILES['file']['type'];
        $file_size = $_FILES['file']['size'];
        $file_tem_loc = $_FILES['file']['tmp_name'];
        $file_store = "images/".$file_name;

        if (move_uploaded_file($file_tem_loc, $file_store)) 
        {
            echo "files successfully uploaded";
        }
    }

}

?>

<form action = "?" method = "POST" enctype = "multipart/form-data">

    <label>Uploading Files</label>
    <p><input type = "file" name = "file"/> </p>
    <p> <input type = "submit" name = "upload" value = "Upload Images"> </p>
    <p> <input type = "submit" name = "post" value = "Post"> </p>
</form>

【问题讨论】:

  • 您只是在特定目录中显示图像。由于您将 if (isset($_POST['upload'])) 保留在 if(isset($_POST['post'])) 中,因此上传功能将无法正常工作
  • 您的具体要求是什么
  • 是的,我注意到我唯一的要求是一次发布一张图片而不是整个文件夹,并且文件不必位于特定目录中
  • 请尝试将 if (isset($_POST['upload'])) 移出其他条件
  • 我试过了,结果一样

标签: php forms image post file-upload


【解决方案1】:

请尝试以下代码,

<?php  
$folder = "images/";
if (isset($_POST['upload'])) 
{
    $file_name = $_FILES['file']['name'];
    $file_type = $_FILES['file']['type'];
    $file_size = $_FILES['file']['size'];
    $file_tem_loc = $_FILES['file']['tmp_name'];
    $file_store = "images/".date('YmdHis').'_'.$file_name;

    if (move_uploaded_file($file_tem_loc, $file_store)) 
    {
        echo "<h3>File successfully uploaded<br>Here is the image that you recently uploaded</h3>";
        echo '<img src = "'.$file_store.'" width = "150" height = 150>';
    }
}

echo "<h3>Here are the images that you uploaded so far</h3>";
if (is_dir($folder)) 
{
    if ($open = opendir($folder)) 
    {
        while (($file = readdir($open)) != false)
        {
            if ($file == '.' || $file == '..')continue;
            {
                echo '<img src = "images/'.$file.'" width = "150" height = 150>';
            }
        }
        closedir($open);
    }
} ?>

<form action="?" method="POST" enctype="multipart/form-data">
    <label>Uploading Files</label>
    <p><input type = "file" name = "file"/> </p>
    <p> <input type = "submit" name = "upload" value = "Upload Images"> </p>
    <!--<p> <input type = "submit" name = "post" value = "Post"> </p>-->
</form>

请注意,我已通过在文件名中附加时间戳来重命名上传的文件名,以避免上传多个相同的图像,在这种情况下,它将覆盖现有文件或不允许上传新文件

【讨论】:

  • 谢谢,但这不是我想要做的,你知道我如何通过发布按钮一次发布一张图片吗?
  • @abiel,它实际上一次只上传一个文件。是否要选择多个文件进行上传,然后一次上传一个文件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-30
  • 2015-05-09
  • 1970-01-01
  • 1970-01-01
  • 2023-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多