Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

php - Image uploader and slider

All my images upload next to each other rather than seperate images. How to fix?

I have a 500 x 300 px box

ScreenShot so u can see what i mean

<?php
    if(isset($_FILES['ImageUpload'])){
        // if the form has been submitted
        $imageName = $_FILES['ImageUpload']['name'];
        $imageTemp = $_FILES['ImageUpload']['tmp_name'];
        $imageType = $_FILES['ImageUpload']['type'];
        // filter image name
        $imageName = preg_replace("#[^a-z0-9.]#i","",$imageName);

        // error handling
        if(!$imageName){
            echo("Please select a file to upload");
        }else{
            move_uploaded_file($imageTemp, "uploads/$imageName");
        }
    }

?>

<style type="text/css">
    #slider{
        width:500px;
        height:300px;
        overflow:hidden;
        margin:30px auto;
        border:2px solid grey;
    /*  background-image:url(images/Progress.gif); */
        background-repeat:no-repeat;
        background-position:center;
    }

</style>

<script type="text/javascript" src="includes/js/jquery.js"></script>

<script type="text/javascript">
    sliderInt = 1;
    sliderNext = 2;

    $(document).ready(function() {
        $("#slider > img#1").fadeIn(300);
        startSlider();
    });

    function startSlider(){
        count = $("#slider > img").size();
        loop = setInterval(function(){

            if(sliderNext > count){
                sliderNext = 1;
                sliderInt =1;
            }

            $("#slider > img").fadeOut(300);
            $("#slider > img#" + sliderNext).fadeIn(300);

            sliderInt = sliderNext;
            sliderNext = sliderNext +1;

        }, 3000)    
    }


</script>


<form action="slider_custom.php" id="uploadsForm" method="POST" enctype="multipart/form-data">

    <input type="file" name="ImageUpload"/> Select a file to upload... <br/><br/>

    <input type="submit" value="Upload"/>

</form>


<div id="slider">


    <?php
        $imageDisplay = "";
        $images = scandir("uploads");
        $ignore = array(".","..");
        $i = '1';
        foreach($images as $file){
            if(!in_array($file, $ignore)){
                $imageDisplay .= '<img id="1" src="uploads/'.$file.'" border="0"/>';
                $i++;
            }
        }

        echo($imageDisplay);
    ?>
</div><!-- end .slider -->
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are echoing the <img> tags into the slider <div id="slider">. They are all inside that div, and displayed.

You could add a style to initially hide them and then have your jquery loop show them one by one.

Also, you probably want to increase the id on each iteration.

Something like:

$i = 1; // no need for quotes here
foreach($images as $file){
        if(!in_array($file, $ignore)){
            $imageDisplay .= '<img style="display:none" id="'.$i.'" src="uploads/'
                             .$file.'" border="0"/>';
            $i++;
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...